More and more, we’re making use of Spring Boot within our projects internally at Solace. It enables us to be much more productive as we develop new messaging based microservices. But what was missing up until now was a nice Spring Boot Starter for Solace JMS and Solace Java messaging. Without these packages, you couldn’t take full advantage of the benefits of Spring Boot auto-configuration which is a very powerful part of Spring Boot.

Now as part of Solace Labs, we’ve shared two projects to help other Solace users easily use Solace Messaging as the foundation for their Spring Boot microservices:

Following the Solace Labs model, both projects are open source and we welcome contributions, suggestions and ideas from the community. We’ll keep updating these projects as we use them more ourselves, but already they are making our applications simpler.

In this blog post, I’ll focus on the Solace JMS Spring Boot Starter due to the prevalence of JMS applications. I’ll go over the following:

  • What is Spring Boot?
  • Using Solace JMS with Spring Boot
  • How does the Sample Application work?

If you’re a seasoned Spring Boot developer, you’ll be most interested in the “Using Solace JMS with Spring Boot”. And if you’re a Solace Java API developer, stay tuned for another blog post which will provide a similar intro focused on the Solace Java Spring Boot Starter.

What is Spring Boot?

If you’re new to Spring Boot, then let me provide you a quick summary. You can learn a lot more on the home page for Spring Boot or from the user documentation. In their words:

“Spring Boot takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.”

And that’s exactly what it does. Take for example, their tutorial showing you how to build a RESTful webservice. You are up and running with only a handful of lines of code. It really makes application development much easier.

Spring Boot Starter POMs

One of the key features I’d like to focus on in this blog is the Spring Boot Starter POMs. These POMs simplify your build configuration by making it possible to include a single dependency in your application build. The starter POMs then handle including all the required dependencies to get the requested component working within a Spring Boot application.

Third-parties can easily create their own auto-configuration modules and Starters and that is exactly what we’ve done in the two projects I mentioned above. Let’s look at how this can help you in your applications.

Auto-Configuration and Dependency-Injection

A second key feature related to this project is Spring Auto-Configuration and the related Dependency-Injection. For a primer you can refer to the Spring Boot documentation. In summary:

Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, If HSQLDB is on your classpath, and you have not manually configured any database connection beans, then we will auto-configure an in-memory database.

Applications opt-in for auto-configuration. Using auto-configuration can make it much easier to rapidly create portable applications and to control the configuration of applications at run time using Spring’s Externalized configuration. I’ll walk you through an example of this below as we look at the included example application.

Using Solace JMS with Spring Boot

Using Solace JMS within Spring is very easy because of the nice work the Spring Team has with the JMS core package. The JMSTemplate is used for sending and there are a variety of ways to receive. I’ll outline how to receive asynchronously using Message-Driven POJO. Applications wishing to use Solace JMS need to do two things:

  • Add the solace-jms-spring-boot-starter dependency to their build.
  • Configure your application.

Updating your build

To use Solace Messaging you need to add a single dependency pointing to the Solace JMS Spring Boot Starter POM. This POM follows the Spring Boot Starter pattern and manages all other dependencies that your application needs to use Solace JMS including the underlying Solace JMS client library. For example, in Gradle:

compile("com.solace.labs.spring.boot:solace-jms-spring-boot-starter:0.1.1")

or with maven

<dependency>
    <groupId>com.solace.labs.spring.boot</groupId>
    <artifactId>solace-jms-spring-boot-starter</artifactId>
    <version>0.1.1</version>
</dependency>

Configuring your application

The final step is to provide the Solace Message Router configuration properties to your application. Spring Boot provides multiple ways to externalize the configuration properties of your application. In this example, I’ll use the application.properties approach. But you could also use a Spring Configuration Server or whatever method is the norm in your environment.

In your application.properties, you need to add any details required by the Solace JMS API to connect with the Solace Message Router. At a minimum you must specify the host. For example:

solace.jms.host=192.168.0.50

You can add other details like message-vpn, client-username, client-password by adding additional properties to application.properties. See the project readme for a full list of what is supported. If you need a property that is not yet supported, you can also customize the ConnectionFactory in code and set any required API property. And as stated we’ll add more properties to this project as we go along.

At this point because of Spring’s auto-configuration and dependency injection, that’s all you need to do. For an example of this, check out the sample that is included in the GitHub project.

How does the Sample Application work?

For those wishing to know a bit more about how auto-configuration worked in the sample, let’s dive into the details. You can check the Spring docs for a primer on auto-configuration.

Let’s focus on sending. The application is a class named DemoApplication and is annotated with @SpringBootApplication. This among other things enables Spring auto-configuration.

@SpringBootApplication
public class DemoApplication

Messages are sent using the MessageProducer class which uses the JmsTemplate to actually send a JMS message.

@Service
static class MessageProducer implements CommandLineRunner {
        @Autowired
        private JmsTemplate jmsTemplate;

        @Override
        public void run(String... strings) throws Exception {
            String msg = "Hello World";
            this.jmsTemplate.convertAndSend("testQueue", msg);
        }
    }

The class uses the Spring annotation @Service which flags this class to be automatically registered as Spring bean. The @Autowired will enable Spring constructor injection creating an auto-generated constructor which will set the JmsTemplate using dependency injection.

As Spring is creating the required JmsTemplate bean, dependency-injection will again cause the creation of a JMS ConnectionFactory. Spring auto-configuration will search the classpath to find a class capable of providing a JMS ConnectionFactory and the Solace JMS auto-configuration jar provides such a class: SolaceJmsAutoConfiguration. That class knows how to create Solace JMS Connection Factories. And that is how your application is bootstrapped with Solace JMS.

Summary

In summary, I think it’s easy to see why the Solace Spring Boot Starters combined with auto-configuration simplify Solace Messaging applications. Why don’t you try them out yourself in your application and see how it can help you? We’ll keep improving them as we go, so you’ll continue to see new properties introduced giving you more control through your application.properties file but already they’re fine for use in many common scenarios. If you have any feedback, raise an issue in GitHub with your ideas and we’ll be happy to work with you!

I’ve also published this post about the Solace Java Spring Boot project which provides a Spring Boot Starter for the Solace Java API with support for Spring auto-configuration.

If you’re new to JMS and would like more help getting up and running these are two great places to check out for more tutorials:

Mark Spielman

Mark Spielman is a product leader with 15+ years of experience helping customers become event driven while building the products they need to become more successful. His current passion with Solace is making sure PubSub+ Cloud is the right choice for everyone looking to modernize their business and embrace event-driven transformation.

Throughout his career, he has had experience and interest in a wide range of topics including large-scale enterprise architecture, cloud platforms, developer engagement, SaaS, agile teams, cross-functional team building, operations and process improvements, product go-to-market, pricing strategies and data-driven product design.

Mark holds a B.A.Sc. in Computer Engineering from the University of Waterloo and always enjoys learning new things.