Solace JMS Topic Publisher Hello World

Solace JMS Topic Publisher Hello World

If have just started with Solace and want to publish message on the newly created Solace Topic 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
  • JMS-Direct Connection Factory(direct-no Ack back) – Please note: For topic you need JMS-Direct connection factory not JMS-Persistent Connection Factory
  • SOLACE_OUTPUT_QUEUE_NAME=queue name
  • SOLACE_USERNAME_PUB=user name
  • SOLACE_PASSWORD_PUB=password

Sample program:

JavaHonkSolaceTopicPublisherHelloWorld.java:

package com.javahonk.jms.solace;

import java.util.Hashtable;

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

import com.solacesystems.jms.SupportedProperty;

public class JavaHonkSolaceTopicPublisherHelloWorld {
	
	public static void main(String args[]) throws JMSException, NamingException {
    	
		System.out.println("SolJMSHelloWorldPub 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);
        ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("JMS-Direct Connection Factory(direct-no Ack back) name");    	
    	Connection connection = cf.createConnection();

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

        Destination destination = (Destination)initialContext.lookup("Topic name");

        MessageProducer producer = session.createProducer(destination);

        TextMessage testMessage = session.createTextMessage("Java Honk Solace Hello world!");
        
        System.out.printf("Connected. About to send message '%s' to topic '%s'...%n", testMessage.getText(), destination.toString());

        producer.send(testMessage);
        System.out.println("Message sent. Exiting.");

        connection.close();

        initialContext.close();
    }    
}

Reference:

Leave a Reply

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