This application is a simple JMS application where in a user sends information to the administrator for update.
To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.
- Sun JDK 5.0+ (J2SE 1.5)
- Eclipse 126.96.36.199 (Eclipse Classic package of Europa distribution), which is platform specific
- Web Tools Platform (WTP) 2.0.1
- Data Tools Platform (DTP) 1.5.1
- Eclipse Modeling Framework (EMF) 2.3.1
- Graphical Editing Framework (GEF) 3.3.1
Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:
The application development will take you through the following
Creating a Dynamic Web Project
- Launch Eclipse. Select File->New->Project.
- Select Web->Dynamic Web Project. Select Next.
- Name the project as WebMDB. Select Next.
- Next window displays the various project facets. Keep the default values and select Next.
- Keep the default values for the next window. Select Next->Finish.
- Right click on WebMDB project and create a new servlet.
- Name the servlet as UserServlet and package as webmdb. This is the producer in the application.
- Select Next and later Finish.
- Add the following code to UserServlet.java
- Let us walkthrough the code
- @Resource(name="DefaultActiveMQConnectionFactory")- This is a resource injection wherein connection factory has been injected.
- @Resource(name="SendReceiveQueue")- a queue has been injected. Resource injection is useful in the sense that we need not include the entries in a deployment descriptor.
- Servlets follow a request response model wherein a request is send to servlet and a response is generated. The function protected void doGet(....,....) follows a request response model.
- PrintWriter out = response.getWriter()- This statement returns a PrintWriter object which is used to send HTML content to client page.
- connection = connectionFactory.createConnection()- Creates a connection to DefaultActiveMQConnectionFactory
- Session session = connection.createSession(..,..)- A session is a context for producing and consuming messages. Use this statement we create a new session.
- MessageProducer producer = session.createProducer(queue)- A client uses message producer to send messages to a destination. Queue name is passed to createProducer method provided by session object.
- TextMessage message = session.createTextMessage()- A TextMessage is used to send a message of java.lang.String type.
- Right Click on WebContent and create a jsp.
- Name the jsp as index.jsp. Select Next.
- Select Finish.
Add the following code to index.jsp This will create the producer and user interface required by the application.
Creating Connection Factory and Destination
In simple terms a Connection Factory is an object which is used by a client to connect to the Service provider. In our case we will be using Active MQ as the provider. Destination is an object which is used by client to provide the target to messages produced and source of the messages to be consumed. In our case the target is going to be a queue. We will use the default connection factory and queue which is already available with Apache Geronimo.
Creating a EJB Project
- Right click under project explorer and Select New-> Project-> EJB Project.
- Name the project as MessageDrivenBean and Select Next.
- On the next screen keep defaults and select Next.
- On the next screen uncheck Generate Deployment Descriptor and select Next.
- Next screen suggests to configure Geronimo Deployment Plan. For current tutorial we are keeping the default values.
This creates the skeleton of the EJB project. Next step is to add a pojo to our project and annotate it.
- Right click on EJB project and select New->class.
- Name the class as AdminMDB and package as mdb. Select Finish.
- Add the following code to AdminMDB.java.
- @MessageDriven- @MessageDriven annotation identifies this object as an MDB and specifies the MDB configuration
- @ActivationConfigProperty- This annotation provides messaging configuration which is a name value pair.
- propertyName="destinationType"- This suggests that MDB is listening to a queue or a topic. In our case propertyValue="javax.jms.Queue" suggesting MDB is listening to queue.
- propertyName="destination"- This suggests that our MDB is listening to a destination with a JNDI name SendReceiveQueue which is suggested by propertyValue="SendReceiveQueue".
- public class AdminMDB implements MessageListener- It is used by container to register MDB with message provider.
- public void onMessage(Message message)- This method processes incoming messages.
Modifying Deployment plan
- In WebMDB project select WebContent/WEB-INF/geronimo-web.xml and modify the content with the one shown below.
- <dependency>- Defines the dependency of the application on ActiveMQ and MessageDrivenBean
- <ns3:resource-ref>- This tag is basically used to define connections that can be JDBC Connection, Java Mail connection or JMS Connection Factory. In our case we are using it for JMS Connection Factory.
- In MessageDrivenBean project select META-INF/openejb-jar.xml and modify it as follows
- <enterprise-beans>- This tag suggests that the usage of EJB's in our application.
- <message-driven>- This tag suggest the usage of MDB's in our application.
- <ejb-name>- Name of the ejb.
- <nam:resource-link>- Link for the resource adapter.
To learn more on MDB deployment plans refer Creating deployment plans section.
Deploy and Run
- Export MessageDrivenBean.jar and WebMDB.war from eclipse as shown in the figure below
- Launch Administrative console and deploy the EJB and Web Application using Deploy New.
- Once done launch the web application with http://localhost:8080/WebMDB/.\\
- Fill the form as suggested in the figure and Select Submit.
- Once done check out the command prompt. Message Driven Bean has triggered and it displays the message currently on the queue.