如何在运行时以编程方式为Wildfly 10/11添加jms队列?

时间:2017-12-10 12:55:04

标签: jms wildfly-10 wildfly-11

下面的代码在Wildfly 9.0.1中运行时创建JMS队列没有问题。在Wildfly 10和11中,hornetq-server被activemq取代。如何正确地将其迁移到Wildfly 10/11? 谢谢。

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 个答案:

答案 0 :(得分:0)

使用Wildfly 10,JMS-Implementation从HornetQ变为Apache ActiveMQ Artemis。以下示例使用Wildfly 10进行测试。

您可以准备命令来创建这样的队列:

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

使用此方法执行操作:

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

代码在WAR中运行,具有以下maven依赖关系:

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

使用Java EE 7和JMS 2.0,还有注释@JMSDestinationDefinitions,它允许在部署时自动创建JMS资源。对于某些用例,这已经足够好了。