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.

4 comments:

Anonymous said...

Hy this example works fine for me, but i need to specify a context for my web service. Just like the @WebContext(http://www.jboss.org/file-access/default/members/jbossas/freezone/docs/Server_Configuration_Guide/beta500/html/ch05s27s01s02.html) does.

Do u know how i can do that?

thanks..

Anonymous said...

Hi there

How do you get jwsc to package a persistence.xml file into the generated ear file when you want to use a webservice which is build on a stateless session bean, which in turn is a facade on a collection of entity beans

Anonymous said...

Hi Debu,

Thank you for the article and the update for WLS. I just want to point out that at least in WLS 10gR3 you do not need to run an ant script to accomplish deployment.

I was able to deploy this bean dumbed down to echo hello from Oracle WebLogic Workshop with annotations and then expose the bean on OSB (Oracle Service Bus ) as a business service and it worked like a champ.

For additional reference, I used James Bayers's blog at

http://blogs.oracle.com/jamesbayer/2008/08/

for the workshop pieces. I will put together a post together on this in the near future.

Once again thank you, the book has been very helpful.

Matt Baldwin

Cali said...

Hi, Matt.
How do you expose it on OSB? Is EJB3 supported in OSB 10gR3?
Thanks.
Carlos Gremmelmaier