0

下面的代码在 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;
}
4

1 回答 1

1

使用 Wildfly 10,JMS 实现从 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"));
    }
}

代码在具有以下 maven 依赖项的 WAR 中运行:

<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 资源。对于某些用例,这可能已经足够好了。

于 2018-02-09T17:42:48.980 回答