ActiveMQ入门
1.下载ActiveMQ
去官方网站下载:http://activemq.apache.org/
2.运行ActiveMQ
将apache-activemq-5.5.1-bin.zip解压缩,双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。
启动ActiveMQ以后,登陆:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue。
3.导入jar包:
4.实例
1、发送端
Java代码收藏代码
importjavax.jms.Connection;
importjavax.jms.ConnectionFactory;
importjavax.jms.DeliveryMode;
importjavax.jms.Destination;
importjavax.jms.JMSException;
importjavax.jms.MessageProducer;
importjavax.jms.Session;
importjavax.jms.TextMessage;
importorg.apache.activemq.ActiveMQConnection;
importorg.apache.activemq.ActiveMQConnectionFactory;
publicclassSender{
/**创建Session*/
publicstaticSessioncreateSession(){
//连接工厂,JMS用它创建连接
ConnectionFactoryconnectionFactory=null;
//MS客户端到JMSProvider的连接
Connectionconnection=null;
//一个发送或接收消息的会话
Sessionsession=null;
try{
//构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory=newActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
//通过连接工厂创建连接
connection=connectionFactory.createConnection();
//启动连接
connection.start();
//创建Session
session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
}catch(Exceptione){
e.printStackTrace();
}
returnsession;
}
/**创建消息发送者*/
publicstaticMessageProducercreateProducer(Sessionsession){
//消息的目的地;消息发送给谁.
Destinationdestination=null;
//消息发送者
MessageProducerproducer=null;
try{
//获取session注意参数值FirstQueue是一个服务器的queue,须在在ActiveMq的console配置
destination=session.createQueue("FirstQueue");
//消息--发送者
producer=session.createProducer(destination);
//设置不持久化,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}catch(Exceptione){
e.printStackTrace();
}
returnproducer;
}
/**发送消息*/
publicstaticvoidsendMessage(Sessionsession,MessageProducerproducer,
Stringcontent){
TextMessagetextMessage=null;
try{
textMessage=session.createTextMessage(content);
producer.send(textMessage);
session.commit();//提交
}catch(JMSExceptione){
e.printStackTrace();
}
}
/**测试*/
publicstaticvoidmain(String[]args){
Sessionsession=createSession();
MessageProducerproducer=createProducer(session);
for(inti=0;i<5;i++){
Stringcontent="Message---"+i;
System.out.println(content);
sendMessage(session,producer,content);
}
}
}
2、接收端
Java代码收藏代码
importjavax.jms.Connection;
importjavax.jms.ConnectionFactory;
importjavax.jms.Destination;
importjavax.jms.MessageConsumer;
importjavax.jms.Session;
importjavax.jms.TextMessage;
importorg.apache.activemq.ActiveMQConnection;
importorg.apache.activemq.ActiveMQConnectionFactory;
publicclassReceiver{
/**创建Session*/
publicstaticSessioncreateSession(){
//连接工厂,JMS用它创建连接
ConnectionFactoryconnectionFactory=null;
//MS客户端到JMSProvider的连接
Connectionconnection=null;
//一个发送或接收消息的会话
Sessionsession=null;
try{
//构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory=newActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
//通过连接工厂创建连接
connection=connectionFactory.createConnection();
//启动连接
connection.start();
//创建Session
session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
}catch(Exceptione){
e.printStackTrace();
}
returnsession;
}
/**创建消息接收者*/
publicstaticMessageConsumercreateConsumer(Sessionsession){
//消息的目的地;消息发送给谁.
Destinationdestination=null;
//消息接收者
MessageConsumerconsumer=null;
try{
//获取session注意参数值FirstQueue是一个服务器的queue,须在在ActiveMq的console配置
destination=session.createQueue("FirstQueue");
//得到消息生成者"发送者"
consumer=session.createConsumer(destination);
}catch(Exceptione){
e.printStackTrace();
}
returnconsumer;
}
/**发送消息*/
publicstaticvoidreceiverMessage(MessageConsumerconsumer){
try{
while(true){
//设置接收者接收消息的时间
TextMessagemessage=(TextMessage)consumer.receive(6*1000);
if(null!=message){
System.out.println("ReceiverMessage--"+message.getText());
}else{
System.out.println("break");
break;
}
}
}catch(Exceptione){
e.printStackTrace();
}
}
/**测试*/
publicstaticvoidmain(String[]args){
receiverMessage(createConsumer(createSession()));
}
}
5、测试结果
执行Sender:
Message---0
Message---1
Message---2
Message---3
Message---4
执行Receive:
ReceiverMessage--Message---0
ReceiverMessage--Message---1
ReceiverMessage--Message---2
ReceiverMessage--Message---3
ReceiverMessage--Message---4