在 ActiveMQ 中,broker 集中管理持久的、临时的 queue 和 topic。

public class BrokerFilter implements Broker {
// 省略其他代码
protected final Broker next;
}

最终的 Broker 链是这样的:

StatisticsBroker -> TransactionBroker -> CompositeDestinationBroker -> AdvisoryBroker -> ManagedRegionBroker

创建Broker 链:

// org.apache.activemq.broker.BrokerService
protected Broker createBroker() throws Exception {
// 创建 RegionBroker
regionBroker = createRegionBroker();
// 添加拦截器
Broker broker = addInterceptors(regionBroker);
// Add a filter that will stop access to the broker once stopped
broker = new MutableBrokerFilter(broker) {
Broker old; @Override
public void stop() throws Exception {
old = this.next.getAndSet(new ErrorBroker("Broker has been stopped: " + this) {
// Just ignore additional stop actions.
@Override
public void stop() throws Exception {
}
});
old.stop();
} @Override
public void start() throws Exception {
if (forceStart && old != null) {
this.next.set(old);
}
getNext().start();
}
};
return broker;
}

创建 RegionBroker:

protected Broker createRegionBroker(DestinationInterceptor destinationInterceptor) throws IOException {
RegionBroker regionBroker;
if (isUseJmx()) {
try {
regionBroker = new ManagedRegionBroker(this, getManagementContext(), getBrokerObjectName(),
getTaskRunnerFactory(), getConsumerSystemUsage(), destinationFactory, destinationInterceptor,getScheduler(),getExecutor());
} catch(MalformedObjectNameException me){
LOG.warn("Cannot create ManagedRegionBroker due " + me.getMessage(), me);
throw new IOException(me);
}
} else {
regionBroker = new RegionBroker(this, getTaskRunnerFactory(), getConsumerSystemUsage(), destinationFactory,
destinationInterceptor,getScheduler(),getExecutor());
}
destinationFactory.setRegionBroker(regionBroker);
regionBroker.setKeepDurableSubsActive(keepDurableSubsActive);
regionBroker.setBrokerName(getBrokerName());
regionBroker.getDestinationStatistics().setEnabled(enableStatistics);
regionBroker.setAllowTempAutoCreationOnSend(isAllowTempAutoCreationOnSend());
if (brokerId != null) {
regionBroker.setBrokerId(brokerId);
}
return regionBroker;
}

为RegionBroker添加BrokerFilter:

protected Broker addInterceptors(Broker broker) throws Exception {
if (isSchedulerSupport()) {
SchedulerBroker sb = new SchedulerBroker(this, broker, getJobSchedulerStore());
if (isUseJmx()) {
JobSchedulerViewMBean view = new JobSchedulerView(sb.getJobScheduler());
try {
ObjectName objectName = BrokerMBeanSupport.createJobSchedulerServiceName(getBrokerObjectName());
AnnotatedMBean.registerMBean(getManagementContext(), view, objectName);
this.adminView.setJMSJobScheduler(objectName);
} catch (Throwable e) {
throw IOExceptionSupport.create("JobScheduler could not be registered in JMX: "
+ e.getMessage(), e);
}
}
broker = sb;
}
if (isUseJmx()) {
HealthViewMBean statusView = new HealthView((ManagedRegionBroker)getRegionBroker());
try {
ObjectName objectName = BrokerMBeanSupport.createHealthServiceName(getBrokerObjectName());
AnnotatedMBean.registerMBean(getManagementContext(), statusView, objectName);
} catch (Throwable e) {
throw IOExceptionSupport.create("Status MBean could not be registered in JMX: "
+ e.getMessage(), e);
}
}
if (isAdvisorySupport()) {
broker = new AdvisoryBroker(broker);
}
broker = new CompositeDestinationBroker(broker);
broker = new TransactionBroker(broker, getPersistenceAdapter().createTransactionStore());
if (isPopulateJMSXUserID()) {
UserIDBroker userIDBroker = new UserIDBroker(broker);
userIDBroker.setUseAuthenticatePrincipal(isUseAuthenticatedPrincipalForJMSXUserID());
broker = userIDBroker;
}
if (isMonitorConnectionSplits()) {
broker = new ConnectionSplitBroker(broker);
}
if (plugins != null) {
for (int i = 0; i < plugins.length; i++) {
BrokerPlugin plugin = plugins[i];
broker = plugin.installPlugin(broker);
}
}
return broker;
}

我们创建的topic和queue都记录在ManagedRegionBroker这个类中:

public class ManagedRegionBroker extends RegionBroker {
private static final Logger LOG = LoggerFactory.getLogger(ManagedRegionBroker.class);
private final ManagementContext managementContext;
private final ObjectName brokerObjectName;
private final Map<ObjectName, DestinationView> topics
= new ConcurrentHashMap<ObjectName, DestinationView>();
private final Map<ObjectName, DestinationView> queues
= new ConcurrentHashMap<ObjectName, DestinationView>();
private final Map<ObjectName, DestinationView> temporaryQueues
= new ConcurrentHashMap<ObjectName, DestinationView>();
private final Map<ObjectName, DestinationView> temporaryTopics
= new ConcurrentHashMap<ObjectName, DestinationView>();
private final Map<ObjectName, SubscriptionView> queueSubscribers
= new ConcurrentHashMap<ObjectName, SubscriptionView>();
private final Map<ObjectName, SubscriptionView> topicSubscribers
= new ConcurrentHashMap<ObjectName, SubscriptionView>();
private final Map<ObjectName, SubscriptionView> durableTopicSubscribers
= new ConcurrentHashMap<ObjectName, SubscriptionView>();
private final Map<ObjectName, SubscriptionView> inactiveDurableTopicSubscribers
= new ConcurrentHashMap<ObjectName, SubscriptionView>();
private final Map<ObjectName, SubscriptionView> temporaryQueueSubscribers
= new ConcurrentHashMap<ObjectName, SubscriptionView>();
private final Map<ObjectName, SubscriptionView> temporaryTopicSubscribers
= new ConcurrentHashMap<ObjectName, SubscriptionView>();
private final Map<ObjectName, ProducerView> queueProducers
= new ConcurrentHashMap<ObjectName, ProducerView>();
private final Map<ObjectName, ProducerView> topicProducers
= new ConcurrentHashMap<ObjectName, ProducerView>();
private final Map<ObjectName, ProducerView> temporaryQueueProducers
= new ConcurrentHashMap<ObjectName, ProducerView>();
private final Map<ObjectName, ProducerView> temporaryTopicProducers
= new ConcurrentHashMap<ObjectName, ProducerView>();
private final Map<ObjectName, ProducerView> dynamicDestinationProducers
= new ConcurrentHashMap<ObjectName, ProducerView>();
private final Map<SubscriptionKey, ObjectName> subscriptionKeys
= new ConcurrentHashMap<SubscriptionKey, ObjectName>();
private final Map<Subscription, ObjectName> subscriptionMap
= new ConcurrentHashMap<Subscription, ObjectName>();
private final Set<ObjectName> registeredMBeans
= new CopyOnWriteArraySet<ObjectName>();
/* This is the first broker in the broker interceptor chain. */
private Broker contextBroker;
}

ActiveMQ broker解析的更多相关文章

  1. ActiveMQ broker 集群, 静态发现和动态发现

    下载 activemq 压缩包解压后,conf 目录下有各种示例配置文件,红线标出的是静态发现和动态发现的配置. 1. 静态配置 启动3个 broker,端口分别为61616,61618,61620, ...

  2. ActiveMQ broker和客户端之间的确认

    生产者发送消息:producer ---------> broker broker返回确认:broker ---------> producer 生产者发送同步消息,broker会返回Re ...

  3. ActiveMQ broker 非持久化queue消息的入队、出队和应答

    消息入队:Queue.doMessageSend 消息分发:Queue.doActualDispatch 消息发送:TransportConnection.dispatch broker收到consu ...

  4. ActiveMQ中Broker的应用与启动方式

    Broker:英语有代理的意思,在activemq中,Broker就相当于一个Activemq实例. 1. 命令行启动实例: 1.activemq start使用默认的activemq.xml启动 E ...

  5. ActiveMQ producer不断发送消息,会导致broker内存耗尽吗?

    http://activemq.apache.org/my-producer-blocks.html 回答了这个问题: ActiveMQ 5.x 支持Message Cursors,它默认把消息从内存 ...

  6. ActiveMQ学习笔记(5)----Broker的启动方式

    Broker:相当于一个ActiveMQ服务器实例,在实际的开发中我们可以启动多个Broker. 命令行启动参数示例如下: 1. activemq start 使用默认的activemq.xml来启动 ...

  7. C++ activemq CMS 学习笔记.

    很早前就仓促的接触过activemq,但当时太赶时间.后面发现activemq 需要了解的东西实在是太多了. 关于activemq 一直想起一遍文章.但也一直缺少自己的见解.或许是网上这些文章太多了. ...

  8. 消息中间件--ActiveMQ&JMS消息服务

    ### 消息中间件 ### ---------- **消息中间件** 1. 消息中间件的概述 2. 消息中间件的应用场景 * 异步处理 * 应用解耦 * 流量削峰 * 消息通信   --------- ...

  9. 基于zookeeper的activemq的主从集群配置

    项目,要用到消息队列,这里采用activemq,相对使用简单点.这里重点是环境部署. 0. 服务器环境 RedHat710.90.7.210.90.7.1010.90.2.102 1. 下载安装zoo ...

随机推荐

  1. 转 Failed to run the WC DB work queue associated with 错误的解决

    svn 异常终止导致的缓存工作队列问题 解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.db 3.将sqlite3.exe放到.svn的同级 ...

  2. tornado关于AsyncHTTPClient的使用笔记

    先来一段同步的httpclient使用代码 url = 'https://www.baidu.com/' http_client = HTTPClient() response = http_clie ...

  3. win10 安装keras

    1.安装Python环境 建议安装Anconda3 ,4.2.0版本 下载地址: https://repo.continuum.io/archive/index.html 或 https://mirr ...

  4. python中while循环运算符及格式化输出

    一,while循环 while 条件: while语句块(循环体) 运行: 判断你给的条件是否为真,如果真则执行循环体.否则跳出循环. 执行完循环体之后再次判断条件是否为真 例子1 我们玩联盟的时候喷 ...

  5. JDBC连接数据库的安全性连接方法

    PreparedStatement ps=null; ResultSet rs=null; Connection ct=null; try { Class.forName("com.mysq ...

  6. 越来越“简单”的Java

    Java,20岁了.从我写下第一行Java代码,迄今已有十余年了,眼见Java——这个当年刚刚找到自己成长方向的懵懂少年,成长为如今当之无愧的业界王者.它已拥有世界上最庞大的开发者社区,以及无可匹敌的 ...

  7. Zabbix报警执行远程命令

    日常Zabbix报警案例中,大多都是报警发送邮件,场景里很少有需要报警后执行一个命令(启动服务.清空磁盘空间.关停服务器);而今天就给大家讲讲最近需要做的事:报警后执行远程命令 刚好zabbix动作中 ...

  8. 《剑指offer》第四十六题(把数字翻译成字符串)

    // 面试题46:把数字翻译成字符串 // 题目:给定一个数字,我们按照如下规则把它翻译为字符串:0翻译成"a",1翻 // 译成"b",……,11翻译成&qu ...

  9. ubuntu下安装anaconda

    1.  到官网http://continuum.io/downloads下载anaconda. 选择linux64-bit-python2.7 2.  安装anaconda,在终端输入:cd ~/Do ...

  10. 第 6 章 存储 - 038 - Docker 的两类存储资源

    存储资源 Docker 为容器提供了两种存放数据的资源: 由 storage driver 管理的镜像层和容器层 Data Volume 1.storage driver 容器由最上面一个可写的容器层 ...