In this blog post, I will look at the Solace Java Spring Boot project which provides a Spring Boot Starter for the Solace Java API with support for Spring auto-configuration. This is a follow up blog post to my first post on the topic of Spring Boot Starters which discussed JMS.
To recap, 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 and applications:
- https://github.com/SolaceLabs/solace-java-spring-boot
- https://github.com/SolaceLabs/solace-jms-spring-boot
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 Java Spring Boot Starter.
Using the Solace Java API with Spring Boot
The Solace Java Spring Boot Starter and Auto-Configuration is very similar to the Solace JMS project in how it functions. It provides an auto-configuration implementation and Spring Boot starter POM for the Solace Java API. However, in this case your application is using the Solace Java API and therefore will contain Solace Java API calls in the application. Spring Boot Applications wishing to use Solace Java need to do the following things:
- Adding the
solace-java-spring-boot-starter
dependency to the build. - Update your application to use the
SpringJCSMPFactory
- Configure your application properties.
Updating your build
To use Solace Messaging you need to add a single dependency pointing to the Solace Java Spring Boot Starter POM. This POM follows the Spring Boot Starter pattern and manages all other dependencies that your application needs to use the Solace Java API including the underlying Solace Java client library. For example, in Gradle:
com.solace.labs.spring.boot:solace-java-spring-boot-starter:0.1.1
or with Maven:
<dependency> <groupId>com.solace.labs.spring.boot</groupId> <artifactId>solace-java-spring-boot-starter</artifactId> <version>0.1.1</version> </dependency>
Updating your application to use the SpringJCSMPFactory
To facilitate Spring dependency injection, this project introduces a new Solace Java API factory (SpringJCSMPFactory
). This class maintains the same interface as the Singleton JCSMPFactory
in the Solace Java API but it contains an associated JCSMPProperties
. This makes it easy to auto-wire this dependency in your code:
@Autowired private SpringJCSMPFactory solaceFactory;
Once you have the SpringJCSMPFactory
, it behaves just like the JCSMPFactory
and can be used to create JCSMPSessions. For example:
final JCSMPSession session = solaceFactory.createSession();
This is explained in more detail in the project readme. Also the project contains a sample which illustrates simple usage.
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 your 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 Java API to connect with the Solace Message Router. At a minimum you must specify the host. For example:
solace.java.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.
Summary
To see this all in action, checkout the sample which illustrates the points explained in the blog. And again, 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?
If you’re new to the Solace Java API and would like more help getting up and running there are nice tutorials here: https://tutorials.solace.dev/jcsmp
Explore other posts from category: DevOps