Solace Queue Subscriber HelloWorld

Solace Queue Subscriber HelloWorld

In this program you will see how to connect Solace queue and subscribe to receive data when publisher publish the data. To keep thread running using independent java program I have created one thread and keep it running:

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

import com.solacesystems.jcsmp.BytesXMLMessage;
import com.solacesystems.jcsmp.FlowReceiver;
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.XMLMessageListener;

public class SolaceQueueSubscriberHelloWorld implements XMLMessageListener, JCSMPStreamingPublishEventHandler{

    public static void main(String... args) throws JCSMPException, InterruptedException {
        
    	SolaceQueueSubscriberHelloWorld customSubscriberTest = new SolaceQueueSubscriberHelloWorld();    	
    	customSubscriberTest.run();
    	
    	//Keep thread alive
    	Thread keepAliveThread = new Thread(new Runnable() {

			@Override
			public void run() {
				while (!Thread.interrupted()) {
				}
			}
		});
		keepAliveThread.start();
    }

	private void run() throws JCSMPException {
		
		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);
        session.connect();
        
        Queue queue = JCSMPFactory.onlyInstance().createQueue("QUEUE_NAME");
        FlowReceiver receiver = null;
        
        receiver = session.createFlow(queue, null, this);
        receiver.start();
              
        
        //receiver.close();
        //session.closeSession();
	}

	@Override
	public void handleError(String messageID, JCSMPException cause, long timestamp) {
		cause.printStackTrace();
		
	}

	@Override
	public void responseReceived(String arg0) {
				
	}

	@Override
	public void onException(JCSMPException exception) {
		exception.printStackTrace();
		
	}

	@Override
	public void onReceive(BytesXMLMessage message) {
		System.out.println("Received Message:\n" + message.dump());
        message.ackMessage();
		
	}
}

Reference:

Leave a Reply

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