Showing posts with label HornetQ. Show all posts
Showing posts with label HornetQ. Show all posts

Saturday, June 25, 2016

[WSO2][ESB Cluster] HornetQ host configuration when connecting to a cluster

When we are going to use HornetQ with  cluster environment (Ex: ESB Cluster), have to configure HorentQ hosts. If not HornetQ will not identify exact configuration.

In HornetQ run.sh script ($HorenetQ_Home/bin/) update as follows:

# Use the following line to run with different ports
#export CLUSTER_PROPS="-Djnp.port=1099 -Djnp.rmiPort=1098 -Djnp.host=localhost -Dhornetq.remoting.netty.host=localhost -Dhornetq.remoting.netty.port=5445"

Uncomment above line and update relevant parameters.

# Use the following line to run with different ports
export CLUSTER_PROPS="-Djnp.port=1099 -Djnp.rmiPort=1098 -Djnp.host=10.100.7.120 -Dhornetq.remoting.netty.host=localhost -Dhornetq.remoting.netty.port=5445"

** Update -Djnp.port and -Djnp.host with relevant values. -Djnp.host should be the IP/host_name of an instance which HornetQ installed.


Tuesday, May 24, 2016

HornetQ JMS Subscriber

Hi all,

HornetQ is a JMS broker which is support JMS 2.0 specification. Here you can find a JMS subscriber written for queue within HornetQ.

 import javax.jms.*;  
 import javax.naming.Context;  
 import javax.naming.InitialContext;  
 import java.util.Properties;  
 /**  
  * This is a hornetq subscriber java class for a queue  
  */  
 public class HornetQSubscriber {  
   private static final String DEFAULT_CONNECTION_FACTORY = "QueueConnectionFactory";  //TopicConnectionFactory when using for topics
   private static final String DEFAULT_DESTINATION = "queue/mySampleQueue";  //Can change when it is a topic
   private static final String INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";  
   private static final String PROVIDER_URL = "jnp://localhost:1099";  
   public static void main(final String[] args) {  
     try {  
       runExample();  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
   }  
   public static void runExample() throws Exception {  
     Connection connection = null;  
     Context initialContext = null;  
     try {  
       // /Step 1. Create an initial context to perform the JNDI lookup.  
       final Properties env = new Properties();  
       env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);  
       env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));  
       initialContext = new InitialContext(env);  
       // Step 2. perform a lookup on the Queue  
       Queue queue = (Queue) initialContext.lookup(DEFAULT_DESTINATION);  
       // Step 3. perform a lookup on the Connection Factory  
       ConnectionFactory cf =  
           (ConnectionFactory) initialContext.lookup(DEFAULT_CONNECTION_FACTORY);  
       // Step 4. Create a JMS Connection  
       connection = cf.createConnection();  
       // Step 5. Create a JMS Session  
       Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);  
       // Step 6. Create a JMS Message Consumer  
       MessageConsumer messageConsumer =  
           session.createConsumer(queue);  
       // Step 7. Start the Connection  
       connection.start();  
       System.out.println("Message consumer started on Queue: " + DEFAULT_DESTINATION +  
           "\n");  
       // Step 8. Receive the message  
       int message_count=0;  
       while (messageConsumer.receive()!=null) {  
         message_count++;  
         System.out.println("Received a message ------------"+message_count);  
       }  
     } finally {  
       // Step 9. Close JMS resources  
       if (connection != null) {  
         connection.close();  
       }  
       // Also the initialContext  
       if (initialContext != null) {  
         initialContext.close();  
       }  
     }  
   }  
 }  

* Note : you can modify class when you are going to publish JMS messages to topics. Relevant places are highlighted within the class.

Saturday, March 26, 2016

Adding JMS Message Store with HornetQ Broker

1) Download HornetQ Broker : http://hornetq.jboss.org/downloads.html

2) Configure HornetQ:

  • Go to HornetQ_Home/config/stand-alone/non-clustered/
  • You will find hornetq_jms.xml
  • Change it as follows:

Note: This will create queue connection factory, topic connection factory and a queue. Queue will be used as storing place for JMS message store.

 <connection-factory name="QueueConnectionFactory">  
    <xa>false</xa>  
    <connectors>  
      <connector-ref connector-name="netty"/>  
    </connectors>  
    <entries>  
      <entry name="/QueueConnectionFactory"/>  
    </entries>  
 </connection-factory>  
 <connection-factory name="TopicConnectionFactory">  
    <xa>false</xa>  
    <connectors>  
      <connector-ref connector-name="netty"/>  
    </connectors>  
    <entries>  
      <entry name="/TopicConnectionFactory"/>  
    </entries>  
 </connection-factory>  
 <queue name="wso2">  
    <entry name="/queue/mySampleQueue"/>  
 </queue>  


3) Configure ESB: 

  • Go to ESB_Home/repository/components/lib/
  • Add hornetq-all.jar: https://docs.google.com/a/wso2.com/file/d/0B_g60lAUxtLhd2RmZXJWUVkwWXM/edit
  • Go to ESB_Home/repository/conf/axis2/
  • You will find axis2.xml
  • Change it as follows:

Uncomment follow line:

 <transportSender name="jms" class="org.apache.axis2.transport.jms.JMSSender"/>  

Add these lines:

 <transportReceiver name="jms"  
  class="org.apache.axis2.transport.jms.JMSListener">  
  <parameter name="myTopicConnectionFactory" locked="false">  
  <parameter name="java.naming.factory.initial" locked="false">org.jnp.interfaces.NamingContextFactory</parameter>  
  <parameter name="java.naming.factory.url.pkgs" locked="false">org.jboss.naming:org.jnp.interfaces</parameter>  
  <parameter name="java.naming.provider.url" locked="false">jnp://localhost:1099</parameter>  
  <parameter name="transport.jms.ConnectionFactoryJNDIName"  
   locked="false">TopicConnectionFactory</parameter>  
  <parameter name="transport.jms.ConnectionFactoryType"  
   locked="false">topic</parameter>  
  </parameter>  
  <parameter name="myQueueConnectionFactory" locked="false">  
  <parameter name="java.naming.factory.initial" locked="false">org.jnp.interfaces.NamingContextFactory</parameter>  
  <parameter name="java.naming.factory.url.pkgs" locked="false">org.jboss.naming:org.jnp.interfaces</parameter>  
  <parameter name="java.naming.provider.url" locked="false">jnp://localhost:1099</parameter>  
  <parameter name="transport.jms.ConnectionFactoryJNDIName"  
   locked="false">QueueConnectionFactory</parameter>  
  <parameter name="transport.jms.ConnectionFactoryType"  
   locked="false">queue</parameter>  
  </parameter>  
  <parameter name="default" locked="false">  
  <parameter name="java.naming.factory.initial" locked="false">org.jnp.interfaces.NamingContextFactory</parameter>  
  <parameter name="java.naming.factory.url.pkgs" locked="false">org.jboss.naming:org.jnp.interfaces</parameter>  
  <parameter name="java.naming.provider.url" locked="false">jnp://localhost:1099</parameter>  
  <parameter name="transport.jms.ConnectionFactoryJNDIName"  
   locked="false">QueueConnectionFactory</parameter>  
  <parameter name="transport.jms.ConnectionFactoryType"  
   locked="false">queue</parameter>  
  </parameter>  
 </transportReceiver>  

  • You have to change JNDI properties of ESB
  • Go to ESB_HOME/repository/conf
  • And you will find jndi.properties file. Open and edit it. Add following line:
 connectionfactory.QueueConnectionFactory = jnp://localhost:1099  

Note: if you need you can also add queues and topics here.

4) Start HornetQ (HornetQ_home/bin)

 sudo ./run.sh  

5) Start ESB server (ESB_Home/bin)

 sh wso2server.sh  

6) Go to management console (https://<IP>:9443/carbon/admin/login.jsp)

7) Go to message stores (Home > Manage > Service Bus > Message Stores)

8) Add JMS message store (Add message stores > Add JMS message store)

9) Give relevant details to add message store

  • Name: As you want
  • Initial Context Factory: org.jnp.interfaces.NamingContextFactory
  • Provider URL: jnp://localhost:1099
  • JNDI Queue Name: queue/mySampleQueue
    Add other details accordingly. 

Note: Remember to remove user name and password. If you are using them, you have to configure HornetQ with that username and password.

10) Click "Save". Then your JMS message store will be created.

Yea, You may feel like "Tadaaa". But for me, it took long time to figure this out ;) 

Happy coding :)