标签:
转载于 http://blog.csdn.net/nimmy/article/details/6247289
近日因工作关系,在研究JMS,使用ActiveMQ作为提供者,考虑到消息的重要,拟采用ActiveMQ的集群,网上查询,资料很少,且语焉不详,最后还是看Apache提供的官方文档(俺E文不好,楞是拿金山词霸一个一个单词的看,累啊),终于做出来了,于是形诸于文,以供有需要的朋友参考
本文阐述了使用 Pure Master Slave 方式 以及 JDBC Master Slave 方式s解决单点故障的问题
关于ActiveMQ集群
1) Java_home : 指向 $java_dir$
2) Ant_home :指向 $ant_dir$
3) Path : 应包含 %java_home%/bin
1) <broker brokerName="pure_master" …
2) <kahaDB directory="${activemq.base}/data/kahadb_pure_master "/>
3) <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
1) <broker brokerName="pure_slave" masterConnectorURI="tcp://0.0.0.0:61616"
shutdownOnMasterFailure="false" …
2) <kahaDB directory="${activemq.base}/data/kahadb_pure_slave "/>
3) <transportConnector name="openwire" uri="tcp://0.0.0.0:61617"/>
1) 启动Master:$activemq_dir$/bin>activemq xbean:file:../conf/pure_master.xml
2) 启动Slave:$activemq_dir$/bin>activemq xbean:file:../conf/pure_slave.xml
public static void main(String[] args) throws Exception {
ConnectionFactory cf = new ActiveMQConnectionFactory("failover:(tcp://0.0.0.0:61616,tcp://0.0.0.0:61617)");
Connection conn = cf.createConnection();
conn.start();
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue qq = new ActiveMQQueue("qq1");
MessageProducer prod = sess.createProducer(qq);
Message msg = null;
Scanner scan = new Scanner(System.in);
String str = scan.next();
while(true) {
msg = sess.createTextMessage(str);
prod.send(msg);
if(str.equals("exit")) {
break;
}
str = scan.next();
}
conn.close();
}
public static void main(String[] args) throws Exception {
ConnectionFactory cf = new ActiveMQConnectionFactory("failover:( tcp://0.0.0.0:61616,tcp://0.0.0.0:61617)");
Connection conn = cf.createConnection();
conn.start();
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue qq = new ActiveMQQueue("qq1");
MessageConsumer cs = sess.createConsumer(qq);
TextMessage msg = (TextMessage)cs.receive();
String str = msg.getText();
while(true) {
System.out.println("receive msg:/t"+msg.getText());
if(str.equals("exit")) {
break;
}
msg = (TextMessage)cs.receive();
str = msg.getText();
}
conn.close();
}
Pure Master Slave模式实现方式简单,可以实现消息的双机热备功能;队列可以实现消息的异步和点对点发送
1) <broker brokerName=" jdbc_broker01" …
2) <!--配置持久适配器-->
<persistenceAdapter>
<jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#mysql-ds"/>
</persistenceAdapter>
3) <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
4) <!--配置数据源:注意是在broker标记之外-->
…
</broker>
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/test?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="maxActive" value="200"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
1) <broker brokerName=" jdbc_broker02" …
2) <transportConnector name="openwire" uri="tcp://0.0.0.0:61617"/>
1) <broker brokerName=" jdbc_broker03" …
2) <transportConnector name="openwire" uri="tcp://0.0.0.0:61618"/>
ConnectionFactory cf = new ActiveMQConnectionFactory("failover:(tcp://0.0.0.0:61616,tcp://0.0.0.0:61617,tcp://0.0.0.0:61618)");
JDBC Master Slave模式实现方式稍微复杂一点,可以实现消息的多点热备功能,Master、Slave的交替完全是即时的,自动的,无需重启任何broker;队列可以实现消息的异步和点对点发送
标签:
原文地址:http://www.cnblogs.com/tbyang/p/4290203.html