Creating an order service application with JMS and JBoss Server 4.2.2 in Eclipse

Introduction

This is a simple J2EE application using JMS for asynchronous processing of
an order sented by a client. The message broker use is JBossMQ already in JBoss Server, so there is no other configuration required to develop a simple JMS application.

To follow this tutorial, you will need the following:
1. JBoss Server 4.2.2
2. Eclipse 3.3

Configuring the IDE with the JBoss Server.
In the right-bottom, Click on the Servers Tab, then Right click in the view and choose New > Server as shown below

server11
Choose JBoss AS 4.2 and make sure the “Default” configuration is selected,then click on Next and Finish.
config_ide
Finally, Click on the Run icon run
Output:
run_console_1
Stop the server and lets continue to the actual development.

Creating the an EJB Project.
1. Select File > New > Project… , then Expand EJB and select EJB Project.

create1
2.Click on Next and Set the
Project Name: OrderService as shown below.

create2
3.Click on Next, then Finish

create3
Finally the project with name OrderService is created.

create

Creating the messaging service
1.Create class “Order” with fields as shown below

orderc
2.Then create setter and getter methods for the fields.
3.Because the object Order will transmitted over the network it needs to serialised.
4.Next create another class “OrderProcessingMDB” as shown below.

ordermdb1
This class needs a bit of explanation.This is a message driven bean
because of the annotation @MessageDriven.
The activation configuration properties will created by the server during
deployment.

How does this works?
This class is called by the server itself when a message arrives at queue point
with the destination name: “queue/OrderRequestQueue”.The class implements the MessageListener which has only one method called onMessage(Message m),which is called by the server when a message arrives.

To complete the Message Driven Bean
add this method which contains the logic to be processed on message arrived

method1

The final project structure in Eclipse looks as shown below

serive_pkg

Running the OrderService
Right click on the project node,then choose Run As > Run on Server.
This will make the project deployed to the server folder “default/deploy”

deployed

Developing the OrderService client

1.Goto File > New, then Project…
2.Under node J2EE, select Application Client Project and click on Next.
client1

3.Set the Project Name: OrderClient. Make sure JBoss Runtime and Default Configuration for JBoss Runtime 4.2.2 is set correctly.
4.Now lets create the client class “OrderClient”.
client2

5.Edit the class to reflect as shown below:

client31
Walking through the code:

We use the javax.naming.Context interface to do a lookup for the Destination and the ConnectionFactory.In order for the client to know the connection to the JBossMQ the provider url is set in the Properties class.
In the @MessageDriven, there is destination name “queue/OrderRequestQueue”, this name is used
for the lookup of the destination.
ConnectionFactory by default can be look up by “ConnectionFactory”.
For further reading goto JBoss.org

Continuation of code:
6. Add the code below to what was done in Step 5.

client42

Walking through the code:

Once we lookup for the destination and the connectionFactory.We can move on what is happening in step 6.In order for the client to send a message (message types: Object, btye, text) we ned a MessageProducer that is created from the session,which is in turn created from the Connection.
The steps iterated here is basically the same in sending a message to message borker in EJB pespertive.

Running the project:
Finally lets run the project:
1.Make the sure the JBoss Server is running.
2.Right click on the OrderClient node,choose Run As > Application
Turn to your server to see the output:

output1

~ by frankappiahnsiah on December 15, 2008.

6 Responses to “Creating an order service application with JMS and JBoss Server 4.2.2 in Eclipse”

  1. I’m getting the following error when deploying the OrderService.jar. Looks like since it’s an EJB project it expects at least one EJB project specified correctly in ejb-jar.xml?

    org.xml.sax.SAXException: cvc-complex-type.2.4.b: The content of element ‘ejb-jar’ is not complete. One of ‘{“http://java.sun.com/xml/ns/j2ee”:display-name, “http://java.sun.com/xml/ns/j2ee”:icon, “http://java.sun.com/xml/ns/j2ee”:enterprise-beans}’ is expected. @ *unknown*[5,11]

  2. yes it is an ejb project and does require ejb-jar.xml . i don’t know how you created the application but with Eclipse this is created for you. So do add this xml to the jar for it to work.

  3. I tried deploying the OrderService.jar. But jboss is throwing an expcetion ( JMS Activation ) and bcos of this its giving naming not found though i can see the destinatio name in the queue ( JMS console). I verified the destination name and are same in both MDB as well as client.

  4. I am new to JBoss Messaging. I followed the steps in JBOSS5.0.
    While executing the onMessage(..) method of OrderProcessingMDB, I am getting the exception as shown below. I found that while executing objMessage.getObject(), getting the exception.
    Can you please let me know how to resolve this issue?

    2009-08-23 16:01:32,078 WARN [org.jboss.ejb3.interceptors.aop.InterceptorsFactory] (WorkManager(2)-3) EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, InterceptorRegistry should be used via the bean container
    2009-08-23 16:01:32,218 WARN [org.jboss.ejb3.interceptors.aop.InterceptorsFactory] (WorkManager(2)-3) EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, InterceptorRegistry should be used via the bean container

    2009-08-23 16:01:33,156 ERROR [org.jboss.aspects.tx.TxPolicy] (WorkManager(2)-4) javax.ejb.EJBTransactionRolledbackException: order.service.client.Order
    2009-08-23 16:01:33,156 ERROR [org.jboss.resource.adapter.jms.inflow.JmsServerSession] (WorkManager(2)-4) Unexpected error delivering message delegator->JBossMessage[20496768875511809]:PERSISTENT, deliveryId=1
    javax.ejb.EJBTransactionRolledbackException: order.service.client.Order
    at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:115)
    at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
    at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:194)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:80)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.mdb.MessagingContainer.localInvoke(MessagingContainer.java:282)
    at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.delivery(MessageInflowLocalProxy.java:270)
    at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.invoke(MessageInflowLocalProxy.java:140)
    at $Proxy280.onMessage(Unknown Source)
    at org.jboss.resource.adapter.jms.inflow.JmsServerSession.onMessage(JmsServerSession.java:178)
    at org.jboss.jms.client.container.ClientConsumer.callOnMessageStatic(ClientConsumer.java:160)
    at org.jboss.jms.client.container.SessionAspect.handleRun(SessionAspect.java:831)
    at org.jboss.aop.advice.org.jboss.jms.client.container.SessionAspect_z_handleRun_28743023.invoke(SessionAspect_z_handleRun_28743023.java)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.jms.client.container.ClosedInterceptor.invoke(ClosedInterceptor.java:170)
    at org.jboss.aop.advice.PerInstanceInterceptor.invoke(PerInstanceInterceptor.java:86)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.jms.client.delegate.ClientSessionDelegate.run(ClientSessionDelegate.java)
    at org.jboss.jms.client.JBossSession.run(JBossSession.java:199)
    at org.jboss.resource.adapter.jms.inflow.JmsServerSession.run(JmsServerSession.java:234)
    at org.jboss.resource.work.WorkWrapper.execute(WorkWrapper.java:205)
    at org.jboss.util.threadpool.BasicTaskWrapper.run(BasicTaskWrapper.java:260)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)
    Caused by: java.lang.RuntimeException: order.service.client.Order
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.jboss.classloader.spi.base.BaseClassLoaderDomain.loadClass(BaseClassLoaderDomain.java:292)
    at org.jboss.classloader.spi.base.BaseClassLoaderDomain.loadClass(BaseClassLoaderDomain.java:1119)
    at org.jboss.classloader.spi.base.BaseClassLoader.loadClassFromDomain(BaseClassLoader.java:798)
    at org.jboss.classloader.spi.base.BaseClassLoader.loadClass(BaseClassLoader.java:441)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
    at org.jboss.messaging.util.ObjectInputStreamWithClassLoader.resolveClass(ObjectInputStreamWithClassLoader.java:78)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at org.jboss.messaging.util.StreamUtils.readObject(StreamUtils.java:154)
    at org.jboss.messaging.core.impl.message.MessageSupport.readPayload(MessageSupport.java:392)
    at org.jboss.jms.message.JBossObjectMessage.getObject(JBossObjectMessage.java:126)
    at org.jboss.jms.message.ObjectMessageProxy.getObject(ObjectMessageProxy.java:68)
    at order.service.jms.OrderProcessingMDB.onMessage(OrderProcessingMDB.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
    at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
    at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
    at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
    at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_28743023.invoke(InvocationContextInterceptor_z_fillMethod_28743023.java)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
    at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_28743023.invoke(InvocationContextInterceptor_z_setup_28743023.java)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
    … 30 more

  5. Ravi, i think possibly the message is not of type ObjectMessage. Check if it is an instance of the ObjectMessage before calling getObject() method on the Message object.

    By the meantime i will try to deploy it in JBoss 5 to verify if i get the same error when i get some free time at hand.

  6. Thanks for the reply. The reason for the above error is my Order bean is in different packages of client and server applications. If we create the order bean in same package in both client and server applications, it is working properly.

Leave a Reply