Apache ActiveMQ Artemis is designed as set of simple Plain Old Java Objects (POJOs). This means Apache ActiveMQ Artemis can be instantiated and run in any dependency injection framework such as Spring or Google Guice. It also means that if you have an application that could use messaging functionality internally then it can directly instantiate Apache ActiveMQ Artemis clients and servers in its own application code to perform that functionality. We call this embedding Apache ActiveMQ Artemis.

Examples of applications that might want to do this include any application that needs very high performance, transactional, persistent messaging but doesn’t want the hassle of writing it all from scratch.

Embedding Apache ActiveMQ Artemis can be done in very few easy steps - supply a broker.xml on the classpath or instantiate the configuration object, instantiate the server, start it, and you have a Apache ActiveMQ Artemis running in your JVM. It’s as simple and easy as that.

1. Embedding with XML configuration

The simplest way to embed Apache ActiveMQ Artemis is to use the embedded wrapper class and configure Apache ActiveMQ Artemis through broker.xml.

Here’s a simple example broker.xml:

  1. <configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:activemq" xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
  2. <core xmlns="urn:activemq:core">
  3. <persistence-enabled>false</persistence-enabled>
  4. <security-enabled>false</security-enabled>
  5. <acceptors>
  6. <acceptor name="in-vm">vm://0</acceptor>
  7. </acceptors>
  8. </core>
  9. </configuration>
  1. import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
  2. ...
  3. EmbeddedActiveMQ embedded = new EmbeddedActiveMQ();
  4. embedded.start();
  5. ServerLocator serverLocator = ActiveMQClient.createServerLocator("vm://0");
  6. ClientSessionFactory factory = serverLocator.createSessionFactory();
  7. ClientSession session = factory.createSession();
  8. session.createQueue(new QueueConfiguration("example"));
  9. ClientProducer producer = session.createProducer("example");
  10. ClientMessage message = session.createMessage(true);
  11. message.getBody().writeString("Hello");
  12. producer.send(message);
  13. session.start();
  14. ClientConsumer consumer = session.createConsumer("example");
  15. ClientMessage msgReceived = consumer.receive();
  16. System.out.println("message = " + msgReceived.getBody().readString());
  17. session.close();

The EmbeddedActiveMQ class has a few additional setter methods that allow you to specify a different config file name as well as other properties. See the javadocs for this class for more details.

2. Embedding with programmatic configuration

You can follow this step-by-step guide to programmatically embed a broker instance.

Create the Configuration object. This contains configuration information for an Apache ActiveMQ Artemis instance. The setter methods of this class allow you to programmatically set configuration options as described in the Server Configuration section.

The acceptors are configured through Configuration. Just add the acceptor URL the same way you would through the main configuration file.

  1. import org.apache.activemq.artemis.core.config.Configuration;
  2. import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
  3. ...
  4. Configuration config = new ConfigurationImpl();
  5. config.addAcceptorConfiguration("in-vm", "vm://0");
  6. config.addAcceptorConfiguration("tcp", "tcp://127.0.0.1:61616");

You need to instantiate an instance of org.apache.activemq.artemis.api.core.server.embedded.EmbeddedActiveMQ and add the configuration object to it.

  1. import org.apache.activemq.artemis.api.core.server.ActiveMQ;
  2. import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
  3. ...
  4. EmbeddedActiveMQ server = new EmbeddedActiveMQ();
  5. server.setConfiguration(config);
  6. server.start();

You also have the option of instantiating ActiveMQServerImpl directly:

  1. ActiveMQServer server = new ActiveMQServerImpl(config);
  2. server.start();