Thursday, June 26, 2008

EJB 3 Stateless bean Web service in WebLogic Server

I recently got some questions from customers about the procedure for deploying EJB 3 Session Bean Web service in WebLogic 10. In this blog, I will outline the steps.

First develop your EJB3 session bean web service as follows:

@WebService(name="PlaceBid",
serviceName="PlaceBidService", portName = "PlaceBidPort",
targetNamespace = "http://actionbazaar.com/xml")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
@PersistenceContext(unitName="actionBazaar",name="actionBazaar")
@Stateless
public class PlaceBidBean {

@WebMethod

public Long addBid(String userId, Long itemId, Double bidPrice) throws BidException {

….

}

}


Deployment

Unlike OC4J, WebLogic do not support parsing of JAX-WS annotations at deployment time and if you try to deploy the compiled EJB 3 Web service, you will get deployment time exception. You have to run the EJB 3 WS thru annotation parsing tool (jwsc –java web services compile) supplied with WebLogic to generate the artifacts and package in your ejb-jar module.

Here is an example ant task to create the web service:

<target
name="build-ws-server" description="Target that builds the
target Web Service">
<jwsc
srcdir="${src.ejb.dir}"
destdir="${bld.service.dir}"
classpath="${common.j2ee.class.path}"
fork="true"
keepGenerated="true"
deprecation="${deprecation}"
keepTempFiles="true"
listfiles="true"
debug="${debug}">
<jws
file="actionbazaar/buslogic/PlaceBidBean.java" type="JAXWS"
explode="true"/>
</jwsc>
</target>

<target name="package-ejb"
depends="compile-ejb-classes,build-ws-server">;

<mkdir dir="${bld.ejb.dir}/actionbazaar/buslogic/jaxws" />

<copy todir="${bld.ejb.dir}/actionbazaar/buslogic/jaxws">

<fileset dir="${bld.service.dir}/actionbazaar/buslogic/PlaceBidBean/actionbazaar/buslogic/jaxws" includes="*"/> </copy>
<echo message="-----> Create EJB jar file"/>
<jar jarfile="${bld.ear.dir}/${ejb.name}.jar">
<fileset dir="${bld.ejb.dir}" includes="**" />
</jar>
</target >

Make sure to package the generated artifacts in the EJB JAR and then deploy the EJB-JAR or EAR to the WebLogic Server. Note that if your web service depends upon custom Java objects - the generated artifacts contain duplicate Java classes that you already may have. These duplicated classes are on a separate package structure and cause ClassCastException for me. So I avoid packaging these classes.

Accessing EJB 3 Web service

After successful deployment you can access the web service as follows:

http://hostname:port/ejb-name/web-service-name?wsdl

http://localhost:7001/PlaceBidBean/PlaceBidBeanService?wsdl



Hope this helps.

You can download the working examples from here. Chapter 15 contains web services example.

Friday, June 20, 2008

EJB 3, JPA, Web Services and Spring Examples for WebLogic Server

Finally I was able to complete porting of all book examples of EJB 3 In Action to run on WebLogic Server.

You can download these from here


These contain a host of examples on EJB 3, JPA, Web Services and Spring Framework. Now we have complete examples for four servers: Glassfish, OC4J, WebLogic and JBoss.

Note that I tested the examples with an Oracle XE database. For simplicity, the examples are configured to use the exampleServer and default PointBase DB that is shipped with it. However some of these examples will properly not on PointBase because it does not support DB Sequence.

All chapters have the common domain model based on our famous ActionBazaar Application

Chapter 5: EJB 3 Interceptors, Timers
Chapter 8: O-R Mapping Annotations
Chapter 9: Entity Manager API
Chapter 10: JPQL and Native SQL Queries
Chapter 11: O-R Mapping with XML
Chapter 12: EM API from Web Tier, Java SE
Chapter 13: Extended Persistence Context
Chapter 15: EJB 3 Web services
Chapter 16: Spring with EJB 3 and JPA

Please try these out and let me know if you find any bugs!

Monday, June 02, 2008

EJB 3 Dependency Injection Refcard



I wrote a Reference Card on EJB 3 Dependency Injection for Developer Zone. This is a quick reference card that describes metadata annotations and XML descriptor elements that you use for injecting resources and services.

You can access this reference card for free from Developer Zone at http://refcardz.dzone.com/refcardz/dependency-injection-in-ejb3