static networkConnector是用于创建一个静态的配置对于网络中的多个Broker做集群,这种协议用于复合url,一个复合url包括多个url地址。

<networkConnectors>
<networkConnector name="local network" duplex="true"
uri="static://(tcp://192.168.174.104:61616,tcp://192.168.174.104:61676)"/>
</networkConnectors>

常用networkConnector配置的可用属性:

  conduitSubscriptions :默认true,是否把同一个broker的多个consumer当做一个来处理

  duplex :默认false,设置是否能双向通信

消息发送代码
public class JmsSend {

    public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.174.104:61616");
Connection connection = connectionFactory.createConnection(); connection.start(); Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination queue=session.createQueue("my-queue4"); MessageProducer producer=session.createProducer(queue); for(int i=0 ; i<20 ; i++){
TextMessage message=session.createTextMessage("message"+i);
//message.setStringProperty("queue", "queue"+i);
//message.setJMSType("1");
producer.send(message);
}
session.commit();
session.close(); connection.close(); } }
192.168.174.104:61616 broker1 接收测试代码
public class JmsReceiver1 {
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://192.168.174.104:61616"); for (int i=0; i<10 ;i++){
new Myhread1(connectionFactory).start(); Thread.sleep(1000); } } } class Myhread1 extends Thread { private ConnectionFactory connectionFactory ; public Myhread1(ConnectionFactory connectionFactory) {
super();
this.connectionFactory = connectionFactory;
} public void run() { try { final Connection connection = connectionFactory.createConnection();
connection.start(); final Session session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE); Destination queue = session.createQueue("my-queue4"); MessageConsumer consumer = session.createConsumer(queue); consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
try {
System.out.println("1======"+msg.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
session.commit();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
session.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}); } catch (JMSException e) {
e.printStackTrace();
} } }
192.168.174.104:61676 broker2 接收测试代码
public class JmsReceiver2 {
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://192.168.174.104:61676"); for (int i=0; i<10 ;i++){
new Myhread2(connectionFactory).start(); Thread.sleep(1000); } } } class Myhread2 extends Thread { private ConnectionFactory connectionFactory ; public Myhread2(ConnectionFactory connectionFactory) {
super();
this.connectionFactory = connectionFactory;
} public void run() { try { final Connection connection = connectionFactory.createConnection();
connection.start(); final Session session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE); Destination queue = session.createQueue("my-queue4"); MessageConsumer consumer = session.createConsumer(queue); consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
try {
System.out.println("2======"+msg.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
session.commit();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
session.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}); } catch (JMSException e) {
e.printStackTrace();
} } }

“丢失”的消息

broker1和broker2通过networkConnector连接,一些consumers连接到broker2,消费broker1上的消息。消息先被broker2从broker1上消费掉,然后转发给这些consumers。不幸的是转发部分消息的时候broker2重启了,这些consumers发现broker2连接失败,通过failover连接到broker1上去了,但是有一部分他们还没有消费的消息被broker1已经分发到了broker2上去了。这些消息,就好像是消失了。

broker1 中my-queue4 接收到20条消息。

broker1通过静态网络与broker2连接,与broker2相连的消费者消费后,broker1中Number of Pending Messages为0,即消息先被broker2从broker1上消费掉。

一些consumers连接到broker1,没法从broker1获取消息消费。

针对“丢失”的消息,配置replayWhenNoConsumers选项

这个选项使得broker1上有需要转发的消息但是没有消费者时,把消息回流到它原始的broker。同时把enableAudit设置为false,为了防止消息回流后被当做重复消息而不被分发。

<policyEntries>
<policyEntry queue=">" enableAudit="false">
<networkBridgeFilterFactory>
<conditionalNetworkBridgeFilterFactory replayWhenNoConsumers="true"/>
</networkBridgeFilterFactory>
</policyEntry>
</policyEntries>

容错的链接--Failover

Failover协议实现了自动重新链接的逻辑。默认情况下,这种协议用于随机的去选择一个链接去链接,如果链接失败了,那么会链接到其他
的Broker上。默认的配置定义了延迟重新链接,意味着传输将会在10秒后自动的去重新链接可用的broker。当然所有的重新链接参数都可以根据应用的需要而配置。

ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("failover:(tcp://192.168.174.104:61616,tcp://192.168.174.104:61676)?randomize=false");
randomize:使用随机链接,以达到负载均衡的目的,默认true。

ActiveMQ的静态网络配置的更多相关文章

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

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

  2. ActiveMQ 集群配置 高可用

    自从activemq5.9.0开始,activemq的集群实现方式取消了传统的Pure Master Slave方式,增加了基于zookeeper+leveldb的实现方式,其他两种方式:目录共享和数 ...

  3. ActiveMQ Cluster (ActiveMQ 集群) 配置

    构建高可用的ActiveMQ系统在生产环境中是非常重要的,对于这个apache的消息中间件实现高可用非常简单,只要在Apache ActiveMQ单点基本配置基础上做一次配置变更(如果在一台设备上部署 ...

  4. centos7静态网络配置

    centos7静态网络配置 cd /etc/sysconfig/network-scripts 找到当前网卡名字 vim ifcfg-ens33 TYPE="Ethernet" # ...

  5. spring+activemq实战之配置监听多队列实现不同队列消息消费

    摘选:https://my.oschina.net/u/3613230/blog/1457227 摘要: 最近在项目开发中,需要用到activemq,用的时候,发现在同一个项目中point-to-po ...

  6. linux下activemq安装与配置activemq-5.15.2

    linux下activemq安装与配置 前提 配置好jdk环境   一.下载:apache-activemq-5.15.2-bin.tar.gz https://archive.apache.org/ ...

  7. ActiveMQ的静态网络链接

    -------------------------------------------------------------------- (1)ActiveMQ的networkConnector是什么 ...

  8. ActiveMQ (二) 常用配置简介

    ActiveMQ的主要配置文件 ActiveMQ的一些常用的属性很多可以在对应的配置文件中进行配置的.比如访问web console的管理端的端口,用户名密码,连接MQ时的用户名和密码,持久化设置,是 ...

  9. linux下activemq安装与配置

    一.下载:apache-activemq-5.14.0-bin.tar.gz http://activemq.apache.org/activemq-5140-release.html   二.安装a ...

随机推荐

  1. js的一些笔试面试题

    1. 判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /^[a-zA-Z][a-zA-Z_0-9]{4,19}$/; reg.test ...

  2. 虚拟机VMware安装及Linux系统基础配置(CentOS 7)

    PS: 我是 VMware 14 安装 CentOS 7 来配置Linux虚拟机,想要安装 Ubuntu 18.04 的可以自行其他搜或者参考博客:https://blog.csdn.net/gith ...

  3. LDAP的filter查询详解

    转: 等于(EQUAL TO):  =大于等于(Greater than):  >=小于等于(Less than):  <=通配符(wildcard):  *  逻辑运算符:逻辑与(log ...

  4. python: 关于解决'\u'开头的字符串转中文的方法

    爬虫爬到的内容是这样的: 如果直接打印出来是这样的: python3的解决办法:字符串.encode('utf-8').decode('unicode_escape') python2:字符串.dec ...

  5. Java List 和 Array 转化

    List to Array List 提供了toArray的接口,所以可以直接调用转为object型数组 List<String> list = new ArrayList<Stri ...

  6. keras 在train_on_batch中启用tensorboard

    def write_log(callback, names, logs, batch_no): for name, value in zip(names, logs): summary = tf.Su ...

  7. 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_03-用户认证-认证服务查询数据库-查询用户接口-接口定义

    1.2.4 查询用户接口 完成用户中心根据账号查询用户信息接口功能. 在ucenter这个服务里面定义查询用户信息的接口 这个接口在auth的服务的loadUserByUserName这个方法里面被调 ...

  8. 123457123456#0#-----com.twoapp.HuaXueGame01--前拼后广--儿童滑雪大冒险游戏jiemei

    com.twoapp.HuaXueGame01--前拼后广--儿童滑雪大冒险游戏jiemei

  9. java中快速读写图片到BufferedImage对象

    java7读取文件到BufferedImage对象 BufferedImage bufferedImage = ImageIO.read(Files.newInputStream(Paths.get( ...

  10. Delphi分割字符串函数Split源码

    function TStringHelper.Split(const Separator: array of string; Count: Integer; Options: TStringSplit ...