Skip to content

Instantly share code, notes, and snippets.

@kimathie
Last active May 20, 2021 13:27
Show Gist options
  • Save kimathie/ffc344c6d59de2b5782f570a22f74d10 to your computer and use it in GitHub Desktop.
Save kimathie/ffc344c6d59de2b5782f570a22f74d10 to your computer and use it in GitHub Desktop.
A simple ActiveMQ Artemis Core API Client
try (ServerLocator locator = ActiveMQClient.createServerLocator("tcp://localhost:61617");
ClientSessionFactory factory = locator.createSessionFactory();
ClientSession session = factory.createSession(true, true, 0)) {
String queueName = "router::hornetq";
/**
* In reference to the question
* https://stackoverflow.com/questions/67606298/how-to-configure-an-activemq-artemis-queue-not-be-created-again
* Just catch the Exception and handle it quietly and proceed executing the rest of the code.
* Hopefully this wont be necessary in future versions.
*/
try {
session.createQueue(new QueueConfiguration(queueName)
.setAutoCreateAddress(Boolean.TRUE)
.setDurable(Boolean.TRUE)
.setRoutingType(RoutingType.ANYCAST));
} catch (ActiveMQQueueExistsException e) {
System.out.println("Queue '" + queueName + "' exists.");
} catch (Exception e) {
throw new RuntimeException(e);
}
try (ClientProducer producer = session.createProducer(queueName);
ClientConsumer consumer = session.createConsumer(queueName)) {
session.start();
Thread sender = new Thread(new Sender(session, producer), "Sender");
Thread receiver = new Thread(new Receiver(consumer), "Receiver");
sender.setDaemon(true);
receiver.setDaemon(true);
sender.start();
receiver.start();
receiver.join();
}
} catch (Error | Exception e) {
throw new RuntimeException(e);
}
@jbertram
Copy link

jbertram commented May 20, 2021

For what it's worth, all the resources you're using (i.e. ServerLocator, ClientSessionFactory, ClientProducer, and ClientConsumer) implement java.lang.AutoCloseable so you can use try-with-resources instead of using a finally block with a null check. This would make your code simpler and more readable, e.g.:

try (ServerLocator locator = ActiveMQClient.createServerLocator("tcp://localhost:61617");
     ClientSessionFactory factory = locator.createSessionFactory();
     ClientSession session = factory.createSession(true, true, 0)) {

   String queueName = "router::hornetq";

   QueueConfiguration queue = new QueueConfiguration(queueName)
      .setAutoCreateAddress(Boolean.TRUE)
      .setDurable(Boolean.TRUE)
      .setRoutingType(RoutingType.ANYCAST);
   /**
    * In reference to the question
    * https://stackoverflow.com/questions/67606298/how-to-configure-an-activemq-artemis-queue-not-be-created-again
    * Just catch the Exception and handle it quietly and proceed executing the rest of the code.
    * Hopefully this wont be necessary in future versions.
    */
   try {
      session.createQueue(queue);
   } catch (ActiveMQQueueExistsException e) {
      System.out.println("Queue '" + queueName + "' exists.");
   } catch (Exception e) {
      throw new RuntimeException(e);
   }

   try (ClientProducer producer = session.createProducer(queueName);
        ClientConsumer consumer = session.createConsumer(queueName)) {

      session.start();

      Thread sender = new Thread(new Sender(session, producer), "Sender");
      Thread receiver = new Thread(new Receiver(consumer), "Receiver");

      sender.setDaemon(true);
      receiver.setDaemon(true);

      sender.start();
      receiver.start();
   }
} catch (Error | Exception e) {
   throw new RuntimeException(e);
}

Also, I think that you will find your sender and receiver threads won't work since as soon as you call start on them you close their resources (i.e. session, producer, & consumer).

@kimathie
Copy link
Author

kimathie commented May 20, 2021

Thanks Justin, this is really good feedback,

  1. I was actually intending to check the code to see if it uses autoclosable but this answers it all.
  2. As for the closing of resources, I forgot to add the join methods. I have corrected that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment