Good Simple example with Glassfish and a RESTful webservice Using JMS

In this little tutorial, we will create a simple MessageDriven bean. It will consume messages which are produced in a simple way by a RESTful webservice, We will also make use of Glassfish server and JMS. Let go!!


Setup Glassfish

You need a clean installation of Glassfish (v2 or v3). I also recommend an IDE with Glassfish integration. Netbeans should be easiest as I’ve heard that it’s got the best Glassfish integration, but I am using Eclipse 3.5. I won’t cover the setup process in your IDE – if you’re having difficulties, please let me know.

The few basic steps you need to do is:

  1. Install Glassfish (e.g. /dev/glassfishv3/)
  2. Setup Glassfish in Eclipse via Window->Preferences->Server->Runtime Environments and point it to /dev/glassfishv3/glassfish. The result should look like this:
  3. Add the “Server” view if it doesn’t show up yet and add a new Server (RMB->New->Server):serverconfig2
    You should get a server entry and be able to start and stop the instance.

    Please verify if that works, first. Now you can start up Glassfish.

Configure Glassfish

Open a command prompt or terminal in /dev/glassfishv3/bin/ and be sure that Glassfish is running. Then create the JMS queues in glassfish (works with v2 and v3) with the asadmin tool:

asadmin create-jms-resource –restype javax.jms.ConnectionFactory jms/SimpleConnectionFactory
asadmin create-jms-resource –restype javax.jms.Queue jms/SimpleQueue

The implementation

Now we need some code. We will use a MessageDriven bean to consume the messages and a RESTful webservice to produce them in a simple way. For the MessageDriven bean, create a “EJB project” (Eclipse: New -> Project -> EJB/EJB Project) and put the following class in there:

import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

* Simle message driven bean.
@MessageDriven(mappedName = “jms/SimpleQueue”)
public class SimpleMessageDrivenBean implements MessageListener {

* Default constructor.
public SimpleMessageDrivenBean() {
* @see MessageListener#onMessage(Message)
public void onMessage(Message message) {
try {
System.out.println(“Got message: ” + ((TextMessage)message).getText() + ” @” + System.currentTimeMillis());
} catch (JMSException e) {


Now we need a Web Project which contains the RESTful Webservice (Eclipse: New -> Project -> Web/Dynamic Web Project). Add the following class to that project:


import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.NamingException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;

* Example of a RESTful webservice producing messages for a JMS Queue.
* Call via http://localhost:8080/webservicetest/jms
* @author Matthias Fraass
@Stateless // dependency injection won’t work without that
@Path(“/jms”) // path appended to context root -> http://localhost:8080/webservicetest/jms
public class SimpleJMSRestfulWebService {

@Resource(mappedName = “jms/SimpleConnectionFactory”) //= connection factorie’s JNDI name
private ConnectionFactory connectionFactory;

@Resource(mappedName = “jms/SimpleQueue”) //= Queue’s JNDI name
private Queue queue;

* Our RESTful webservice
public String putMessage() throws NamingException, JMSException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
System.out.println(“called putMessage”);
javax.jms.Connection  connection = connectionFactory.createConnection();
javax.jms.Session        session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
long start = System.currentTimeMillis();
int i=0;
TextMessage message = session.createTextMessage();
message.setText(“Hello, JMS! #”+start);
while (i < 1000);
long now = System.currentTimeMillis();
return “JMS produced 1000 messages within ” + (now-start) + “ms”;


Now you need to configure the Jersey Servlet in web.xml, so that it picks up the WebService. Put the following in betwen the display-name and the welcome-file-list tags.


<servlet-name>Jersey Web Application</servlet-name>











<servlet-name>Jersey Web Application</servlet-name>




This WebService will produce 1000 messages and output the time it took. We’ll use that to compare the performance of different distribution scenarios and message queue products during the course of the next articles.


The last thing we need is the EAR project (New -> Project / JavaEE/Enterprise Application Project). Add the EJB and the Web project to the EAR project. The result will be:


You should now be able to deploy that in glassfish:


and then


The server.log (glassfish/domains/domain1/logs/server.log or in the Eclipse Console – be sure to switch to “server.log”) should output this:

INFO: DOL Loading time150INFO: endpoint.determine.destinationtype

INFO: Portable JNDI names for EJB SimpleJMSRestfulWebService : [java:global/JMS1/JMS1web/SimpleJMSRestfulWebService, java:global/JMS1/JMS1web/SimpleJMSRestfulWebService!]
INFO: Unknown loader org.glassfish.internal.api.DelegatingClassLoader@11b92ef class org.glassfish.internal.api.DelegatingClassLoader
INFO: Scanning for root resource and provider classes in the packages:
INFO: Root resource classes found:
INFO: Provider classes found:
INFO: Initiating Jersey application, version ‘Jersey: 1.1.0-ea 04/30/2009 06:59 PM’
INFO: Binding the EJB class to EJBManagedComponentProvider
INFO: Loading application JMS1#/C:/dev/glassfishv3/glassfish/domains/domain1/eclipseApps/JMS1/JMS1web_war/ at JMS1web
INFO: Deployment of JMS1 done is 992 ms

Then, point a browser to e.g. http://localhost:8080/JMS1web/jms/ and you should see “JMS produced 1000 messages within XYms” in your browser (doesn’t work with Chrome) The server.log should look like this:

INFO: called putMessage ..
INFO: Got message: Hello, JMS! #1257518116879 @1257518117079
INFO: Got message: Hello, JMS! #1257518116879 @1257518117089 …

Vóila! Your first JMS enterprise project is running! It is using the internal OpenMQ instance of Glassfish, which is running on port 7676 by default. That’s not *remote* messaging yet, but it can already enable you to asynchronously process tasks. That’s good for most smaller projects or local development and testing We will use that simple example as a starting point for the following, more demanding ;) tutorials which will do remote messaging.

Original Post :

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image