Search This Blog

Friday, September 28, 2012

Sample HornetQ



Steps to configure HornetQ


Go to the file hornetq-2.2.14.Final\config\stand-alone\non-clustered\hornetq-beans.xml

rename the below localhost to your system ip address

  
     
        
     
      ${jnp.port:1099}
      ${jnp.host:localhost}
      ${jnp.rmiPort:1098}
      ${jnp.host:localhost}
  

  
     
        
     
      ${jnp.port:1099}
      ${jnp.host:192.168.7.9}
      ${jnp.rmiPort:1098}
      ${jnp.host:192.168.7.9}
  


Go to the file D:\Suresh backup\hornetq-2.2.14.Final\config\stand-alone\non-clustered\hornetq-configuration.xml


rename the below localhost to your system ip address


     
         org.hornetq.core.remoting.impl.netty.NettyConnectorFactory
           value="${hornetq.remoting.netty.host:localhost}"/>
           value="${hornetq.remoting.netty.port:5445}"/>
     
     
     
         org.hornetq.core.remoting.impl.netty.NettyConnectorFactory
           value="${hornetq.remoting.netty.host:localhost}"/>
           value="${hornetq.remoting.netty.batch.port:5455}"/>
         
     
  

  
     
         org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory
           value="${hornetq.remoting.netty.host:localhost}"/>
           value="${hornetq.remoting.netty.port:5445}"/>
     
     
     
         org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory
           value="${hornetq.remoting.netty.host:localhost}"/>
           value="${hornetq.remoting.netty.batch.port:5455}"/>
        
        
     
  


Optional
If you  want to change  your  Queue name
Go to the file D:\Suresh backup\hornetq-2.2.14.Final\config\stand-alone\non-clustered\hornetq-jms.xml
 Rename the DLQ
     
  
     
     
  

go to the bin folder and start the run.bat
D:\Suresh backup\hornetq-2.2.14.Final\bin>run.bat


Sample Sender pgm


/**
 *
 */
/**
 *
 */
package com.test;


import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * @author Suresh
 *
 */
public class Sender
{
                 public static void main(String[] args) {
                                 
                                         try {
                               
                                 
                               
                                             /*
                               
                                              * Connecting to JBoss naming service running on local host and on
                               
                                              * default port 1099 the environment that should be created is like the
                                 
                                              * one shown below :
                                               */
                                 
                                             Hashtable env = new Hashtable();
                               
                                             env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                                 
                                             env.put(Context.PROVIDER_URL, "jnp://192.168.7.9:1099");
                                 
                                             env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
                               
                                              
                                 
                                             // Create the initial context
                                 
                                             Context ctx = new InitialContext(env);
                                 
                                 
                                             // Lookup the JMS connection factory from the JBoss 5.1 object store
                               
                                             ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
                                 
                                   
                               
                                             // Lookup a queue from the JBoss 5.1 object store
                                 
                                             Queue queue = (javax.jms.Queue)ctx.lookup("/queue/DLQ");
                               
                                 
                                 
                                             // Create a connection to the JBoss 5.1 Message Service
                               
                                             Connection connection = connectionFactory.createConnection();
                               
                                 
                                 
                                             // Create a session within the connection
                               
                                             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                                 
                                 
                                            // Create a message producer to put messages on the queue
                                           MessageProducer messageProducer = session.createProducer(queue);
                                 
                                 
                                 
                                             //Create and send a message to the queue
                               
                                            // TextMessage textMessage = session.createTextMessage();
                                             TextMessage message=session.createTextMessage ();
                                 
                                           //  textMessage.setText("Hello World 123");
                                            // message.setText (hi);
                                             message.setStringProperty ( "hello","hi");
                                            
                                 
                                            System.out.println("Sending Message: " + message.getText());
                                 
                                          //   messageProducer.send(textMessage);
                                             messageProducer.send(message);
                                 
                                                
                                 
                               
                                             //Close the session and connection resources.
                               
                                             session.close();
                               
                                             connection.close();
                               
                                         } catch (Exception ex) {
                               
                                             ex.printStackTrace();
                               
                                         }
                               
                                     }


}

Sample Receiver Pgm

/**
 *
 */
package com.test;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * @author Suresh
 *
 */
public class Receiver
{

                 public static void main(String[] args) {
                                 
         try {

 

             /*

              * Connecting to JBoss naming service running on local host and on

              * default port 1099 the environment that should be created is like the

              * one shown below :
               */

             Hashtable env = new Hashtable();

             env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

             env.put(Context.PROVIDER_URL, "jnp://192.168.7.9:1099");

             env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

             

             // Create the initial context

             Context ctx = new InitialContext(env);


             // Lookup the JMS connection factory from the JBoss 5.1 object store

             ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");

   

             // Lookup a queue from the JBoss 5.1 object store

             Queue queue = (javax.jms.Queue)ctx.lookup("/queue/DLQ");

 

             // Create a connection to the JBoss 5.1 Message Service
             for(int i=0;i<2 i="i" span="span">
             {
             Connection connection = connectionFactory.createConnection();

 

             // Create a session within the connection

             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                                         
 

             // Create a message consumer

             MessageConsumer messageConsumer = session.createConsumer(queue);

 

             // Start the Connection created

             connection.start();

 

             // Receive a message from the queue.
           Message msg = messageConsumer.receive ( );

 

             // Retrieve the contents of the message.
        
             if (msg instanceof TextMessage) {

                 TextMessage txtMsg = (TextMessage) msg;

              //   System.out.println("Read Message: " + txtMsg.getText());
              System.out.println("Read Message: " + txtMsg.getStringProperty ( "hello" ));
              txtMsg.clearBody();

             }
            
             session.close();

             connection.close();

         }

                      //Close the session and connection resources.
          
         } catch (Exception ex) {

             ex.printStackTrace();

         }

     }
}


Links to Refer.




No comments:

Hit Counter


View My Stats