Spring JMS Solace Example

Spring JMS Solace Example thumbnail
By Dhiraj 16 October, 2018

In this article, we will be creating a sample Spring JMS Solace Example that will consume messages from a Solace Messaging queue and topic. For easy setup of our application, we will be using spring boot and all the configurations will be java based following JMS specifications. We will be creating a sample JMS listener that will consume messages from the Solace Messaging queue.

In my previous articles we have already demonstrated multiple examples on JMS and discussed a lot about JMS. But the previous articles were integration with Apache ActiveMQ such as Spring Boot JMS Example with ActiveMQ. Here, we will have a similar implementation with Solace.

Project Structure

It&aposs a simple spring boot project structure. You can visit http://start.spring.io to generate a sample spring boot project.


Following will be the final project structure that we will be building here.


Maven Dependency

Following are the extra dependencies that we have added in this example to enable Solace messaging.


Spring Bean Configuration

Here, we will be defining our connection factory and connection related stuffs. We have configured our message listener and exception listener here.Once, the application is initialised, it will automatically start listening to the Solace queue. This will create 1 connection to the queue or topic.

package com.devglan.soringbootsolaceexample.config;

import com.devglan.soringbootsolaceexample.listener.JmsExceptionListener;
import com.devglan.soringbootsolaceexample.listener.JmsMessageListener;
import com.solacesystems.jms.SolConnectionFactory;
import com.solacesystems.jms.SolJmsUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.Queue;

public class BeanConfig {

    private static final Logger logger = LoggerFactory.getLogger(BeanConfig.class);

    private Environment environment;

    private JmsExceptionListener exceptionListener;

    public SolConnectionFactory solConnectionFactory() throws Exception {
        SolConnectionFactory connectionFactory = SolJmsUtility.createConnectionFactory();
        return connectionFactory;

    public JmsMessageListener jmsMessageListener() {
        return new JmsMessageListener();

    @Bean(destroyMethod = "close")
    public Connection connection() {
        Connection connection = null;
        javax.jms.Session session;
        try {
            connection = solConnectionFactory().createConnection();
            session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(environment.getProperty("solace.message.consumer.queue"));
            MessageConsumer messageConsumer = session.createConsumer(queue);
            logger.info("Connected. Awaiting message...");
        } catch (Exception e) {
            logger.info("JMS connection failed with Solace." + e.getMessage());
        return connection;


Following is the application.properties where we have defined our connection parameters.


Solace Message Listener

Following is the message listener implementation. It assumes the message in the queue or topic to be in a JSON format. It converts the mesage in Customer object and passess to th service class. Th service class then process the data as per the business requirement.

The listener class implements MessageListener and overrides onMesage() method.

package com.devglan.soringbootsolaceexample.listener;

import com.devglan.soringbootsolaceexample.model.Customer;
import com.devglan.soringbootsolaceexample.service.SolaceMessageService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.io.IOException;

public class JmsMessageListener implements MessageListener {

    private static final Logger logger = LoggerFactory.getLogger(JmsMessageListener.class);

    private SolaceMessageService messageService;

    public void onMessage(Message message) {
        String messageData;
        Customer customer;
        if(message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage)message;
            try {
                messageData = textMessage.getText();
                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    customer = objectMapper.readValue(messageData, Customer.class);
                    if(customer == null) {
                        logger.error("Invalid message from the solace queue");
                    }else {
                        logger.info("Successfully parsed solace message to object.");
                } catch (IOException e) {
                    logger.error("Error while parsing JSON from Solace.");
            } catch (JMSException e) {
        }else {
            logger.info("Invalid message. Skipping ....");


Following is our sample customer class.

public class Customer {

    private String id;
    private String firstName;
    private String lastName;
    private String query;

	//setters and getters

Message Exception Listener

To create an exception listener, we require to implement ExceptionListener from javax.jms package. For this example we aare simply logging the exception message.

package com.devglan.soringbootsolaceexample.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.jms.ExceptionListener;
import javax.jms.JMSException;

public class JmsExceptionListener implements ExceptionListener {

    private static final Logger logger = LoggerFactory.getLogger(JmsExceptionListener.class);

    public void onException(JMSException e) {

Running the Example

To start consuming messages mesage from the Solace queue, we can simply run our SoringBootSolaceExampleApplication.java as a java application and we can see thatr our application starts consuming messages from the queue.

package com.devglan.soringbootsolaceexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SoringBootSolaceExampleApplication {

	public static void main(String[] args) {
		SpringApplication.run(SoringBootSolaceExampleApplication.class, args);


In this article, we created a sample spring boot application to consume messages from Solace Messaging queue and topic. We also defined our custrom exception listener. The source code for this implementation can be found at GitHub here.


If You Appreciate This, You Can Consider:

We are thankful for your never ending support.

About The Author

A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.

Further Reading on Spring Boot