JMS with HornetQ – JBoss 7 EAP 1

Lets make JMS app with HornetsQ as MOM. I make use of JBoss 7 EAP and exposed the topic so that i can remotely lookup that into my main program.

First i added jms topic inside my standalone-full.xml file.

 <jms-topic name="ClusterEventTopic">
   <entry name="java:jboss/exported/queue/ClusterEventTopic"/>
</jms-topic>

Below is the Message Producer class:


import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicProducer {

	 public static void main(String[] args) throws NamingException, JMSException {
	        Context context = TopicConsumer.getInitialContext();
	        try{
	        System.out.println("Entering into the main method!!!");

	        ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
	        Destination destination = (Destination) context.lookup("queue/ClusterEventTopic");
	        Connection connection = connectionFactory.createConnection("admin2", "P@ssword2");
	        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	        MessageProducer producer = session.createProducer(destination);
	        connection.start();
	        producer.send(session.createTextMessage("Hello World !"));
	        System.out.println("Exiting from the main method!!!");
	       }finally{
	            //context.close();
	        }

	    }

	    public void onMessage(Message arg0) {
	        try {
				System.out.println("Incoming Message : " + ((TextMessage)arg0).getText());
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }


	    public static Context getInitialContext() throws NamingException{
	        Properties props = new Properties();

	        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
	        props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS", "JBOSS-LOCAL-USER");
	        props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
	        props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false");
	        props.put(Context.PROVIDER_URL,"remote://localhost:5047");


	        props.put("jboss.naming.client.ejb.context", true);
	        // username
	        props.put(Context.SECURITY_PRINCIPAL, "admin2");
	        // password
	        props.put(Context.SECURITY_CREDENTIALS, "P@ssword2");

	        return new InitialContext(props);

	    }
}

Below is the Message Consumer class:


import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicConsumer implements MessageListener{


    public static void main(String[] args) throws NamingException, JMSException, InterruptedException {
        Context context = TopicConsumer.getInitialContext();
        try{
        System.out.println("Entering into the main method!!!");

        ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
        Destination destination = (Destination) context.lookup("queue/ClusterEventTopic");
        Connection connection = connectionFactory.createConnection("admin2", "P@ssword2");
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        session.createConsumer(destination).setMessageListener(new TopicConsumer());
        connection.start();
        System.out.println("Exiting from the main method!!!");
        Thread.sleep(Integer.MAX_VALUE);
        }finally{
            //context.close();
        }

    }

    public void onMessage(Message arg0) {
        try {
			System.out.println("Incoming Message : " + ((TextMessage)arg0).getText());
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }


    public static Context getInitialContext() throws NamingException{
        Properties props = new Properties();

        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS", "JBOSS-LOCAL-USER");
        props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
        props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false");
        props.put(Context.PROVIDER_URL,"remote://localhost:5047");


        props.put("jboss.naming.client.ejb.context", true);
        // username
        props.put(Context.SECURITY_PRINCIPAL, "admin2");
        // password
        props.put(Context.SECURITY_CREDENTIALS, "P@ssword2");

        return new InitialContext(props);

    }
}

One comment on “JMS with HornetQ – JBoss 7 EAP

  1. Reply Biofinite Instant Wrinkle Reducer Aug 4,2015 1:37 pm

    Having read this I believed it was very informative.
    I appreciate you spending some time and effort to put this content together.

    I once again find myself spending a lot of time both reading and leaving comments.
    But so what, it was still worth it!

Leave a Reply