Solace JMS Queue Subscriber Hello World

Solace JMS Queue Subscriber Hello World

If have just started with Solace and want to subscriber and consume message publish by the publisher on the newly created Solace queue using JMS API then please use below sample Hello World program.

Solace JMS version: sol-jms-7.1.2.230

  • To run this program you will need below jar:

Solace JMS Queue Publisher Hello World

You will also need below information:

  • SOLACE_HOST=tcp://url:portnumber
  • SOLACE_VPN=VPN NAME
  • SOLACE_CONNECTION_FACTORY=JMS connection factory
  • SOLACE_OUTPUT_QUEUE_NAME=queue name
  • SOLACE_USERNAME_PUB=user name
  • SOLACE_PASSWORD_PUB=password

Sample program:

JavaHonkSolaceQueueSubscriberHelloWorld.java:

package com.javahonk.jms.solace;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.solacesystems.jms.SolJmsUtility;
import com.solacesystems.jms.SupportedProperty;
import com.solacesystems.jms.message.SolBytesMessage;
import com.solacesystems.jms.message.SolTextMessage;

public class JavaHonkSolaceQueueSubscriberHelloWorld {
    
    public static void main(String args[]) throws JMSException, NamingException {
        
    	System.out.println("SolJMSHelloWorldQueueSub initializing...");

        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(InitialContext.INITIAL_CONTEXT_FACTORY, "com.solacesystems.jndi.SolJNDIInitialContextFactory");
        env.put(InitialContext.PROVIDER_URL, "tcp://URL:PORT NUMBER");
        env.put(SupportedProperty.SOLACE_JMS_VPN, "VPN NAME");
        env.put(Context.SECURITY_PRINCIPAL, "USER NAME");
        env.put(Context.SECURITY_CREDENTIALS, "PASSWORD");

        InitialContext initialContext = new InitialContext(env);
    	QueueConnectionFactory cf = (QueueConnectionFactory)initialContext.lookup("JMS CONNECTION FACTORY NAME");
    	QueueConnection connection = cf.createQueueConnection();

        Session session = connection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);

        Queue queue = (Queue)initialContext.lookup("QUEUE NAME");

        MessageConsumer consumer = session.createConsumer(queue);
        
        connection.start();
        
        System.out.println("Now waiting for a message. (press Ctrl+C) to terminate anytime");
        
        while (true) {
            
        	Message testMessage = consumer.receive();
            System.out.println("Message received:"+((SolTextMessage)testMessage).getText());
            
            if (testMessage instanceof SolBytesMessage) {
            	
            	System.out.println("Message received:"+((SolBytesMessage)testMessage).getMessage());
            	System.out.println("Message size: {} kb"+SolJmsUtility.getMessageSize(testMessage));
            	testMessage.acknowledge();	                 
            }
            
            if (testMessage instanceof SolTextMessage) {
            	
            	System.out.println("Message received:"+((SolTextMessage)testMessage).getText());
            	System.out.println("Message size: {} kb"+SolJmsUtility.getMessageSize(testMessage));
            	testMessage.acknowledge();
            }
        }
    }
}

Reference:

Leave a Reply

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