Embedded JMS Server Example

This examples shows how to setup and run an embedded JMS server using ActiveMQ along with ActiveMQ configuration files.

Example step-by-step

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

  1. Create ActiveMQ core configuration files and make sure they are within your classpath. By default, ActiveMQ expects the configuration file name to be "activemq-configuration.xml".
  2. Create an embedded ActiveMQ JMS server
  3.             EmbeddedJMS jmsServer = new EmbeddedJMS();
             
  4. Setup security configurations
  5.             SecurityConfiguration securityConfig = new SecurityConfiguration();
                securityConfig.addUser("guest", "guest");
                securityConfig.addRole("guest", "guest");
                securityConfig.setDefaultUser("guest");
                jmsServer.setSecurityManager(new ActiveMQSecurityManagerImpl(securityConfig));
             
  6. Start the embedded ActiveMQ JMS server
  7.             jmsServer.start()
             
  8. Create JMS resources (connection factory and queue) for the example
  9.             JMSServerManager jmsServerManager = jmsServer.getJMSServerManager();
                List connectors = new ArrayList();
                connectors.add("in-vm");
                jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory");
                jmsServerManager.createQueue(false, "exampleQueue", null, false, "queue/exampleQueue");
             

    At this point the JMS server is started and any JMS clients can look up JMS resources from the 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.

  10. Lookup JMS resources defined in the configuration
  11.             ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
                Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");
             
  12. Send and receive a message using JMS API
  13. See the Queue Example for detailed steps to send and receive a JMS message

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

  14. Close the connection
  15.             if (connection != null)
                {
                   connection.close();
                }
             
  16. Stop the JMS server
  17.             jmsServer.stop();