【问题标题】:WildFly Swarm configure Messaging RemoteWildFly Swarm 配置消息远程
【发布时间】:2018-05-14 11:26:20
【问题描述】:

我将配置两个 wildfly-swarm(版本 2018.5.0)服务器远程。第一个服务器应该向第二个服务器发送消息(通过 wildfly-swarm 消息传递)。在第二台服务器上,一个消息使用者正在运行。

在阅读了很多(过时的)教程后,我得出的结论是我太愚蠢了。

我在一台服务器上构建了一个带有 wildfly-swarm 消息传递的测试项目。

project-default.yaml

swarm:
  messaging-activemq:
    servers:
      default:
        jms-queues:
          my-queue: {}
        jms-topics:
          my-topic: {}
  logging:
      pattern-formatters:
        LOG_FORMATTER:
          pattern: "%p [%c] %s%e%n"
      periodic-rotating-file-handlers:
        FILE:
          file:
            path: pathtolog/swarm.log
          suffix: .yyyy-MM-dd
          named-formatter: LOG_FORMATTER
          level: ALL
      root-logger:
        handlers:
        - FILE

MyApplication.java

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class MyApplication extends Application
{
}

MyResource.java

import javax.annotation.Resource;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.jms.JMSContext;
import javax.jms.Topic;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.logging.Level;
import java.util.logging.Logger;


@ApplicationScoped
@Path("/")
public class MyResource
{
    Logger LOG = Logger.getLogger(MyResource.class.getName());

    public static final String MY_TOPIC = "/jms/topic/my-topic";

    @Inject
    private JMSContext context;

    @Resource(lookup = MY_TOPIC)
    private Topic topic;

    @GET
    @Produces("text/plain")
    public String get()
    {
        LOG.log(Level.INFO, "Send Message Hello JMS!");
        context.createProducer().send(topic, "Hello JMS!");
        return "send!";
    }

}

MyTopicMDB.java

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.logging.Level;
import java.util.logging.Logger;


@MessageDriven(name = "MyTopicMDB", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = MyResource.MY_TOPIC),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
})
public class MyTopicMDB implements MessageListener
{
    Logger LOG = Logger.getLogger(MyResource.class.getName());

    @Override
    public void onMessage(Message message)
    {
        try
        {
            LOG.log(Level.INFO, "received Message " + ((TextMessage) message).getText());
            System.out.println("received: " + ((TextMessage) message).getText());
        }
        catch (JMSException e)
        {
            LOG.log(Level.INFO, "Fehler: " + e);
            e.printStackTrace();
        }
    }
}

知道我必须如何为服务器(一个发送者和一个消费者)配置 project-default.yaml 吗?

【问题讨论】:

    标签: java jms wildfly-swarm


    【解决方案1】:

    我们遇到了同样的问题,你几乎就在那里:唯一缺少的是 Swarm(现在是 Thorntail)服务器的项目默认设置,它将连接到远程消息传递服务器:

    swarm:
      network:
        socket-binding-groups:
          standard-sockets:
            outbound-socket-bindings:
              remote-activemq-socket-binding:
                remote-host: <address of remote server>
                remote-port: <port of remote server, likely going to be 61616>
    
      messaging-activemq:
        servers:
          default:
            [...]
            remote-connectors:
              remote-activemq-connector:
                socket-binding: remote-activemq-socket-binding
            pooled-connection-factories:
              remote-connection-factory:
                # if authentication is required
                user: <user>
                password: <password>
                connectors:
                  - remote-activemq-connector
                entries:
                  - 'java:/jms/remote-mq'
                  - 'java:/DefaultJMSConnectionFactory'
    

    然后在发送方,你会像这样注入你的连接工厂:

    @Inject
    @JMSConnectionFactory("java:/jms/remote-mq")
    private JMSContext context;
    

    或者,在消费者方面,您可以使用

    注释您的 MDB
    @ResourceAdapter("remote-connection-factory")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多