C:\Users\U439665\Downloads\2016-09-01 23_01_08-Publish_Subscribe (JMS) - Solace Developer Portal.png

Send Read BytesMessage Solace Java Example

You could use Solace system to send receive message using two either JMS OR Solace API but Send and reading BytesMessage are same in both the case. In this example you will see how to publish BytesMessage on Solace queue/topic and process it while receive by Subscriber queue/topic:

Send BytesMessage:

public void sendBytesMessage(String message) {
		
		try {
			
			logger.info("Sending Bytesmessage : {}", message);
			
			BytesMessage testMessage = session.createBytesMessage();
			
			testMessage.writeBytes(message.getBytes());
			testMessage.setJMSType("Java Honk type");
			
			producer.send(testMessage);
			
			logger.info("Bytesmessage sent successfully : {}", message);
			
		} catch (Exception e) {
			logger.error("Error sending message to output queue name:{} --> details exceptions: --> ", SOLACE_OUTPUT_QUEUE_NAME, e);
		}
}

Read BytesMessage:

public void onMessage(Message testMessage) {

		try {
			
			if (testMessage instanceof SolBytesMessage) {

				BytesMessage bytesXMLMessage = ((BytesMessage) testMessage);
				byte[] b = new byte[(int) bytesXMLMessage.getBodyLength()];
				bytesXMLMessage.readBytes(b);
				
				//Print Message received as String
				logger.info("Message received:"+new String(b));
				
				//Get JMS type of message
				logger.info(bytesXMLMessage.getJMSType());
				
				//Dump all message info
				logger.info("Message: "+SolJmsUtility.dumpMessage(testMessage));	
				testMessage.acknowledge();

			}
			
		} catch (JMSException e) {
			e.printStackTrace();
		}

}

If your reference below is Solace publisher complete class:

JavaHonkQueuePublisher.java:

package com.javahonk.jms.solace;

import java.util.Hashtable;

import javax.annotation.PostConstruct;
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

import com.solacesystems.jms.SupportedProperty;

public class JavaHonkQueuePublisher {

	@Value("${SOLACE_INITIAL_CONTEXT_FACTORY}")
	private String SOLACE_INITIAL_CONTEXT_FACTORY;
	
	@Value("${SOLACE_HOST}")
	private String SOLACE_HOST;
	
	@Value("${SOLACE_VPN}")
	private String SOLACE_VPN;
	
	@Value("${SOLACE_CONNECTION_FACTORY}")
	private String SOLACE_CONNECTION_FACTORY;
	
	@Value("${SOLACE_OUTPUT_QUEUE_NAME}")
	private String SOLACE_OUTPUT_QUEUE_NAME;
	
	@Value("${SOLACE_USERNAME_QUEUE_PUB}")
	private String SOLACE_USERNAME_QUEUE_PUB;
	
	@Value("${SOLACE_PASSWORD_QUEUE_PUB}")
	private String SOLACE_PASSWORD_QUEUE_PUB;
	
	private Session session;
	
	private static MessageProducer producer;
	
	private static final Logger logger = LogManager.getLogger(JavaHonkQueuePublisher.class);
	
	@PostConstruct
	public void init() throws JMSException, NamingException{
		
		System.out.println("JavaHonk Solace publisher initializing...");
    	
    	Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(InitialContext.INITIAL_CONTEXT_FACTORY, SOLACE_INITIAL_CONTEXT_FACTORY);
        env.put(InitialContext.PROVIDER_URL, SOLACE_HOST);
        env.put(SupportedProperty.SOLACE_JMS_VPN, SOLACE_VPN);
        env.put(Context.SECURITY_PRINCIPAL, SOLACE_USERNAME_QUEUE_PUB);
        env.put(Context.SECURITY_CREDENTIALS, SOLACE_PASSWORD_QUEUE_PUB);

        InitialContext initialContext = new InitialContext(env);
    	QueueConnectionFactory cf = (QueueConnectionFactory)initialContext.lookup(SOLACE_CONNECTION_FACTORY);
    	QueueConnection connection = cf.createQueueConnection();

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

        Destination destination = (Destination)initialContext.lookup(SOLACE_OUTPUT_QUEUE_NAME);

        producer = session.createProducer(destination);

        connection.start();  
        
        logger.info("Solace connection made successfully");
	}
    
	
	public void sendTextMessage(String message) {
		
		try {
			
			logger.info("Sending message : {}", message);
			
			TextMessage testMessage = session.createTextMessage(message);
			producer.send(testMessage);
			
			logger.info("Message sent successfully : {}", message);
			
		} catch (Exception e) {
			logger.error("Error sending message to output queue name:{} --> details exceptions: --> ", SOLACE_OUTPUT_QUEUE_NAME, e);
		}
	}
	
	public void sendBytesMessage(String message) {
		
		try {
			
			logger.info("Sending Bytesmessage : {}", message);
			
			BytesMessage testMessage = session.createBytesMessage();
			
			testMessage.writeBytes(message.getBytes());
			testMessage.setJMSType("Java Honk type");
			
			producer.send(testMessage);
			
			logger.info("Bytesmessage sent successfully : {}", message);
			
		} catch (Exception e) {
			logger.error("Error sending message to output queue name:{} --> details exceptions: --> ", SOLACE_OUTPUT_QUEUE_NAME, e);
		}
	}

}

Solace Subscriber complete class: JavaHonkQueueSubscriber.java:

package com.javahonk.jms.solace;

import java.util.Hashtable;

import javax.annotation.PostConstruct;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

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

public class JavaHonkQueueSubscriber implements MessageListener {
	
	@Value("${SOLACE_INITIAL_CONTEXT_FACTORY}")
	private String SOLACE_INITIAL_CONTEXT_FACTORY;
	
	@Value("${SOLACE_HOST}")
	private String SOLACE_HOST;
	
	@Value("${SOLACE_VPN}")
	private String SOLACE_VPN;
	
	@Value("${SOLACE_CONNECTION_FACTORY}")
	private String SOLACE_CONNECTION_FACTORY;
	
	@Value("${SOLACE_OUTPUT_QUEUE_NAME}")
	private String SOLACE_OUTPUT_QUEUE_NAME;
	
	@Value("${SOLACE_USERNAME_QUEUE_SUB}")
	private String SOLACE_USERNAME_QUEUE_SUB;
	
	@Value("${SOLACE_PASSWORD_QUEUE_SUB}")
	private String SOLACE_PASSWORD_QUEUE_SUB;
	
	private static final Logger logger = LogManager.getLogger(JavaHonkQueueSubscriber.class);
	
	@PostConstruct
	public void init() throws JMSException, NamingException{
		
		System.out.println("JavaHonk Solace Subscriber initializing...");
    	
    	Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(InitialContext.INITIAL_CONTEXT_FACTORY, SOLACE_INITIAL_CONTEXT_FACTORY);
        env.put(InitialContext.PROVIDER_URL, SOLACE_HOST);
        env.put(SupportedProperty.SOLACE_JMS_VPN, SOLACE_VPN);
        env.put(Context.SECURITY_PRINCIPAL, SOLACE_USERNAME_QUEUE_SUB);
        env.put(Context.SECURITY_CREDENTIALS, SOLACE_PASSWORD_QUEUE_SUB);

        InitialContext initialContext = new InitialContext(env);
    	QueueConnectionFactory cf = (QueueConnectionFactory)initialContext.lookup(SOLACE_CONNECTION_FACTORY);
    	QueueConnection connection = cf.createQueueConnection();

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

        Queue queue = (Queue)initialContext.lookup(SOLACE_OUTPUT_QUEUE_NAME);

        MessageConsumer consumer = session.createConsumer(queue);
        consumer.setMessageListener(this);
        
        connection.start();  
        
        logger.info("Solace connection made successfully");
	}
    
   public void onMessage(Message testMessage) {

		try {
			
			if (testMessage instanceof SolBytesMessage) {

				BytesMessage bytesXMLMessage = ((BytesMessage) testMessage);
				byte[] b = new byte[(int) bytesXMLMessage.getBodyLength()];
				bytesXMLMessage.readBytes(b);
				
				//Print Message received as String
				logger.info("Message received:"+new String(b));
				
				//Get JMS type of message
				logger.info(bytesXMLMessage.getJMSType());
				
				//Dump all message info
				logger.info("Message: "+SolJmsUtility.dumpMessage(testMessage));	
				testMessage.acknowledge();

			}
			
			if (testMessage instanceof SolTextMessage) {
				System.out.println(((SolTextMessage) testMessage).getJMSType());
				System.out.println("Message received:"+ ((SolTextMessage) testMessage).getText());
				System.out.println("Message size: {} kb"+ SolJmsUtility.getMessageSize(testMessage));
				testMessage.acknowledge();
			}
			
		} catch (JMSException e) {
			e.printStackTrace();
		}

	}
}
  • For more information please visit Solace documentation here

Leave a Reply

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