How to add jms queue programmatically in runtime for Wildfly 10/11?

1k Views Asked by At

This code below creates JMS queue at run time in Wildfly 9.0.1 with no problem. In Wildfly 10 and 11 hornetq-server was replaced with activemq. How properly migrate it to Wildfly 10/11? Thank you.

private boolean createQueue(String operationName, String queueName) {
    boolean result = false;

        ModelControllerClient client = qService.getModelControllerClient();       
        if(client != null){
            ModelNode operation = new ModelNode();
            ModelNode address = operation.get(ClientConstants.OP_ADDR);

            address.add("subsystem", "messaging");
            address.add("hornetq-server", "default");

            address.add("jms-queue", queueName);

            ModelNode entries = operation.get("entries");
            entries.add("jms/queue/" + queueName);
            operation.get(ClientConstants.OP).set(operationName);

            try {                
                ModelNode returnVal = client.execute(operation);
                return returnVal.get("outcome").asString().equalsIgnoreCase("success");

            } catch (Exception e) {
                DLOG.error(ExceptionUtils.getStackTrace(e));
            } finally {
                try {
                    client.close();
                } catch (IOException ex) {
                    DLOG.error(ExceptionUtils.getStackTrace(ex));
                }
            }
        }        
    return result;
}
1

There are 1 best solutions below

0
On

With Wildfly 10 the JMS-Implementation changed from HornetQ to Apache ActiveMQ Artemis.The following example is tested with Wildfly 10.

You could prepare the command to create a queue like this:

public void createQueue() throws Exception {
    ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9990);
    if (client != null) {
        ModelNode op = new ModelNode();
        op.get(ClientConstants.OP_ADDR).add(ClientConstants.SUBSYSTEM, "messaging-activemq");
        op.get(ClientConstants.OP_ADDR).add(ClientConstants.SERVER, "default");
        op.get(ClientConstants.OP_ADDR).add("jms-queue", "HelloWorldQueue");
        op.get("entries").add("queue/HelloWorldQueue");
        op.get("entries").add("java:jboss/exported/queue/HelloWorldQueue");
        op.get(ClientConstants.OP).set("add");
        applyUpdate(op, client);
    }
}

And execute the operation with this method:

private static void applyUpdate(ModelNode update, final ModelControllerClient client) throws IOException {
    LOG.info("Execute: " + update.toString());
    ModelNode result = client.execute(new OperationBuilder(update).build());
    if (result.hasDefined("outcome") && "success".equals(result.get("outcome").asString())) {
        if (result.hasDefined("result")) {
            LOG.info(result.get("result").toString());
        }
    } else if (result.hasDefined("failure-description")) {
        throw new RuntimeException(result.get("failure-description").toString());
    } else {
        throw new RuntimeException("Operation not successful; outcome = " + result.get("outcome"));
    }
}

The code runs inside a WAR with the following maven dependency:

<dependency>
    <groupId>org.wildfly.core</groupId>
    <artifactId>wildfly-controller-client</artifactId>
    <version>3.0.10.Final</version>
</dependency>

With Java EE 7 and JMS 2.0 there is as well the annotation @JMSDestinationDefinitions which allows automatic creation of JMS resources at deployment time. For some use cases this could already be good enough.