ActiveMQ有哪些集群部署方式
- Master-Slave 部署方式
- Broker-Cluster 部署方式
- Master-Slave与Broker-Cluster相结合的部署方式
延时、调度消息实现
延时、调度消息 官方文档http://activemq.apache.org/delay-and-schedule-message-delivery.html
。
定时发送邮件通知,或者触发代码执行。
- 服务端调整,在
/var/activemq/conf/activemq.xml
中的 broker节点上schedulerSupport="true"
不配置的话就不支持调度的方式。 - 发送的消息也要进行调整。
服务端代码:
/ 延时、调度消息 http://activemq.apache.org/delay-and-schedule-message-delivery.html
// 定时发送邮件通知,或者触发代码执行
public class DelayScheduleMessageDemo {
public static void main(String[] args) {
new ProducerThread("tcp://activemq.tony.com:61616", "queue1").start();
}
static class ProducerThread extends Thread {
String brokerUrl;
String destinationUrl;
public ProducerThread(String brokerUrl, String destinationUrl) {
this.brokerUrl = brokerUrl;
this.destinationUrl = destinationUrl;
}
@Override
public void run() {
ActiveMQConnectionFactory connectionFactory;
Connection conn;
Session session;
try {
// 1、创建连接工厂
connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
// 2、创建连接对象md
conn = connectionFactory.createConnection();
conn.start();
// 3、创建会话
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 4、创建点对点发送的目标
Destination destination = session.createQueue(destinationUrl);
// 5、创建生产者消息
MessageProducer producer = session.createProducer(destination);
// 设置生产者的模式,有两种可选 持久化 / 不持久化
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
// 6、示例消息
// 延时 5秒
TextMessage message = session.createTextMessage("Hello world - 1!");
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 5 * 1000L);
// 延时 5秒,投递3次,间隔10秒 (投递次数=重复次数+默认的一次)
TextMessage message2 = session.createTextMessage("Hello world - 2!");
message2.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 5 * 1000L); // 延时
message2.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 2 * 1000L); // 投递间隔
message2.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 2); // 重复次数
// CRON 表达式的方式 以及 和上面参数的组合
TextMessage message3 = session.createTextMessage("Hello world - 3!");
message3.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "0 * * * *");
// 7、发送消息
producer.send(message);
producer.send(message2);
producer.send(message3);
// 8、 关闭连接
session.close();
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
文档查询:
消息属性 http://activemq.apache.org/activemq-message-properties.html
activemq内部消息 http://activemq.apache.org/advisory-message.html
大文件大对象 http://activemq.apache.org/blob-messages.html
延时消息 http://activemq.apache.org/delay-and-schedule-message-delivery.html
发送java对象 http://activemq.apache.org/message-transformation.html
http://activemq.apache.org/objectmessage.html
发送java集合 http://activemq.apache.org/structured-message-properties-and-mapmessages.html
Shared filesystem Master-Slave部署方式
利用NAS进行一个文件的磁盘共享。
Shared database Master-Slave部署方式 —单点故障问题
- 消息的持久化
- 搭建一个主从的集群,同时还要求支持故障的转移
集群配置参考 http://activemq.apache.org/clustering.html
数据库共享方案
1、 使用数据库进行消息持久化
1.1、引入数据库驱动包和数据库连接池
根据需要,把数据库驱动放到activemq目录下 lib/extra
如:mysql-connector-java-5.1.41.jar
1.2、修改activemq.xml,使用jdbc持久化
# /var/activemq/conf/activemq.xml 文件 persistenceAdapter节点
<!-- persistent=true-->
<broker brokerName="localhost" persistent="true" xmlns="http://activemq.apache.org/schema/core">
<persistenceAdapter>
<!-- 这里是关键 -->
<jdbcPersistenceAdapter dataSource="#mysql-ds" useDatabaseLock="false" transactionIsolation="4"/>
</persistenceAdapter>
........
</broker>
<!-- MySql DataSource Sample Setup -->
<bean id="mysql-ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://database.tony.com:3306/test_activemq?relaxAutoCommit=true"/>
<property name="username" value="tony"/>
<property name="password" value="tony"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
2、集群配置
多台服务器部署启动activemq服务,使用同一个数据库
3、客户端使用 http://activemq.apache.org/failover-transport-reference.html
# brokerURI 使用failover,故障自动切换方式
# 非failover的公共参数配置通过nested.*,例如 failover:(...)?nested.wireFormat.maxInactivityDuration=1000
# ?randomize=false 随机选择,默认是顺序
# 指定优先切换 failover:(tcp://host1:61616,tcp://host2:61616,tcp://host3:61616)?priorityBackup=true&priorityURIs=tcp://local1:61616,tcp://local2:61616
# maxReconnectDelay重连的最大间隔时间(毫秒)
brokerUrl = "failover:(tcp://activemq.tony.com:61616,tcp://activemq-slave.tony.com:61616)?initialReconnectDelay=100";
4、原理简述
1、 数据库表自动创建
2、 多服务器争抢获取LOCK表锁
3、 连接断开后,客户端自动重连
Replicated LevelDB Store方式(弃用)
Broker-Cluster部署方式
Static Broker-Cluster 部署配置
Dynamic Broker-Cluster 部署配置
Master-Slave与Broker-Cluster结合
Master-Slave与Broker-Cluster结合部署配置
网络连接器配置说明
高可用集群官方文档
http://activemq.apache.org/clustering.html
1、 基于jdbc
2、 基于kahaDB
3、 http://activemq.apache.org/replicated-message-store.html
http://activemq.apache.org/pure-master-slave.html
http://activemq.apache.org/networks-of-brokers.html