Please go through my previous posts to understand some JMS Concepts and JMS 1.1 API Basics. In this post, we are going to discuss on “How to develop JMS 1.1 Producer and Consumer Example With Eclipse IDE and JBoss 6.0 Application Server”.
Post Brief TOC
- Introduction
- Steps to Configure JBoss 6.0 AS In Eclipse IDE
- Create Eclipse EJB Project And Configure Queue
- Develop The Producer and Consumer Programs
- Run and Observe The results
Introduction
In my previous example at “JMS 1.1 With Embedded JBoss HornetQ Server Example”, we have developed JMS 1.1 Producer and Consumer in a single program and also used Embedded Server.
Now, we are going to develop similar kind of example, but with two separate programs: one for Producer and another for Consumer. We use JBoss 6.0 Application Server as JMS Server.
JBoss AS (Application Server) is an open source application server from Red Hot for developing and deploying Enterprise Java applications, Web applications and services, and portals. By default, JBoss AS uses HornetQ as JMS Provider. HornetQ is an open source JMS Provider for building multi-protocol, embeddable, very high performance, clustered and asynchronous messaging systems.
In this post, we’re going to develop JMS 1.1 Application with JBoss AS V.6.x in Eclipse IDE.
Steps to Configure JBoss 6.0 AS In Eclipse IDE
-
- Download JBoss AS from https://www.jboss.org/jbossas/downloads/ as zip file.
- Extract jboss-6.0.0.Final.zip file into local file system.
-
- Create JBoss AS in Eclipse IDE.
Click on “Create a new server” link to open “New Server” Window
Select “JBoss AS 6.x” and Click on “Next” button. Select JBoss 6.x Path from your local file system. Select “Default” Configuration and click on “Finish” Button
Now we can observe newly created server in Eclipse IDE
-
- Start JBoss 6.x AS in Eclipse IDE.
Now we can observe JBoss 6.x AS is up and running
Create Eclipse EJB Project And Configure Queue
-
- To Configure HornetQ Destinations(Queues or Topics) with JBoss 6 AS, create an EJB Project in Eclipse IDE
Provide Project name “HornetQConfigApp” and select Target runtime “JBoss 6.x Runtime”
Click on Finish Button. Now Project structure looks like
Create “hornetq-jms.xml” file at “ejbModuleMETAINF” directory to add new Queue
hornetq-jms.xml – It is used to configure JMS Destinations (Queues and Topics) for HornetQ JMS Provider
<configuration xmlns="urn:hornetq"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd">
<queuename="JDQueue">
<entryname="queue/JDQueue"/>
</queue>
</configuration>
- Deploy this Queue configuration into JBoss 6.x AS
Right click on Server and select “Add and Remove” option
Select our application and click on “Add>” button then “Finish”
Now our required Queue “jms/JDQueue” is created in JBoss 6 AS – HornetQ Message Broker successfully.
Develop The Producer and Consumer Programs
import javax.jms.*;
import javax.naming.*;
public class JBossJMSPublisher {
public static void main(String[] args) throws Exception{
Context context = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("/ConnectionFactory");
Destination queue = (Destination)context.lookup("queue/JDQueue");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
TextMessage sntMessage =session.createTextMessage("JD Sample JMS Queue Message");
producer.send(sntMessage);
System.out.println("Message Sent successfully.");
connection.close();
}
}
Create JMS Consumer Program
import javax.jms.*;
import javax.naming.*;
public class JBossJMSConsumer {
public static void main(String[] args) throws Exception{
Context context = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("/ConnectionFactory");
Destination queue = (Destination)context.lookup("queue/JDQueue");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
TextMessage rcvMessage = (TextMessage) consumer.receive();
System.out.println("Received Msg = " + rcvMessage.getText());
connection.close();
}
}
- Define JBoss AS JNDI in jndi.properties at “src” folder
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
- Add required jars to Project Build path
Use “Add Library” button to add JBoss 6.x Library
- Final project looks like
Run and Observe The results
-
- Now test JMS application
Run JMS Producer program to send messages to JBoss 6.x AS HornetQ Queue
Run JMS Consumer program and observe the message
That’s it all about developing Simple JMS API 1.1 Producer and Consumer application with JBoss 6.0 Server. We will discuss JMS API 2.0 Producer and Consumer Example in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions.