Embedded JMS Server Example

This examples shows how to setup and run an embedded JMS server using ActiveMQ.

ActiveMQ was designed using POJOs (Plain Old Java Objects) which means embedding ActiveMQ in your own application is as simple as instantiating a few objects.

This example does not use any configuration files. The server is configured using POJOs and can be easily ported to any dependency injection framework.
We will setup and run a full-fledged JMS server which binds its JMS resources to JNDI and can be accessed by remote clients.

Example step-by-step

To run the example, simply type mvn verify -Pexample from this directory

  1. Create ActiveMQ core configuration, and set the properties accordingly
  2.             Configuration configuration = new ConfigurationImpl();
                configuration.setPersistenceEnabled(false);
                configuration.setSecurityEnabled(false);
                configuration.getAcceptorConfigurations().add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
                Configuration configuration = new ConfigurationImpl();
  3. Create the ActiveMQ core server
  4.             ActiveMQServer activemqServer = ActiveMQ.newActiveMQServer(configuration);
             
  5. Create the JMS configuration
  6.             JMSConfiguration jmsConfig = new JMSConfigurationImpl();
             
  7. Configure the JMS ConnectionFactory
  8.             TransportConfiguration connectorConfig = new TransportConfiguration(NettyConnectorFactory.class.getName());
                ConnectionFactoryConfiguration cfConfig = new ConnectionFactoryConfigurationImpl("cf", connectorConfig, "/cf");
                jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);
             
  9. Configure the JMS Queue
  10.             QueueConfiguration queueConfig = new QueueConfigurationImpl("queue1", null, false, "/queue/queue1");
                jmsConfig.getQueueConfigurations().add(queueConfig);
             
  11. Start the JMS Server using the ActiveMQ core server and the JMS configuration
  12.             JMSServerManager jmsServer = new JMSServerManagerImpl(activemqServer, jmsConfig);
                jmsServer.start();
             

    At this point the JMS server is started and any JMS clients can look up JMS resources from JNDI to send/receive messages from the server. To keep the example simple, we will send and receive a JMS message from the same JVM used to run the JMS server.

  13. Lookup JMS resources defined in the configuration
  14.             ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf");
                Queue queue = (Queue)context.lookup("/queue/queue1");
             
  15. Send and receive a message using JMS API
  16. See the Queue Example for detailed steps to send and receive a JMS message

    Finally, we stop the JMS server and its associated resources.

  17. Stop the JMS server
  18.             jmsServer.stop();
             
  19. Stop the JNDI server
  20.             naming.stop();