1. Introduction

JMS or Java Messaging Service is a Java API standard that allows our Java applications to send a message to another Java (or some other) application. It's highly scalable and allows loosely coupling of our applications because of asynchronous messaging. 
JMS is an API standard, which means that it provides the definition (collection of interfaces), and what we're using are its implementations. 
In our experience, RabbitMQ and Apache ActiveMQ are mostly used, but there are also others like Amazon SQS, JBoss Messaging, and paid implementations like one from Oracle - OracleAQ or from IBM - IBM MQ.

In this short tutorial, we'll show how to create an embedded JMS server with ActiveMQ and Spring Boot. This type of server is not so common in the production environment and is usually used for demonstration purposes since it's dependent on the parent application. 

2. Implementation

First, let's add Maven dependencies needed for our Spring Boot application that will work with ActiveMQ:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>artemis-server</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>artemis-jms-server</artifactId>
</dependency>

Embedded Artemis is hard to be found in production environments. It's usually used for demo purposes and Hello-World applications where we need to quickly set up a server for further use. There are few ways to set up a JMS server, and in this example, we'll go with the easiest:

And now, let's see how to start the embedded JMS server:

import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.ActiveMQServers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jms.artemis.ArtemisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
public class SendMessageDemoApplication {

    public static void main(String[] args) throws Exception {
      ActiveMQServer server = ActiveMQServers
        .newActiveMQServer(new ConfigurationImpl()
        .setPersistenceEnabled(true)
        .setJournalDirectory("target/journal")
        .setSecurityEnabled(false));
      server.start();
      SpringApplication.run(SendMessageDemoApplication.class, args);
    }
}

Notice that in the configuration we're setting the persistence to true. ActiveMQ supports both persistent and non-persistent message delivery. As per the JMS specification, the default delivery mode is persistent. The main difference between persistent and non-persistent delivery is that if you are using persistent delivery, messages are persisted to disk or database (currently not production-ready on Apache Artemis ActiveMQ) so that they will live after a broker restart. When using non-persistent delivery, if the broker is restarted, all in-transit messages will be lost. Also, in this example, we disabled security. 

To send a JMS message to the embedded Artemis ActiveMQ, check out this tutorial, and if you want to see how to receive the JMS message from the embedded server, check out this one. 

Since it's also possible to use 

3. Conclusion

In this tutorial, we showed how to create an embedded JMS server with ActiveMQ and Spring Boot.