ActiveMQ的集群
Queue consumer clusters
              ActiveMQ支持 Consumer对消息高可靠性的负载平衡消费,如果一个 Consumer死掉,
该消息会转发到其它的 Consumer消费的 Queue上。如果一个 Consumer获得消息比其它
Consumer快,那么他将获得更多的消息。

因此推荐 ActiveMQ的 Broker和Client使用failover://transport的方式来配置链接。

Broker clusters
            大部情况下是使用一系列的 Broker和 Client链接到一起。如果一个 Broker死掉了
Client可以自动链接到其它 Broke上。实现以上行为需要用 failover协议作为 Client。
           如果启动了多个 Broker, Client可以使用 static discover或者 Dynamic discovery
容易的从一个 broker到另一个 broker直接链接。
           这样当一个 broker上没有 Consumer的话,那么它的消息不会被消费的,然而该
broker会通过存储和转发的策略来把该消息发到其它 broker上。
           特别注意: ActiveMQ默认的两个 broker, static链接后是单方向的, broker-A可以
访问消费 broker-B的消息,如果要支持双向通信,需要在 netWorkConnector配置的时候,
设置duplex=true就可以了。

如图所示:

把b1和b2当成一个集群,c1和c2为两个客服端。

配置如下:

  <networkConnectors>
<networkConnector name="local network" duplex="true" conduitSubscriptions="false"
uri="static://(tcp://192.168.145.100:61616,tcp://192.168.145.100:61676)"/>
</networkConnectors>
conduitSubscriptions="false" 默认为true. 

代码如下:

package test.mq.staitsnetwork;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class Sender {
public static void main(String[] args) throws JMSException, InterruptedException {
ConnectionFactory ConnectionFactory=new ActiveMQConnectionFactory(
//"tcp://192.168.145.100:61616");
"failover:(tcp://192.168.145.100:61616,tcp://192.168.145.100:61676)?randomize=true");
Connection connection=ConnectionFactory.createConnection();
connection.start(); Session session=connection.createSession(Boolean.TRUE, Session.CLIENT_ACKNOWLEDGE);
Destination destination=session.createQueue("my_queue");
MessageProducer Producer=session.createProducer(destination); for(int i=;i<;i++){
TextMessage message=session.createTextMessage("message----"+i);
//Thread.sleep(1000);
Producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
package test.mq.staitsnetwork;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class QReceiver1{ public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(
"tcp://192.168.145.100:61616"
);
Connection connection = connectionFactory.createConnection();
connection.start();
final Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination=session.createQueue("my_queue");
for(int i=;i<;i++){
MessageConsumer Consumer=session.createConsumer(destination);
Consumer.setMessageListener(new MessageListener() { @Override
public void onMessage(Message msg) {
TextMessage txtmsg=(TextMessage) msg;
try {
System.out.println("接收信息--->"+txtmsg.getText());
session.commit();
} catch (JMSException e1) {
e1.printStackTrace();
} }
});
} }
}
package test.mq.staitsnetwork;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class QReceiver2{ public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(
"tcp://192.168.145.100:61676"
);
Connection connection = connectionFactory.createConnection();
connection.start();
final Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination=session.createQueue("my_queue");
for(int i=;i<;i++){
MessageConsumer Consumer=session.createConsumer(destination);
Consumer.setMessageListener(new MessageListener() { @Override
public void onMessage(Message msg) {
TextMessage txtmsg=(TextMessage) msg;
try {
System.out.println("接收信息--->"+txtmsg.getText());
session.commit();
} catch (JMSException e1) {
e1.printStackTrace();
} }
});
} }
}

分布式-信息方式-ActiveMQ的集群的更多相关文章

  1. 分布式-信息方式-ActiveMQ基础

    ActiveMQ简介 ActiveMQ是什么ActiveMQ是Apache推出的,一款开源全支持JMS.1和J2EE1.4范的JMS Provider实现的信息中间件.(message oriente ...

  2. 分布式-信息方式-ActiveMQ的静态网络连接

                           ActiveMQ的静态网络连接 在一台服务器上启动多个Broker步骤如下:1:把整个conf文件夹复制一份,比如叫做conf22:修改里面的 activ ...

  3. 分布式-信息方式-ActiveMQ静态网络连接多线程的consumer(消费者)访问集群

    操作如下: 1:把整个conf文件夹复制一份,比如叫做conf22:修改里面的 activemq.xml文件(1)里面的 brokerName不能跟原来的重复(2)数据存放的文件名称不能重复,比如:& ...

  4. 分布式-信息方式-ActiveMQ的Message dispatch高级特性之(指针) Message cursors

    Message dispatch高级特性之 Message cursors概述            ActiveMQ发送持久消息的典型处现方式是:当消息的消费者准备就绪时,消息发送系统把存储的 消息 ...

  5. 分布式-信息方式-ActiveMQ的Destination高级特性3

    虚拟destination用来创建逻辑destination,客户端可以通过它来生产和消费消息,它会把消息映射到物理destination. ActiveMQ支持2种方式: 1:虚拟主题(Virtua ...

  6. 分布式-信息方式-ActiveMQ的Destination高级特性1

    ActiveMQ的Destination高级特性 Destination高级特性----->Composite Destinations 组合队列Composite Destinations : ...

  7. 分布式-信息方式-ActiveMQ静态网络连接的容错

    容错的链接Failover Protocol 前面讲述的都是client配置链接到指定的 broker上.但是,如果 Broker的链接失败怎么办呢?此时, Client有两个选项:要么立刻死掉,要么 ...

  8. 分布式-信息方式-ActiveMQ的消息存储持久化

    ActiveMQ的消息存储持久化■概述ActiveMQ不仅支持 persistent和 non-persistent两种方式,还支持消息的恢复( recovery)方式PTPQueue的存储是很简单的 ...

  9. 分布式-信息方式-ActiveMQ的Destination高级特性2

    使用filtered destinations,在xml配置如下: <destinationInterceptors> <virtualDestinationInterceptor& ...

随机推荐

  1. JDK8~13新特性概览

    JDK8 1. 接口default 与 static 关键字 /** * jdk8中接口可以使用声明default和static修饰的方法 * static修饰的方法和普通的方法一样,可以被直接调用 ...

  2. Task的取消

    原文:.NET 4 并行(多核)编程系列之三 从Task的取消 .NET 4 并行(多核)编程系列之三 从Task的取消 前言:因为Task是.NET 4并行编程最为核心的一个类,也我们在是在并行编程 ...

  3. POJ题解Sorting It All Out-传递丢包+倍增

    题目链接: http://poj.org/problem?id=1094 题目大意(直接从谷歌翻译上复制下来的): 描述 不同值的递增排序顺序是其中使用某种形式的小于运算符来将元素从最小到最大排序的顺 ...

  4. centos查看实时网络带宽占用情况方法【转】

    Linux中查看网卡流量工具有iptraf.iftop以及nethogs等,iftop可以用来监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息等. centos安装iftop的命令如下: ...

  5. 使用pagehelper分页工具page警告问题

    警告: Hessian/Burlap: 'com.github.pagehelper.Page' is an unknown class in WebappClassLoader java.lang. ...

  6. 关于页面多个ajax请求阻塞的问题

    最近遇到一个问题,我的一个页面有多个ajax请求,但是一个很快的请求却需要很长时间才返回,而且慢于一个耗时比较长的请求,我在考虑是不是有ajax异步并发有问题,但是查询了一些资料,ajax不存在这样的 ...

  7. laravel 使用 intervention/image 的注意方法

    出错NotSupportedException in AbstractEncoder.php line 151: Encodingformat (tmp) is not supported. 这个只是 ...

  8. 【异常】Reason: Executor heartbeat timed out after 140927 ms

    1 详细异常 ERROR scheduler.JobScheduler: Error running job streaming job ms. org.apache.spark.SparkExcep ...

  9. 16、Nginx Rewrite重写

    1.Rewrite基本概述 1.1.什么是rewrite Rewrite主要实现url地址重写, 以及地址重定向,就是将用户请求web服务器的地址重新定向到其他URL的过程. 1.2.Rewrite使 ...

  10. ShuffleNet系列学习笔记

    ShuffleNet是旷世提出的高效轻量化网络,是一款很值得一提的轻量化网络,其相关论文也是很有价值的. ShuffleNet V1 该网络提出于2017年,论文为<ShuffleNet: An ...