Apache artemis 需要使用 STRICT 配置创建与服务器节点一样多的连接
Apache artemis need to created many connection as many server node with STRICT config
我检查了 clustered-static-discovery,还有基于 udp 的集群,
我所看到的,如果我在集群中有 2 个节点,则必须需要 2 个连接,如果我有 4 个节点,则必须需要 4 个连接才能循环使用消息。
假设我有 2 个服务器需要 2 个连接,如果我只创建了一个连接或侦听器,并且将产生 10 条消息,那么我将错过 5 条消息。
我们如何在一个连接中接收消息而不是创建多个连接(取决于使用了多少个服务器节点)。
因为有运行个时间节点添加的场景,所以我们会错过那些运行个时间节点添加的消息
这里是示例,我有 2 个节点(在集群中)和一个连接
import javax.jms.*;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.core.client.impl.ClientSessionInternal;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.util.ServerUtil;
/**
* A simple example that demonstrates server side load-balancing of messages between the queue instances on different
* nodes of the cluster. The cluster is created from a static list of nodes.
*/
public class StaticClusteredQueueExample {
public static void main(final String[] args) throws Exception {
Connection connection0 = null;
try {
Topic topic = ActiveMQJMSClient.createTopic("exampleTopic");
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:9616");
Thread.sleep(2000);
connection0 = cf0.createConnection();
final String clientID = "admin";
connection0.setClientID(clientID);
final String subscriptionName = "mySub";
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection0.start();
MessageConsumer subscriber0 = session0.createDurableSubscriber(topic, subscriptionName);
Thread.sleep(2000);
MessageProducer producer = session0.createProducer(topic);
// We send 20 messages to server
final int numMessages = 20;
for (int i = 0; i < numMessages; i++) {
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
Thread.sleep(2000);
for (int i = 0; i < numMessages; i += 2) {
try {
TextMessage message0 = (TextMessage) subscriber0.receive(5000);
System.out.println("" + message0.getText() + ": from node " + ServerUtil.getServer(connection0));
} catch (Exception e) {}
}
} finally {
// Step 15. Be sure to close our resources!
if (connection0 != null) {
connection0.close();
}
}
}
}
在上面的示例中,生产者发送了 20 条消息
但是当我打印输出时它只打印 10 条消息而不是 20
如 the clustering documentation 所述,消息在集群中的负载平衡方式取决于 <cluster-connection>
上的 <message-load-balancing>
配置元素。文档指出:
This parameter determines if/how messages will be distributed between other nodes of the cluster. It can be one of three values - OFF
, STRICT
, or ON_DEMAND
(default). This parameter replaces the deprecated forward-when-no-consumers
parameter.
If this is set to OFF
then messages will never be forwarded to another node in the cluster
If this is set to STRICT
then each incoming message will be round robin'd even though the same queues on the other nodes of the cluster may have no consumers at all, or they may have consumers that have non matching message filters (selectors). Note that Apache ActiveMQ Artemis will not forward messages to other nodes if there are no queues of the same name on the other nodes, even if this parameter is set to STRICT
. Using STRICT
is like setting the legacy forward-when-no-consumers
parameter to true
.
If this is set to ON_DEMAND
then Apache ActiveMQ Artemis will only forward messages to other nodes of the cluster if the address to which they are being forwarded has queues which have consumers, and if those consumers have message filters (selectors) at least one of those selectors must match the message. Using ON_DEMAND
is like setting the legacy forward-when-no-consumers
parameter to false
.
Default is ON_DEMAND
.
clustered-static-discovery
示例将 STRICT
用于 <message-load-balancing>
,这就是为什么需要连接到每个节点的原因。
此外,请记住 <redistribution-delay>
会影响此处的行为。您可以在 the clustering documentation.
的“消息重新分发”部分阅读更多相关信息
如果您使用 ON_DEMAND
作为 <message-load-balancing>
和 <redistribution-delay>
即 >=0
那么您将能够使用集群中特定队列上的每条消息到单个节点的连接。
我检查了 clustered-static-discovery,还有基于 udp 的集群, 我所看到的,如果我在集群中有 2 个节点,则必须需要 2 个连接,如果我有 4 个节点,则必须需要 4 个连接才能循环使用消息。
假设我有 2 个服务器需要 2 个连接,如果我只创建了一个连接或侦听器,并且将产生 10 条消息,那么我将错过 5 条消息。
我们如何在一个连接中接收消息而不是创建多个连接(取决于使用了多少个服务器节点)。 因为有运行个时间节点添加的场景,所以我们会错过那些运行个时间节点添加的消息
这里是示例,我有 2 个节点(在集群中)和一个连接
import javax.jms.*;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.core.client.impl.ClientSessionInternal;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.util.ServerUtil;
/**
* A simple example that demonstrates server side load-balancing of messages between the queue instances on different
* nodes of the cluster. The cluster is created from a static list of nodes.
*/
public class StaticClusteredQueueExample {
public static void main(final String[] args) throws Exception {
Connection connection0 = null;
try {
Topic topic = ActiveMQJMSClient.createTopic("exampleTopic");
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:9616");
Thread.sleep(2000);
connection0 = cf0.createConnection();
final String clientID = "admin";
connection0.setClientID(clientID);
final String subscriptionName = "mySub";
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection0.start();
MessageConsumer subscriber0 = session0.createDurableSubscriber(topic, subscriptionName);
Thread.sleep(2000);
MessageProducer producer = session0.createProducer(topic);
// We send 20 messages to server
final int numMessages = 20;
for (int i = 0; i < numMessages; i++) {
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
Thread.sleep(2000);
for (int i = 0; i < numMessages; i += 2) {
try {
TextMessage message0 = (TextMessage) subscriber0.receive(5000);
System.out.println("" + message0.getText() + ": from node " + ServerUtil.getServer(connection0));
} catch (Exception e) {}
}
} finally {
// Step 15. Be sure to close our resources!
if (connection0 != null) {
connection0.close();
}
}
}
}
在上面的示例中,生产者发送了 20 条消息 但是当我打印输出时它只打印 10 条消息而不是 20
如 the clustering documentation 所述,消息在集群中的负载平衡方式取决于 <cluster-connection>
上的 <message-load-balancing>
配置元素。文档指出:
This parameter determines if/how messages will be distributed between other nodes of the cluster. It can be one of three values -
OFF
,STRICT
, orON_DEMAND
(default). This parameter replaces the deprecatedforward-when-no-consumers
parameter.If this is set to
OFF
then messages will never be forwarded to another node in the clusterIf this is set to
STRICT
then each incoming message will be round robin'd even though the same queues on the other nodes of the cluster may have no consumers at all, or they may have consumers that have non matching message filters (selectors). Note that Apache ActiveMQ Artemis will not forward messages to other nodes if there are no queues of the same name on the other nodes, even if this parameter is set toSTRICT
. UsingSTRICT
is like setting the legacyforward-when-no-consumers
parameter totrue
.If this is set to
ON_DEMAND
then Apache ActiveMQ Artemis will only forward messages to other nodes of the cluster if the address to which they are being forwarded has queues which have consumers, and if those consumers have message filters (selectors) at least one of those selectors must match the message. UsingON_DEMAND
is like setting thelegacy forward-when-no-consumers
parameter tofalse
.Default is
ON_DEMAND
.
clustered-static-discovery
示例将 STRICT
用于 <message-load-balancing>
,这就是为什么需要连接到每个节点的原因。
此外,请记住 <redistribution-delay>
会影响此处的行为。您可以在 the clustering documentation.
如果您使用 ON_DEMAND
作为 <message-load-balancing>
和 <redistribution-delay>
即 >=0
那么您将能够使用集群中特定队列上的每条消息到单个节点的连接。