Using ActiveMQ core API

Apache ActiveMQ features also a core API which can be used to handle the JMS server and operations on the top of it, using its own non-JMS API. This is also known as the core API.

The core API provides all the functionality of JMS but without much of the complexity. It also provides features that are not available using JMS.

Here is an example, contained in the example/core/embedded folder which shows how you can connect to a remote Apache ActiveMQ Artemis Server using the default address, create a JMS Queue, send a message and consume it:

public class EmbeddedRemoteExample
{

   public static void main(final String[] args)
   {
      try
      {
         // Step 3. As we are not using a JNDI environment we instantiate the objects directly

 
         Map<String,Object> map = new HashMap<String,Object>();
         map.put("host", "localhost");
         map.put("port", 61616);
         // -------------------------------------------------------

         ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName(), map));
         ClientSessionFactory sf = serverLocator.createSessionFactory();

         // Step 4. Create a core queue
         ClientSession coreSession = sf.createSession(false, false, false);

         final String queueName = "queue.exampleQueue";

         coreSession.createQueue(queueName, queueName, true);

         coreSession.close();

         ClientSession session = null;

         try
         {

            // Step 5. Create the session, and producer
            session = sf.createSession();

            ClientProducer producer = session.createProducer(queueName);

            // Step 6. Create and send a message
            ClientMessage message = session.createMessage(false);

            final String propName = "myprop";

            message.putStringProperty(propName, "Hello sent at " + new Date());

            System.out.println("Sending the message.");

            producer.send(message);

            // Step 7. Create the message consumer and start the connection
            ClientConsumer messageConsumer = session.createConsumer(queueName);
            session.start();

            // Step 8. Receive the message.
            ClientMessage messageReceived = messageConsumer.receive(1000);
            System.out.println("Received TextMessage:" + messageReceived.getStringProperty(propName));
         }
         finally
         {
            // Step 9. Be sure to close our resources!
            if (sf != null)
            {
               sf.close();
            }
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
}

Found the article helpful? if so please follow us on Socials