Wildfly 10集群(域)中的Artemis(ActiveMQ)消息传递

时间:2016-03-09 10:00:02

标签: activemq cluster-computing wildfly activemq-artemis

有人可以提供在Wildfly 10群集(域)下工作的消息传递应用程序示例吗?我们正在努力解决这个问题,鉴于它是一项新技术,可怜的资源缺乏。

目前我们有以下内容:

由两个主机(节点)组成的域,每个域上有三个组,即域中的六个独立服务器。

服务器配置的相关部分(在domain.xml中):

        <subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
            <server name="default">
                <security enabled="false"/>
                <cluster password="${jboss.messaging.cluster.password}"/>
                <security-setting name="#">
                    <role name="guest" delete-non-durable-queue="true" create-non-durable-queue="true" consume="true" send="true"/>
                </security-setting>
                <address-setting name="#" redistribution-delay="1000" message-counter-history-day-limit="10" page-size-bytes="2097152" max-siz
                <http-connector name="http-connector" endpoint="http-acceptor" socket-binding="http"/>
                <http-connector name="http-connector-throughput" endpoint="http-acceptor-throughput" socket-binding="http">
                    <param name="batch-delay" value="50"/>
                </http-connector>
                <in-vm-connector name="in-vm" server-id="0"/>
                <http-acceptor name="http-acceptor" http-listener="default"/>
                <http-acceptor name="http-acceptor-throughput" http-listener="default">
                    <param name="batch-delay" value="50"/>
                    <param name="direct-deliver" value="false"/>
                </http-acceptor>
                <in-vm-acceptor name="in-vm" server-id="0"/>
                <broadcast-group name="bg-group1" connectors="http-connector" jgroups-channel="activemq-cluster" jgroups-stack="tcphq"/>
                <discovery-group name="dg-group1" jgroups-channel="activemq-cluster" jgroups-stack="tcphq"/>
                <cluster-connection name="my-cluster" discovery-group="dg-group1" connector-name="http-connector" address="jms"/>
                <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
                <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
                <jms-queue name="TestQ" entries="java:jboss/exported/jms/queue/testq"/>
                <connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
                <connection-factory name="RemoteConnectionFactory" reconnect-attempts="-1" block-on-acknowledge="true" ha="true" entries="java
                <pooled-connection-factory name="activemq-ra" transaction="xa" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" co
            </server>
        </subsystem>

除了添加 TestQ 队列外,配置或多或少是默认的。

tcphq 堆栈在JGroups配置中定义如下:

                <stack name="tcphq">
                    <transport type="TCP" socket-binding="jgroups-tcp-hq"/>
                    <protocol type="TCPPING">
                        <property name="initial_hosts">
                          dev1[7660],dev1[7810],dev1[7960],dev2[7660],dev2[7810],dev2[7960]
                        </property>
                        <property name="port_range">
                            0
                        </property>
                    </protocol>
                    <protocol type="MERGE3"/>
                    <protocol type="FD_SOCK" socket-binding="jgroups-tcp-hq-fd"/>
                    <protocol type="FD"/>
                    <protocol type="VERIFY_SUSPECT"/>
                    <protocol type="pbcast.NAKACK2"/>
                    <protocol type="UNICAST3"/>
                    <protocol type="pbcast.STABLE"/>
                    <protocol type="pbcast.GMS"/>
                    <protocol type="MFC"/>
                    <protocol type="FRAG2"/>
                </stack>

我编写了一个测试应用程序,它包含一个简单的“服务器”,意思是MDB和客户端,如下所示:

服务器(MDB):

@MessageDriven(mappedName = "test", activationConfig = {
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "java:jboss/exported/jms/queue/testq"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MessageServer implements MessageListener {

    @Override
    public void onMessage(Message message) {

        try {
            ObjectMessage msg = null;

            if (message instanceof ObjectMessage) {
                msg = (ObjectMessage) message;
            }
            System.out.print("The number in the message: "+ msg.getIntProperty("count"));
        } catch (JMSException ex) {
            Logger.getLogger(MessageServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

客户端:

@Singleton
@Startup
public class ClientBean implements ClientBeanLocal {

    @Resource(mappedName = "java:jboss/exported/jms/RemoteConnectionFactory")
    private ConnectionFactory factory;

    @Resource(mappedName = "java:jboss/exported/jms/queue/testq")
    private Queue queue;

    @PostConstruct
    public void sendMessage() {

        Connection connection = null;
        try {

            connection = factory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(queue);

            Message message = session.createObjectMessage();
            message.setIntProperty("count", 1);

            producer.send(message);
            System.out.println("Message sent.");

        } catch (JMSException ex) {
            Logger.getLogger(ClientBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(ClientBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (connection != null) connection.close();
            } catch (JMSException ex) {
                Logger.getLogger(ClientBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}

如果客户端和服务器都位于同一个组中,它实际上运行良好。在这种情况下,它甚至似乎在主机(节点)之间进行通信。但是,如果服务器和客户端位于不同的组中,则不会调用MDB。此外,甚至看起来只有当MDB驻留在具有0偏移的组中时才调用它。当我将服务器MDB移动到另一个组时,即使客户端位于同一组中,它也没有响应。

我对Wildfly 10中的JMS感到有点困惑。对于使用HornetQ的旧版本,有很多示例和材料,但对于Artemis来说却很少。有人可以帮忙吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

当我提出同样的问题时 - 把答案对我有用。

实际上作为Miroslav answered on developer.jboss.org,每个服务器上socket-binding"jgroups-tcp-hq"配置的首要问题是port-offset。 应该在 dev1 [7660] 服务器上将<socket-binding name="jgroups-tcp-hq" ... port="7600"/>port-offset设置为(例如,使用jboss.socket.binding.port-offset属性)为60; 210 dev1 [7810] ; 360在 dev1 [7960] 上。对于 dev2 服务器也是如此。

第二个是jboss.bind.address.private财产 通常默认的jgroup socket-binding是指"private"接口,例如

<socket-binding name="jgroups-tcp-hq" interface="private" port="7600"/>

所以"private"接口地址必须与jboss.bind.address.private属性一起提供(例如jboss.bind.address.private=dev1) - 否则将无法在节点之间建立ClusterConnectionBridge!
See also this post for more details.

如果建立了ActiveMQ服务器实例之间的通信,则日志条目必须出现在server.log中:AMQ221027: Bridge ClusterConnectionBridge@63549ead [name=sf.my-cluster ...] is connected.
See also this answer.