一、参考资料
JMS与ActiveMQ的关系,概念:http://boy00fly.iteye.com/blog/1103586
ActiveMQ的热备份与集群配置:http://www.open-open.com/lib/view/open1400126457817.html
queue与topic代码:http://longdick.iteye.com/blog/465229
实际应用案例:http://www.codeceo.com/article/be-a-polyglot-programmer.html
二、基本概念理解
Apache ActiveMQ是一个消息传递的中间件,符合JMS的规范实现。
JMS里面的角色:
1、Producer,产生消息;
2、Consumer,接收消息;
3、Provider,消息中间人,用于接收Producer的消息并发送到Consumer,类似产品有:Apache的ActiveMQ。RabbitMQ使用AMQP协议;
三、ActiveMQ的应用
1、ActiveMQ服务器基本应用
·官网下载ActiveMQ服务器,http://activemq.apache.org/
·解压ZIP,进入conf,确认activemq.xml中的tcp://127.0.0.1:61616,供Producer与Consumer访问的地址与端口。
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://127.0.0.1:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
·同是conf目录下,确认jetty.xml中的端口号,这个用于进入ActiveMQ的管理页面:http://127.0.0.1:8162/admin,可以查看消息的消费情况。
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
<!-- the default port number for the web console -->
<property name="host" value="0.0.0.0"/>
<property name="port" value="8162"/>
</bean>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.13.1</version>
</dependency>
</dependencies>
//Producer
public static void send(){
MessageProducer messageProducer=null;//消息发送者
//连接工厂,配置连接JMSProvider的用户,密码,地址
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"failover:(tcp://127.0.0.1:61616)");
Connection connection=null;
try {
connection=connectionFactory.createConnection();//连接JMSProvider
Session session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
//创建Queue,发送一个消息到JMSProvider,只能有一个接收者接收,若没有则存在JMSProvider中
Destination destination=session.createQueue("TEST_QUEUE");
//创建Topic,发送消息到订阅者们
//destination=session.createTopic("TEST_TOPIC");
messageProducer=session.createProducer(destination);//消息生产者
//设置持久化模式则重启ActiveMQ服务,消息还在队列中
//设置非持久化,则重启ActiveMQ服务,消息不在队列中
//messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
//定义消息
TextMessage message=session.createTextMessage("Hello,This is Sender");
//ObjectMessage objectMessage=session.createObjectMessage();
messageProducer.send(message);//发送消息
session.commit();
} catch (JMSException e) {
e.printStackTrace();
}finally {
if(connection!=null){
try {
connection.close();//关闭连接
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
//Customer
public static void receive(){
ConnectionFactory connectionFactory;
Connection connection=null;
Session session;
Destination destination;
MessageConsumer messageConsumer;
connectionFactory=new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"failover:(tcp://127.0.0.1:61616)");
try {
connection=connectionFactory.createConnection();
session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination=session.createQueue("TEST_QUEUE");
messageConsumer=session.createConsumer(destination);
messageConsumer.setMessageListener(new MyListener());
connection.start();
} catch (JMSException e) {
e.printStackTrace();
}
}
Topic模式:发布/订阅消息,Producer产生消息发送到Provider,发送到订阅了该Producer且处于活动状态的Consumer处。与Queen模式不同,消息可以被多个Consumer接收,Provider一直保存所有消息,直到消息被所有Consumer接收。与Queue不同的是,Provider会自动广播消息到Consumer,不需要Consumer来拉取。
//Producer
public static void send(){
MessageProducer messageProducer;//消息发送者
//连接工厂,配置连接JMSProvider的用户,密码,地址
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"failover:(tcp://127.0.0.1:61616)");
Connection connection=null;
try {
connection=connectionFactory.createConnection();//连接JMSProvider
connection.start();
Session session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE);//设置true表示开启事务
//创建Topic,发送消息到订阅者们
Destination destination=session.createTopic("TEST_TOPIC");
messageProducer=session.createProducer(destination);//消息生产者
// messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);//持久化模式
messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//非持久化模式
//定义消息
TextMessage mapMessage=session.createTextMessage("Hello,This is Sender by Topic");
messageProducer.send(mapMessage);//发送消息
session.commit();
} catch (JMSException e) {
e.printStackTrace();
}finally {
if(connection!=null){
try {
connection.close();//关闭连接
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
//Customer
public static void receive() {
ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
Destination destination;
MessageConsumer messageConsumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"failover:(tcp://127.0.0.1:61616)");
try {
connection = connectionFactory.createConnection();
connection.setClientID("");//在持久模式下需要设置CLIENTID
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic("TEST_TOPIC");
messageConsumer = session.createConsumer(destination);
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
String receiveMessage = ((TextMessage)message).getText();
System.out.println("接收到的消息:" + receiveMessage);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
connection.start();
} catch (JMSException e) {
e.printStackTrace();
} finally {
// if (connection != null) {
// try {
// connection.close();
// } catch (JMSException e) {
// e.printStackTrace();
// }
// }
}
}