|Home > Documentation > Sample applications > JMS and MDB sample application|
Enterprise messaging has become an increasingly important component of loosely coupled, reliable enterprise frameworks. This is due in large part to the proliferation of enterprise applications and disparate enterprise resources, and the increasing need to integrate these applications into cohesive systems. Over the years Messaging and Message Oriented Middleware (MOM) has provided this integration proprietary manner. Introduction of Java Messaging Service (JMS) as a standard, eliminated many of the disadvantages in proprietary MOM based products. In addition,Message Driven Beans(MDBs) introduced together with Enterprise Java Beans 2.0 have served to get the best out of existing investments in J2EE application servers. Most of the J2EE application servers in modern era are acting as a MOM with a whole lot of value added services to JMS. As a J2EE 1.4 certified application server, Apache Geronimo comes into the party with support of JMS integrating with one of the best breed open source messaging frameworks, ActiveMQ. This article will provide you with a way to use JMS for your enterprise application scenario both as a local and remotely referred environments with Geronimo and ActiveMQ.
The company referred in this sample application sells one specific item in both retail and wholesale markets under different categories. All the placed orders in the application have to be authorized by a company sales employee before delivering goods to the customer. For the wholesale market, the company has placed their agents all over the country. They send their orders as a bunch at once, which is called a consignment. End users place their orders using the company web site while agents send their consignments with a special software installed in their premises. All the consignments must be approved by the company General Manager before it is handed over to a sales employee.
This is a typical application to use JMS as a solution because both consignment and order requests are processed in asynchronous manner.
After reading this article, you should be able to define Message Queues and their Connection Factories in Geronimo/ActiveMQ environment, send and receive messages using different kinds of applications in your Enterprise Application with ease.
This article is organized into following sections.
Geronimo server comes with a JMS server and application components that can access JMS resources like connection factories, topics and queues from it. This JMS server is also known as message broker. The default message broker supported by Geronimo is ActiveMQ, usually does not need to be changed since it is a mature and feature-rich JMS product. This implementation uses inbuilt Derby database for the message persistent features.
ActiveMQ supports a large variety of transports (such as TCP, SSL, UDP, multicast, intra-JVM, and NIO) and client interactions (such as push, pull, and publish/subscribe). In the Geronimo context ActiveMQ supports MDBs, which are EJBs that consume JMS messages. It allows JMS applications to take J2EE specific features from Geronimo and application components such as JSPs, Servlets or EJBs utilizing JMS. Geronimo has implemented this JMS API in an abstract layer to support any JMS provider. It has achieved this feature by supporting J2EE Connector (JCA) specification. The JCA 1.5 specification details the contracts required between the application server and the driver supplied by ActiveMQ (resource adapter). Applications deployed in the Geronimo access ActiveMQ message broker only through this resource adapter(RA).
Order processing application has two defined message queues to receive orders and consignments. Order requests can be generated and sent via the company's web application. When order requests are received to the order queue, a MDB will be triggered. It will carry out the next level of order request processing by saving those requests in to a server repository. Those saved order requests will be processed by a company employee later.
The company's sales agents are using the consignment sender application to send consignment (collection of orders) requests from their locations. First, they will prepare consignment as an XML file, then it will be passed as an application parameter. Consignment sender application will read the content of an XML file (with a consignment request) and send it to the consignment queue. General Manager in the company uses the consignment receiver application to find out the consignment requests. When a consignment request received to the consignment queue, consignment receiver listener application will download those requests to the General Manager's computer. He will then authorize it and hand it over to a sales employee for further processing.
The core of the order placement application will be deployed as an EAR to the application server. Overview of the contents of EAR is given in the following depiction.
The Message-Driven Bean uses the @MessageDriven annotation to replace the declaration of this MDB in the ejb-jar.xml file. By providing the annotation with further information it knows to look for a destination (in this case it happens to be a queue) to process. So this MDB will sit there and process messages passed into the 'OrderQueue.' The end result is that is echoes this message to the screen.
In this application there is a MDB that will listen on OrderQueue. openejb-jar.xml tells Geronimo that there is a MDB which is associated with the jms-resources JMS Resource Group. It links OrderRecvMDB with OrderQueue via CommonConnectionFactory.
geronimo-application.xml and application.xml define the main components of the EAR. Both EJB component and Web archive information are given in these files as usual. This geronimo-application.xml also includes a section for defining a JMS queue and a common queue connection factory to access it. This is used for deploying the geronimo-activemq-ra.rar that is embedded in the ear.
The OrderSenderServlet.java servlet will parse the web form, create a message, and send that message to the OrderQueue via the CommonConnectoryFactory.
|Please note that Geronimo ignores the 'mappedName' configuration attribute for @Resource. Instead, use 'name' when annotating.|
web.xml of the archive has the relevant configurations for the both queue connection factory and the queue, which is essential to refer to resources in a local enviroment.
|Please note that this web application supports Servlet 2.5 specification. Some of the configurations in older versions (2.4) are slightly different than given in the above web.xml.|
geronimo-web.xml is not necessary in this case as the annotations will help resolve the queue or connection factory references.
The tools used for developing and building the order placement application are:
Maven is a popular open source build tool for enterprise Java projects, designed to take much of the hard work out of the build process. Maven uses a declarative approach, where the project structure and contents are described, rather than the task-based approach used in Ant or in traditional make files, for example. This helps enforce company-wide development standards and reduces the time needed to write and maintain build scripts. The declarative, lifecycle-based approach used by Maven 1 is, for many, a radical departure from more traditional build techniques, and Maven 2 goes even further in this regard. Maven 2 can be download from the following URL:
Download the order processing application from the following link:
After decompressing the given file, the jms-mdb-sample directory will be created.
You can checkout the source code of this sample from SVN:
The jms-mdb-sample folder will already contain an ear file ready to be deployed. However, you can still play with the source and build it yourself.
Use a command prompt to navigate into the jms-mdb-sample directory and just give mvn install site command to build. It will overwrite the jms-mdb-sample-ear-2.0-SNAPSHOT.ear under the jms-mdb-sample folder.
Deploying Order processing sample application is pretty much the same as the deployment of JMS resources.
To test the sample web application open a browser and type http://localhost:8080/order. It will forward you in to the Order Management Welcome page. Then user has to fill the necessary information for the order placement and submit it.
After processing an order you will see the message printed to your console.
This article has demonstrated the use of JMS features in Apache Geronimo with the ActiveMQ JMS server. It provides a hypothetical example which extensively used JMS features.
Some of the highlights of this article : -