Embedded Example

This example shows how to setup and run ActiveMQ embedded with remote clients connecting.

ActiveMQ was designed to use POJOs (Plain Old Java Objects), what makes embedding ActiveMQ as simple as instantiating a few objects.

ActiveMQ Embedded could be used from very simple use cases with only InVM support to very complex cases with clustering, persistence and fail over.

Example step-by-step

To run the example, simply type mvn verify -Pserver from this directory to start the server and mvn verify -Pclient to run the client example

In this we don't use any configuration files. (Everything is embedded). We simply instantiate ConfigurationImpl, ActiveMQServer, start it and operate on JMS regularly


  1. On EmbeddedServer: Create the Configuration, and set the properties accordingly
  2.            Configuration configuration = new ConfigurationImpl();
               configuration.setEnablePersistence(false);
               configuration.setSecurityEnabled(false);
            
  3. On EmbeddedServer: Create and start the server
  4.            ActiveMQServer server = ActiveMQ.newActiveMQServer(configuration);
               server.start();
            
  5. As we are not using a JNDI environment we instantiate the objects directly
  6.            ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName()));
               ClientSessionFactory sf = serverLocator.createSessionFactory();
            
  7. Create a Core Queue
  8.            ClientSession coreSession = sf.createSession(false, false, false);
               final String queueName = "queue.exampleQueue";
               coreSession.createQueue(queueName, queueName, true);
               coreSession.close();
            
  9. Create the session and producer
  10.            session = sf.createSession();
                                       
               ClientProducer producer = session.createProducer(queueName);
            
  11. Create and send a Message
  12.            ClientMessage message = session.createMessage(false);
               message.putStringProperty(propName, "Hello sent at " + new Date());
               System.out.println("Sending the message.");
               producer.send(message);
            
  13. Create the message consumer and start the connection
  14.            ClientConsumer messageConsumer = session.createConsumer(queueName);
               session.start();
            
  15. Receive the message
  16.            ClientMessage messageReceived = messageConsumer.receive(1000);
               System.out.println("Received TextMessage:" + messageReceived.getProperty(propName));
            
  17. Be sure to close our resources!
  18.            if (sf != null)
               {
                  sf.close();
               }