I'm migrating the JavaLite Async from Artemis 2.3.0 to 2.11.0 version. JavaLite Async does NOT use any file base-configuration, but instead relies on code.
Between v 2.3.0 and 2.11.0 the JMS management API is gone/deprecated now, and we are encouraged to use Core Management API.
Unfortunately I cannot find a way to:
- Create a queue programmatically
- Lookup that queue using JNDI to use JMS to send and receive messages.
Here is an example (imports are left for brevity):
class QueueLookup {
private static final String LOCATION = "./target/artemis";
private static EmbeddedActiveMQ server;
public static void main(String[] args) throws Exception {
try{
Configuration configuration = new ConfigurationImpl()
.setPersistenceEnabled(true)
.setBindingsDirectory(LOCATION + "/bindings")
.setJournalDirectory(LOCATION + "/journal")
.setLargeMessagesDirectory(LOCATION + "/largemessages")
.setPagingDirectory(LOCATION + "/paging")
.setSecurityEnabled(false)
.addAcceptorConfiguration("invm", "vm://0")
.setJournalBufferTimeout_AIO(100)
.setJournalBufferTimeout_NIO(100)
.setJournalType(JournalType.NIO)
.setMaxDiskUsage(90);
//the following three lines have no effect
CoreQueueConfiguration coreQueueConfiguration = new CoreQueueConfiguration();
coreQueueConfiguration.setName("Queue123").setDurable(true);
configuration.addQueueConfiguration(coreQueueConfiguration);
server = new EmbeddedActiveMQ();
server.setConfiguration(configuration);
server.start();
TransportConfiguration transportConfiguration = new TransportConfiguration(InVMConnectorFactory.class.getName());
ConnectionFactory connectionFactory = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration);
Hashtable<String, String> jndi = new Hashtable<>();
jndi.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
jndi.put("connectionFactory.ConnectionFactory", "vm://0");
//# queue.[jndiName] = [physicalName]
jndi.put("queue.queue/Queue123", "Queue123");
InitialContext initialContext = new InitialContext(jndi);
Queue jmsQueue = (Queue) initialContext.lookup("queue/Queue123");
try (Connection connection = connectionFactory.createConnection()) {
try(Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)){
MessageProducer producer = jmsSession.createProducer(jmsQueue);
connection.start();
TextMessage message = jmsSession.createTextMessage("Hello, Artemis!");
producer.send(message);
System.out.println("Message sent: " + message.getText());
}
} catch (Exception ex){
ex.printStackTrace();
}
}finally {
server.stop();
}
}
}
These lines have no effect:
CoreQueueConfiguration coreQueueConfiguration = new CoreQueueConfiguration();
coreQueueConfiguration.setName("Queue123").setDurable(true);
configuration.addQueueConfiguration(coreQueueConfiguration);
However, if I remove this line:
jndi.put("queue.queue/Queue123", "Queue123");
then JNDI cannot find the queue.
On the surface, it seems that the I could "create" queues by adding their names to the JNDI:
jndi.put("queue.queue/Queue123", "Queue123");
however, this only works partially, with the queue seems to be existing to
send and receive messages, while neither QueueControl
nor QueueBrtowser
can find it.
Can someone please explain how I can do this in code (no XML configuration):
- Create a queue and pass all necessary parameters (durable, etc)
- Find this queue using JNDI
- Control this queue using QueueControl
- Browse this queue using QueueBrowser.
A full example complete with a GUI version can be found here: https://github.com/ipolevoy/artemis-sanbox/blob/master/src/main/java/examples/
Much appreciate any help!
EmbeddedJMS
, while the classJMSConfiguration
is not even deprecated. Is there a valid reason to still use it? – ipolevoyJMSConfiguration
is used byEmbeddedJMS
andJMSServerManager
both of which are deprecated so I don't see any reason why you'd use it. – Justin BertramJMSConfiguration
is not deprecated, which makes if very confusing for us. It would be great if you had a migration guide for people using JMS management API. However, seems we are over the hump, slogging for about week on it internally :) – ipolevoy