Solace Queue Publisher Hello World

Solace Queue Publisher Hello World

To start working with Solace you should have something which connect to the server and works properly. In this simple program you will see how to connect Solace messaging server and publish message on the queue:

  • SolaceQueuePublisherHelloWorld.java:
package com.javahonk.solace;

import com.solacesystems.jcsmp.DeliveryMode;
import com.solacesystems.jcsmp.EndpointProperties;
import com.solacesystems.jcsmp.JCSMPException;
import com.solacesystems.jcsmp.JCSMPFactory;
import com.solacesystems.jcsmp.JCSMPProperties;
import com.solacesystems.jcsmp.JCSMPSession;
import com.solacesystems.jcsmp.JCSMPStreamingPublishEventHandler;
import com.solacesystems.jcsmp.Queue;
import com.solacesystems.jcsmp.TextMessage;
import com.solacesystems.jcsmp.XMLMessageProducer;

public class SolaceQueuePublisherHelloWorld {

    public static void main(String args[]) throws JCSMPException, InterruptedException {
    	
    	JCSMPProperties properties = new JCSMPProperties();
        
    	properties.setProperty(JCSMPProperties.HOST, "HOST");
        properties.setProperty(JCSMPProperties.VPN_NAME, "VPN");
        properties.setProperty(JCSMPProperties.USERNAME, "USER_NAME");
        properties.setProperty(JCSMPProperties.PASSWORD, "PASSWORD");        
        
        JCSMPSession session = JCSMPFactory.onlyInstance().createSession(properties);       
    	
        String queueName = "QUEUE_NAME";
       
        EndpointProperties endpointProps = new EndpointProperties();
        endpointProps.setPermission(EndpointProperties.PERMISSION_CONSUME);
        endpointProps.setAccessType(EndpointProperties.ACCESSTYPE_EXCLUSIVE);
        
        Queue queue = JCSMPFactory.onlyInstance().createQueue(queueName);
        
        //This is required otherwise you will get exception if queue is already exists
        session.provision(queue, endpointProps, JCSMPSession.FLAG_IGNORE_ALREADY_EXISTS);

        final XMLMessageProducer prod = session.getMessageProducer(
                new JCSMPStreamingPublishEventHandler() {
                    public void responseReceived(String messageID) {
                        System.out.printf("Producer received response msg ID:",messageID);
                    }
                    public void handleError(String messageID, JCSMPException e, long timestamp) {
						System.out.println("Publisher received error for msg:"
								+ messageID + " TimeStamp: " + timestamp + " Exception details" + e);
                    }
                });

        
        System.out.println("Connected to solace messaging server. Queue name: "+queue.getName());
        
        TextMessage msg = JCSMPFactory.onlyInstance().createMessage(TextMessage.class);
        msg.setDeliveryMode(DeliveryMode.PERSISTENT);
        String text = "Hello Solace world from Java Honk!";
        msg.setText(text);
        
        prod.send(msg, queue);
        System.out.println("Message sent successfully.");
        
        //Take a break
        Thread.sleep(1000);
        session.closeSession();
    }
}

Reference:

Leave a Reply

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