Despite tons of material, downloads and websites on JAVA enterprise applications, tools and features, there are few sources only for “Let’s get started” styled tutorials. The Netbeans tutorial is often the best starting point (link).
I was reading about the background of JMS, some of the material is of a very theoretical nature and I tried to find some simple sample to get my hands dirty. A good short article on JMS in the glassfish context you can find at java.net (link)
This tutorial describes the steps to get a most simple messaging application up and running implementing a one-to-many communication using topics, also called publish-subscribe. A enterprise client creates a message put it on a topic and a message-driven bean (MDB) retrieves the message.
Using: Netbeans 6.5 with installed Glassfish V2.
1. Creating a new JAVA EE project.
2. We call it MyJMS. Disable the web-application and enable application-client
3. We will have a project window with 2 subprojects for the EJB and the application client.
4. Before we start with the coding of the EJB and the client, we configure the applicationserver. Open the admin console via right click on the application server under services (Start the server first, if not done yet). You remember the default admin password for glassfish ? “adminadmin”.You better change if your box is open to outside world.
In the admin console we create the ressources for the JMS topic.
Create a destination resource with JNDI name “jms/topic” and Resource type “javax.jms.topic” (rest default settings).
Create a connection factory with the JNDI name “jms/TopicFactory” (rest default settings, but remove username and password).
5. Now we take care of the application client who will send a message to the topic destination.
The IDE created a main.java template for us.
Resource injection by adding these 4 lines
@Resource(mappedName=”jms/TopicFactory”)
private static ConnectionFactory topicFactory;
@Resource(mappedName=”jms/Topic”)
private static Topic topic;
Adding code to create a connection an send message
Connection topicConnection = null;
Session session = null;
MapMessage message = null;
MessageProducer producer = null;
try {
topicConnection = topicFactory.createConnection();
session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(topic);
message = session.createMapMessage();
message.setString(“lastname”, “Smith”);
message.setString(“firstname”, “John”);
message.setString(“id”, “0100″);
producer.send(message);
}
catch (Exception e) {…
6. Now we create a message-driven bean that consumes the message. Let’s call it MessageBean
We create it in the package ejb and connect it to the server destination we created earlier. Netbeans creates a basic template for us.
We need to inject the resources again:
@Resource(mappedName=”jms/TopicFactory”)
private ConnectionFactory topicFactory;
@Resource(mappedName=”jms/Topic”)
private Topic topic;
And we add some code to onMessage method:
MapMessage msg = null;
try {
msg = (MapMessage)message;
System.out.println(“—————————————-”);
System.out.println(msg.getString(“lastname”));
System.out.println(msg.getString(“firstname”));
System.out.println(msg.getString(“id”));
System.out.println(“—————————————-”);
}
catch (Exception e) {…
7. Lets run it:
Glassfish Log:
—————————————-
Smith
John
0100
—————————————-
Conclusion: This is the most basic version of a messaging application. Please note there is no proper error handling or any business logic or whatsoever. There is no documentation in the above code because some of the abstract commands are so sophisticated that the tutorial would become a compendium and you are back to square one: It is too much to swallow.
My recommendation: Get something simple up and running and then start tweaking and start to understand.
Potential Problems:
- You might hit a cast error when trying to receive the message. Change the JMS settings from EMBEDDED to LOCAL. Dont ask me why.
DirectConsumer:Caught Exception delivering messagecom.sun.messaging.jmq.io.Packet cannot be cast to com.sun.messaging.jms.ra.DirectPacket













Pretty Cool!!!
Your comment on potential problem is really commendable & funny indeed
Thanks for the tutorial.
Thanks alot for this simple example.
But when I run the client I get a NullPointerException on the line:
topicConnection = topicFactory.createConnection();
looks like the ConnectionFactory isn’t being injected. Any idea why and how to fix it?
BTW I used the same example (after changing it a little) to create a MDB for a queue.
This is a great sample…
But, like Rawl, I get the same NullPointerException.
Is there a fix for this?
Thanks!
Thank for this example, after reading documentation about JMS and MDB i have been searching for a simple sample to apply whatever i read, finally i got this tutorial, which is simple , but covers most of the aspects of JMS, this sample is working well for me,
To avoid confusion
While creating the destination resource with JNDI name “jms/topic” and Resource type “javax.jms.topic” (rest default settings).
JNDI name should be jms/Topic. (‘T’ not ‘t’)
My glassfish v2 asked for a ‘Physical Destination Name’. Give some junk value or you can first add a physical destination under admin console->configuration->Java Message Service->Physical Destinations->New (“somename” as name, “javax.jms.Topic” as Type) and then specify the newly added physical destination name.
While creating a connection factory with the JNDI name “jms/TopicFactory” (rest default settings, but remove username and password) specify the type as “javax.jms.TopicConnectionFactory”
Regards,
Rubic INDIA
HI All:
Good tutorial, except I can’t get it to work. I am running NetBean 6.8.1M on Ubuntu 9.04 . Glassfish v3 prelude. When I try to setup the Destination Resource with the following values, I get this error on the glassfish admin console.
JNDI Name: jms/Topic
Physical Destination Name : “somename”
Resource Type: javax.jms.Topic
Status: Enabled
Then I get this error once I press the OK button to create the resource.
An error has occurred.
org.hibernate.ejb.HibernatePersistence.isLoadedWithoutReference(Ljava/lang/Object;Ljava/lang/String;)Ljavax/persistence/spi/LoadState;
The error message above ie “Hibernate” error on Netbean Admin Console was coming from glassfish V3 Prelude.
I change to glassfish V2 latest built and things work as they are supposed to.
Thanks again for the above example.
Well written tutorial, but I still can’t get it to work…
I’m getting CONNECTION REFUSED all over the place, and it seems like it wants to connect to port 3700 for some reason even though JMS appears to be running on 7676. Any ideas?
Never mind! Got it fixed, had to go into domain.xml and sun-acc.xml and fix my domain name. I had changed the domain name of my machine since my last install of GlassFish v2-ur1.
Thanks for the nice tutorial!!
Excellent example, but does not work. I have the following error:
“DPL8007: Elemento message-destination y valor null de descriptores de implementación no válidos”
endpoint.determine.destinationtype
MQJMSRA_DC2001: connectionId=8999358329247142656:_setClientID():JMSService.setClientId():JMSServiceException=setClientId: set client ID failed. Connection ID: 8999358329247142656, Client ID: MessageBean, Shareable: false, nameSpace: null
createConsumer on JMSService:jmsdirect failed for connectionId:8999358329247142656 and sessionId:8999358329247146496 due to
javax.resource.NotSupportedException: MQRA:EC:Error creating Direct Message Consumer:
createConsumer on JMSService:jmsdirect failed for connectionId:8999358329247142656 and sessionId:8999358329247146496 due to unkown JMSService server error.
at com.sun.messaging.jms.ra.EndpointConsumer.createDirectMessageConsumer(EndpointConsumer.java:966)
at com.sun.messaging.jms.ra.EndpointConsumer._init(EndpointConsumer.java:305)
at com.sun.messaging.jms.ra.EndpointConsumer.(EndpointConsumer.java:215)
at com.sun.messaging.jms.ra.ResourceAdapter.endpointActivation(ResourceAdapter.java:604)
at com.sun.enterprise.connectors.inflow.ConnectorMessageBeanClient.setup(ConnectorMessageBeanClient.java:256)
at com.sun.ejb.containers.MessageBeanContainer.(MessageBeanContainer.java:209)
at com.sun.ejb.containers.ContainerFactoryImpl.createContainer(ContainerFactoryImpl.java:524)
at com.sun.enterprise.server.AbstractLoader.loadEjbs(AbstractLoader.java:527)
at com.sun.enterprise.server.ApplicationLoader.doLoad(ApplicationLoader.java:191)
at com.sun.enterprise.server.TomcatApplicationLoader.doLoad(TomcatApplicationLoader.java:126)
at com.sun.enterprise.server.ExtendedApplicationLoader.doLoad(ExtendedApplicationLoader.java:134)
at com.sun.enterprise.server.AbstractLoader.load(AbstractLoader.java:238)
at com.sun.enterprise.server.AbstractManager.load(AbstractManager.java:226)
at com.sun.enterprise.server.ApplicationLifecycle.onStartup(ApplicationLifecycle.java:224)
at com.sun.enterprise.server.ApplicationServer.onStartup(ApplicationServer.java:446)
at com.sun.enterprise.server.ondemand.OnDemandServer.onStartup(OnDemandServer.java:134)
at com.sun.enterprise.server.PEMain.run(PEMain.java:409)
at com.sun.enterprise.server.PEMain.main(PEMain.java:336)
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 com.sun.enterprise.server.PELaunch.main(PELaunch.java:415)
Caused by: javax.jms.JMSException: createConsumer on JMSService:jmsdirect failed for connectionId:8999358329247142656 and sessionId:8999358329247146496 due to unkown JMSService server error.
at com.sun.messaging.jms.ra.DirectSession._createAndAddConsumer(DirectSession.java:1645)
at com.sun.messaging.jms.ra.DirectSession.createDurableSubscriber(DirectSession.java:354)
at com.sun.messaging.jms.ra.EndpointConsumer.createDirectMessageConsumer(EndpointConsumer.java:944)
… 22 more
Caused by: com.sun.messaging.jmq.jmsservice.JMSServiceException: addConsumer: Add consumer failed. Connection ID: 8999358329247142656, session ID: 8999358329247146496
at com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.addConsumer(IMQDirectService.java:1320)
at com.sun.messaging.jms.ra.DirectSession._createAndAddConsumer(DirectSession.java:1595)
… 24 more
Caused by: java.lang.NullPointerException
at com.sun.messaging.jmq.jmsserver.core.Subscription.calcHashcode(Subscription.java:260)
at com.sun.messaging.jmq.jmsserver.core.Subscription.(Subscription.java:287)
at com.sun.messaging.jmq.jmsserver.core.Subscription.subscribe(Subscription.java:887)
at com.sun.messaging.jmq.jmsserver.core.Subscription.subscribe(Subscription.java:854)
at com.sun.messaging.jmq.jmsserver.core.Subscription.findCreateDurableSubscription(Subscription.java:975)
at com.sun.messaging.jmq.jmsserver.core.Subscription.findCreateDurableSubscription(Subscription.java:931)
at com.sun.messaging.jmq.jmsserver.data.handlers.ConsumerHandler.createConsumer(ConsumerHandler.java:639)
at com.sun.messaging.jmq.jmsserver.data.protocol.ProtocolImpl.createConsumer(ProtocolImpl.java:479)
at com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.addConsumer(IMQDirectService.java:1303)
… 25 more
MDB00017: [MessageBean]: Se produjo una excepción al crear el contenedor de Bean controlado por mensajes: [java.lang.Exception]
java.lang.Exception
java.lang.Exception
at com.sun.enterprise.connectors.inflow.ConnectorMessageBeanClient.setup(ConnectorMessageBeanClient.java:265)
at com.sun.ejb.containers.MessageBeanContainer.(MessageBeanContainer.java:209)
at com.sun.ejb.containers.ContainerFactoryImpl.createContainer(ContainerFactoryImpl.java:524)
at com.sun.enterprise.server.AbstractLoader.loadEjbs(AbstractLoader.java:527)
at com.sun.enterprise.server.ApplicationLoader.doLoad(ApplicationLoader.java:191)
at com.sun.enterprise.server.TomcatApplicationLoader.doLoad(TomcatApplicationLoader.java:126)
at com.sun.enterprise.server.ExtendedApplicationLoader.doLoad(ExtendedApplicationLoader.java:134)
at com.sun.enterprise.server.AbstractLoader.load(AbstractLoader.java:238)
at com.sun.enterprise.server.AbstractManager.load(AbstractManager.java:226)
at com.sun.enterprise.server.ApplicationLifecycle.onStartup(ApplicationLifecycle.java:224)
at com.sun.enterprise.server.ApplicationServer.onStartup(ApplicationServer.java:446)
at com.sun.enterprise.server.ondemand.OnDemandServer.onStartup(OnDemandServer.java:134)
at com.sun.enterprise.server.PEMain.run(PEMain.java:409)
at com.sun.enterprise.server.PEMain.main(PEMain.java:336)
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 com.sun.enterprise.server.PELaunch.main(PELaunch.java:415)
Caused by: javax.resource.NotSupportedException: MQRA:EC:Error creating Direct Message Consumer:
createConsumer on JMSService:jmsdirect failed for connectionId:8999358329247142656 and sessionId:8999358329247146496 due to unkown JMSService server error.
at com.sun.messaging.jms.ra.EndpointConsumer.createDirectMessageConsumer(EndpointConsumer.java:966)
at com.sun.messaging.jms.ra.EndpointConsumer._init(EndpointConsumer.java:305)
at com.sun.messaging.jms.ra.EndpointConsumer.(EndpointConsumer.java:215)
at com.sun.messaging.jms.ra.ResourceAdapter.endpointActivation(ResourceAdapter.java:604)
at com.sun.enterprise.connectors.inflow.ConnectorMessageBeanClient.setup(ConnectorMessageBeanClient.java:256)
… 18 more
Caused by: javax.jms.JMSException: createConsumer on JMSService:jmsdirect failed for connectionId:8999358329247142656 and sessionId:8999358329247146496 due to unkown JMSService server error.
at com.sun.messaging.jms.ra.DirectSession._createAndAddConsumer(DirectSession.java:1645)
at com.sun.messaging.jms.ra.DirectSession.createDurableSubscriber(DirectSession.java:354)
at com.sun.messaging.jms.ra.EndpointConsumer.createDirectMessageConsumer(EndpointConsumer.java:944)
… 22 more
Caused by: com.sun.messaging.jmq.jmsservice.JMSServiceException: addConsumer: Add consumer failed. Connection ID: 8999358329247142656, session ID: 8999358329247146496
at com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.addConsumer(IMQDirectService.java:1320)
at com.sun.messaging.jms.ra.DirectSession._createAndAddConsumer(DirectSession.java:1595)
… 24 more
Caused by: java.lang.NullPointerException
at com.sun.messaging.jmq.jmsserver.core.Subscription.calcHashcode(Subscription.java:260)
at com.sun.messaging.jmq.jmsserver.core.Subscription.(Subscription.java:287)
at com.sun.messaging.jmq.jmsserver.core.Subscription.subscribe(Subscription.java:887)
at com.sun.messaging.jmq.jmsserver.core.Subscription.subscribe(Subscription.java:854)
at com.sun.messaging.jmq.jmsserver.core.Subscription.findCreateDurableSubscription(Subscription.java:975)
at com.sun.messaging.jmq.jmsserver.core.Subscription.findCreateDurableSubscription(Subscription.java:931)
at com.sun.messaging.jmq.jmsserver.data.handlers.ConsumerHandler.createConsumer(ConsumerHandler.java:639)
at com.sun.messaging.jmq.jmsserver.data.protocol.ProtocolImpl.createConsumer(ProtocolImpl.java:479)
at com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.addConsumer(IMQDirectService.java:1303)
… 25 more
EJB5090: Se produjo una excepción al crear el contenedor EJB [java.lang.Exception]
appId=MyJMS moduleName=MyJMS-ejb_jar ejbName=MessageBean
LDR5004: Se produjo un error inesperado al crear el contenedor ejb
java.lang.Exception
at com.sun.enterprise.connectors.inflow.ConnectorMessageBeanClient.setup(ConnectorMessageBeanClient.java:265)
at com.sun.ejb.containers.MessageBeanContainer.(MessageBeanContainer.java:209)
at com.sun.ejb.containers.ContainerFactoryImpl.createContainer(ContainerFactoryImpl.java:524)
at com.sun.enterprise.server.AbstractLoader.loadEjbs(AbstractLoader.java:527)
at com.sun.enterprise.server.ApplicationLoader.doLoad(ApplicationLoader.java:191)
at com.sun.enterprise.server.TomcatApplicationLoader.doLoad(TomcatApplicationLoader.java:126)
at com.sun.enterprise.server.ExtendedApplicationLoader.doLoad(ExtendedApplicationLoader.java:134)
at com.sun.enterprise.server.AbstractLoader.load(AbstractLoader.java:238)
at com.sun.enterprise.server.AbstractManager.load(AbstractManager.java:226)
at com.sun.enterprise.server.ApplicationLifecycle.onStartup(ApplicationLifecycle.java:224)
at com.sun.enterprise.server.ApplicationServer.onStartup(ApplicationServer.java:446)
at com.sun.enterprise.server.ondemand.OnDemandServer.onStartup(OnDemandServer.java:134)
at com.sun.enterprise.server.PEMain.run(PEMain.java:409)
at com.sun.enterprise.server.PEMain.main(PEMain.java:336)
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 com.sun.enterprise.server.PELaunch.main(PELaunch.java:415)
Caused by: javax.resource.NotSupportedException: MQRA:EC:Error creating Direct Message Consumer:
createConsumer on JMSService:jmsdirect failed for connectionId:8999358329247142656 and sessionId:8999358329247146496 due to unkown JMSService server error.
at com.sun.messaging.jms.ra.EndpointConsumer.createDirectMessageConsumer(EndpointConsumer.java:966)
at com.sun.messaging.jms.ra.EndpointConsumer._init(EndpointConsumer.java:305)
at com.sun.messaging.jms.ra.EndpointConsumer.(EndpointConsumer.java:215)
at com.sun.messaging.jms.ra.ResourceAdapter.endpointActivation(ResourceAdapter.java:604)
at com.sun.enterprise.connectors.inflow.ConnectorMessageBeanClient.setup(ConnectorMessageBeanClient.java:256)
… 18 more
Caused by: javax.jms.JMSException: createConsumer on JMSService:jmsdirect failed for connectionId:8999358329247142656 and sessionId:8999358329247146496 due to unkown JMSService server error.
at com.sun.messaging.jms.ra.DirectSession._createAndAddConsumer(DirectSession.java:1645)
at com.sun.messaging.jms.ra.DirectSession.createDurableSubscriber(DirectSession.java:354)
at com.sun.messaging.jms.ra.EndpointConsumer.createDirectMessageConsumer(EndpointConsumer.java:944)
… 22 more
Caused by: com.sun.messaging.jmq.jmsservice.JMSServiceException: addConsumer: Add consumer failed. Connection ID: 8999358329247142656, session ID: 8999358329247146496
at com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.addConsumer(IMQDirectService.java:1320)
at com.sun.messaging.jms.ra.DirectSession._createAndAddConsumer(DirectSession.java:1595)
… 24 more
Caused by: java.lang.NullPointerException
at com.sun.messaging.jmq.jmsserver.core.Subscription.calcHashcode(Subscription.java:260)
at com.sun.messaging.jmq.jmsserver.core.Subscription.(Subscription.java:287)
at com.sun.messaging.jmq.jmsserver.core.Subscription.subscribe(Subscription.java:887)
at com.sun.messaging.jmq.jmsserver.core.Subscription.subscribe(Subscription.java:854)
at com.sun.messaging.jmq.jmsserver.core.Subscription.findCreateDurableSubscription(Subscription.java:975)
at com.sun.messaging.jmq.jmsserver.core.Subscription.findCreateDurableSubscription(Subscription.java:931)
at com.sun.messaging.jmq.jmsserver.data.handlers.ConsumerHandler.createConsumer(ConsumerHandler.java:639)
at com.sun.messaging.jmq.jmsserver.data.protocol.ProtocolImpl.createConsumer(ProtocolImpl.java:479)
at com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.addConsumer(IMQDirectService.java:1303)
… 25 more
Error al intentar procesar las extensiones del Manifest del archivo JAR C:\Users\Jessica\.personalDomain\personalDomain\applications\j2ee-apps\MyJMS\MyJMS-ejb.jar; se ignorará y se continuará
java.io.FileNotFoundException: C:\Users\Jessica\.personalDomain\personalDomain\applications\j2ee-apps\MyJMS\MyJMS-ejb.jar (El sistema no puede encontrar el archivo especificado)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.(ZipFile.java:114)
at java.util.jar.JarFile.(JarFile.java:133)
at java.util.jar.JarFile.(JarFile.java:97)
at com.sun.enterprise.appclient.jws.ExtensionFileManager.findExtensionTransitiveClosure(ExtensionFileManager.java:240)
at com.sun.enterprise.appclient.jws.AppclientJWSSupportInfo.findExtensions(AppclientJWSSupportInfo.java:1545)
at com.sun.enterprise.appclient.jws.AppclientJWSSupportInfo.prepareAppclient(AppclientJWSSupportInfo.java:1386)
at com.sun.enterprise.appclient.jws.AppclientJWSSupportInfo.prepareNestedAppclient(AppclientJWSSupportInfo.java:1497)
at com.sun.enterprise.appclient.jws.AppclientJWSSupportInfo.prepareApplication(AppclientJWSSupportInfo.java:1246)
at com.sun.enterprise.appclient.jws.AppclientJWSSupportInfo.startJWSServicesForApplication(AppclientJWSSupportInfo.java:475)
at com.sun.enterprise.appclient.jws.AppclientJWSSupportManager.handleApplicationEvent(AppclientJWSSupportManager.java:156)
at com.sun.enterprise.server.event.ApplicationLoaderEventNotifier.notifyListeners(ApplicationLoaderEventNotifier.java:154)
at com.sun.enterprise.server.AbstractLoader.notifyAppEvent(AbstractLoader.java:874)
at com.sun.enterprise.server.ApplicationLoader.doLoad(ApplicationLoader.java:192)
at com.sun.enterprise.server.TomcatApplicationLoader.doLoad(TomcatApplicationLoader.java:126)
at com.sun.enterprise.server.ExtendedApplicationLoader.doLoad(ExtendedApplicationLoader.java:134)
at com.sun.enterprise.server.AbstractLoader.load(AbstractLoader.java:238)
at com.sun.enterprise.server.AbstractManager.load(AbstractManager.java:226)
at com.sun.enterprise.server.ApplicationLifecycle.onStartup(ApplicationLifecycle.java:224)
at com.sun.enterprise.server.ApplicationServer.onStartup(ApplicationServer.java:446)
at com.sun.enterprise.server.ondemand.OnDemandServer.onStartup(OnDemandServer.java:134)
at com.sun.enterprise.server.PEMain.run(PEMain.java:409)
at com.sun.enterprise.server.PEMain.main(PEMain.java:336)
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 com.sun.enterprise.server.PELaunch.main(PELaunch.java:415)
i have same problem