JMS 1.1 Producer and Consumer Example With Eclipse IDE, EJB Project and JBoss 6.0 AS

Filed Under: JMS

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 http://www.jboss.org/jbossas/downloads/ as zip file.
  • Extract jboss-6.0.0.Final.zip file into local file system.
  • jboss6.0.0.final-home

  • Create JBoss AS in Eclipse IDE.
  • Click on “Create a new server” link to open “New Server” Window

    jboss6.0.0.final-create-server1

    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

    jboss6.0.0.final-create-server2

    Now we can observe newly created server in Eclipse IDE

    jboss6.0.0.final-create-server3

  • Start JBoss 6.x AS in Eclipse IDE.
  • jboss6.0.0.final-startserver1

    Now we can observe JBoss 6.x AS is up and running

    jboss6.0.0.final-startserver2

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
  • jboss6.0.0.final-ejbproject1

    Provide Project name “HornetQConfigApp” and select Target runtime “JBoss 6.x Runtime”

    jboss6.0.0.final-ejbproject2

    Click on Finish Button. Now Project structure looks like

    jboss6.0.0.final-ejbproject3

    Create “hornetq-jms.xml” file at “ejbModule\METAINF” directory to add new Queue

    jboss6.0.0.final-ejbproject4

    hornetq-jms.xml – It is used to configure JMS Destinations (Queues and Topics) for HornetQ JMS Provider

    
    <configuration xmlns="urn:hornetq" 
    		xmlns:xsi="http://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

    jboss6.0.0.final-deployqueue1

    jboss6.0.0.final-deployqueue2

    Select our application and click on “Add>” button then “Finish”

    jboss6.0.0.final-deployqueue3

    Now our required Queue “jms/JDQueue” is created in JBoss 6 AS – HornetQ Message Broker successfully.

Develop The Producer and Consumer Programs

  • Create new Java Project to develop JMS Publisher and Consumer applications
  • Create JBossJMSPublisher program

    
    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

    jboss6.0.0.final-serverlib1

  • Final project looks like
  • jboss6.0.0.final-complete-project

Run and Observe The results

  • Now test JMS application
  • Run JMS Producer program to send messages to JBoss 6.x AS HornetQ Queue

    jboss6.0.0.final-producer-output

    Run JMS Consumer program and observe the message

    jboss6.0.0.final-consumer-output

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.

Comments

  1. Niraj says:

    Hi

    i want to know how will send a message from my app to Gmail using jms. kindly help me its urgent

  2. uday says:

    In XML getting error:
    Element type “configurationxmlns” must be followed by either attribute specifications, “>” or “/>”.

    How to fix it?

    1. Rambabu says:

      Hi Uday,

      Sorry there was a typo error. I fixed it. This “configurationxmlns” is not a single word and a space was missing between two words.
      It should be “configuration xmlns”
      I hope it will fix your problem.

      Ram

  3. Andrew Frolov says:

    There are whitespaces missing in this config.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages