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.

1 comment:

  1. How to deposit online casino games - Kadang Pintar
    Kadyo, a member of Kadyo Dari 온카지노 먹튀 Bintaran and Kadyo Dari Bintaran, is one of the most popular casino sites and they have also established some amazing games.

    ReplyDelete