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);
}
@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