1.MQ消息处理插件 接口属于JMS(JAVA MESSAGE SERVICE) 规范,
2.官网下载地址:http://activemq.apache.org/
3.运行ActiveMQ
解压缩Apache-activeMq.zip,双击Apache-activemq\bin\activemq.bat运行ActiveMQ
4.登陆http://localhost:8161/admin/ 默认用户名/密码
admin/admin
5.将下载的lib 包中的activemq-all-5.13.3.jar 拷贝到项目lib中
6.点对点的消息模式
生产者代码
package com.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSProducer {
private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;//默认的连接;
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认的密码;
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认的链接地址;
private static final int SENDNUM=10;//发送消息的数量;
public static void main(String[] args) {
ConnectionFactory connectionFactory;//链接工厂
Connection connect = null;//lianjie
Session session;//会话接受或者发送消息的线程;
Destination destination;//消息目的地;
MessageProducer messageProducer;//消息生产者;
connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
try {
connect = connectionFactory.createConnection();//通过链接工厂获取链接
connect.start();//启动链接
session = connect.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);//创建session
destination = session.createQueue("FirstQueue2");//创建消息队列
messageProducer = session.createProducer(destination);//创建消息生产者;
sendMessage(session,messageProducer);
session.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(connect!=null){
try {
connect.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
for(int i=0;i<JMSProducer.SENDNUM;i++){
TextMessage message = session.createTextMessage("ActiveMQ 发送的消息"+i);
System.out.println("发送消息:"+"ActiveMQ 发送的消息"+i);
messageProducer.send(message);
}
}
}
6.消费者代码
package com.java1234.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
*
* @author zzx
*
*/
public class JMSConsumer {
private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;//默认的连接;
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认的密码;
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认的链接地址;
private static final int SENDNUM=10;//发送消息的数量;
public static void main(String[] args) {
ConnectionFactory connectionFactory;//链接工厂
Connection connect = null;//lianjie
Session session;//会话接受或者发送消息的线程;
Destination destination;//消息目的地;
MessageConsumer messageConsumer;//消息消费者;
connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
try {
connect = connectionFactory.createConnection();//通过链接工厂获取链接
connect.start();//启动链接
session = connect.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);//创建session
destination = session.createQueue("FirstQueue1");//创建消息队列
messageConsumer = session.createConsumer(destination);//创建消息消费者;
while(true){
TextMessage textMessage = (TextMessage) messageConsumer.receive(100*1000);
if(textMessage!=null){
System.out.println("收到的消息:"+textMessage.getText());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(connect!=null){
try {
connect.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}