ActiveMQ的静态网络配置
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的静态网络配置的更多相关文章
- 分布式-信息方式-ActiveMQ的静态网络连接
ActiveMQ的静态网络连接 在一台服务器上启动多个Broker步骤如下:1:把整个conf文件夹复制一份,比如叫做conf22:修改里面的 activ ...
- ActiveMQ 集群配置 高可用
自从activemq5.9.0开始,activemq的集群实现方式取消了传统的Pure Master Slave方式,增加了基于zookeeper+leveldb的实现方式,其他两种方式:目录共享和数 ...
- ActiveMQ Cluster (ActiveMQ 集群) 配置
构建高可用的ActiveMQ系统在生产环境中是非常重要的,对于这个apache的消息中间件实现高可用非常简单,只要在Apache ActiveMQ单点基本配置基础上做一次配置变更(如果在一台设备上部署 ...
- centos7静态网络配置
centos7静态网络配置 cd /etc/sysconfig/network-scripts 找到当前网卡名字 vim ifcfg-ens33 TYPE="Ethernet" # ...
- spring+activemq实战之配置监听多队列实现不同队列消息消费
摘选:https://my.oschina.net/u/3613230/blog/1457227 摘要: 最近在项目开发中,需要用到activemq,用的时候,发现在同一个项目中point-to-po ...
- linux下activemq安装与配置activemq-5.15.2
linux下activemq安装与配置 前提 配置好jdk环境 一.下载:apache-activemq-5.15.2-bin.tar.gz https://archive.apache.org/ ...
- ActiveMQ的静态网络链接
-------------------------------------------------------------------- (1)ActiveMQ的networkConnector是什么 ...
- ActiveMQ (二) 常用配置简介
ActiveMQ的主要配置文件 ActiveMQ的一些常用的属性很多可以在对应的配置文件中进行配置的.比如访问web console的管理端的端口,用户名密码,连接MQ时的用户名和密码,持久化设置,是 ...
- linux下activemq安装与配置
一.下载:apache-activemq-5.14.0-bin.tar.gz http://activemq.apache.org/activemq-5140-release.html 二.安装a ...
随机推荐
- 从xml中返回的对象,和new 返回的对象时不同的。
public BigDecimal getTax() { return tax == null ? BigDecimal.ZERO : tax; } 这是自定义的一个类 对null 做出了处理. 但是 ...
- 关于Vmvare虚拟机中Linux系统不能全屏的问题
安装虚拟机后并加载ubuntu后,发现界面一直是正方形的,真是神了. 但是当时沉迷学习,这点小细节并没有什么影响,就没有管它. 嗯.... 现在,为了追求完美,是时候让它全屏了,可无论怎样搞,怎样百度 ...
- 记一次被DDoS敲诈的历程 糖果LUA FreeBuf 今天 0x01 背景
记一次被DDoS敲诈的历程 糖果LUA FreeBuf 今天 0x01 背景
- ubuntu更强大的包管理工具:aptitude
aptitude 与 apt-get 一样,是 Debian 及其衍生系统ubuntu上 一个强大的包管理工具.与 apt-get 不同的是,aptitude 在处理依赖问题上更佳一些.apt ...
- React——相关工具概述
Create a New React App Use an integrated toolchain for the best user and developer experience. This ...
- 源码安装LNMP
需要准备的安装包以及下载地址(只是一个大概地址,版本和下载方式需要自行选择): Nginx http://nginx.org/en/download.html nginx主程序包 MySQL http ...
- thymeleaf动态拼接class
场景:站内消息,一些已读的要区别与未读的. <table class="layui-table"> <thead> <tr> <th la ...
- 【产品】C转B
http://www.woshipm.com/pmd/2780830.html 这篇文章总结的不错.
- 【pep8规范】arc diff 不符合 pep 8 规范
arc land 的时候,arc报错提示代码不符合pep8规范: 1.单行代码过长(括号中的换行不需要加 /) python代码换行加 / https://blog.csdn.net/codechel ...
- 【Leetcode_easy】804. Unique Morse Code Words
problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...