JMS-ActiveMQ入门 (zhuan)

ActiveMQ使用笔记

1.下载ActiveMQ5.1,官方网站地址:http://activemq.apache.org/

2.解压,双击bin目录下activemq.bat

管理页面:http://localhost:8161/admin

3.创建queue,这里创建一个名为queue1的queue,供测试用

4.Queue发送

packagedemo.activemq;

importjavax.jms.Connection;

importjavax.jms.ConnectionFactory;

importjavax.jms.DeliveryMode;

importjavax.jms.Destination;

importjavax.jms.MessageProducer;

importjavax.jms.Session;

importjavax.jms.TextMessage;

importorg.apache.activemq.ActiveMQConnection;

importorg.apache.activemq.ActiveMQConnectionFactory;

publicclassQueueSend{

privatestaticfinalintSEND_NUMBER=5;

publicstaticvoidmain(String[]args){

//ConnectionFactory:连接工厂,JMS用它创建连接

ConnectionFactoryconnectionFactory;

//Connection:JMS客户端到JMSProvider的连接

Connectionconnection=null;

//Session:一个发送或接收消息的线程

Sessionsession;

//Destination:消息的目的地;消息发送给谁.

Destinationdestination;

//MessageProducer:消息发送者

MessageProducerproducer;

//TextMessagemessage;

//构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar

connectionFactory=newActiveMQConnectionFactory(

ActiveMQConnection.DEFAULT_USER,

ActiveMQConnection.DEFAULT_PASSWORD,

"tcp://localhost:61616");

try{

//构造从工厂得到连接对象

connection=connectionFactory.createConnection();

//启动

connection.start();

//获取操作连接

session=connection.createSession(Boolean.TRUE,

Session.AUTO_ACKNOWLEDGE);

//queue1需要在admin界面创建

destination=session.createQueue("queue1");

//得到消息生成者

producer=session.createProducer(destination);

//设置不持久化,此处学习,实际根据项目决定

producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

//构造消息,此处写死,项目就是参数,或者方法获取

sendMessage(session,producer);

session.commit();

}catch(Exceptione){

e.printStackTrace();

}finally{

try{

if(null!=connection)

connection.close();

}catch(Throwableignore){

}

}

}

publicstaticvoidsendMessage(Sessionsession,MessageProducerproducer)

throwsException{

for(inti=1;i<=SEND_NUMBER;i++){

TextMessagemessage=session

.createTextMessage("ActiveMq发送的消息"+i);

//发送消息到目的地方

System.out.println("发送消息:"+i+"成功");

producer.send(message);

}

}

}

执行之后

4.Queue接收消息

packagedemo.activemq;

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;

publicclassQueueReceive{

publicstaticvoidmain(String[]args){

//ConnectionFactory:连接工厂,JMS用它创建连接

ConnectionFactoryconnectionFactory;

//Connection:JMS客户端到JMSProvider的连接

Connectionconnection=null;

//Session:一个发送或接收消息的线程

Sessionsession;

//Destination:消息的目的地;消息发送给谁.

Destinationdestination;

//消费者,消息接收者

MessageConsumerconsumer;

connectionFactory=newActiveMQConnectionFactory(

ActiveMQConnection.DEFAULT_USER,

ActiveMQConnection.DEFAULT_PASSWORD,

"tcp://localhost:61616");

try{

//构造从工厂得到连接对象

connection=connectionFactory.createConnection();

//启动

connection.start();

//获取操作连接

session=connection.createSession(Boolean.FALSE,

Session.AUTO_ACKNOWLEDGE);

//获取session注意参数是一个服务器的queue,须在在ActiveMq的console配置

destination=session.createQueue("queue1");

consumer=session.createConsumer(destination);

while(true){

TextMessagemessage=(TextMessage)consumer.receive(1000);

if(null!=message){

System.out.println("收到消息"+message.getText());

}else{

break;

}

}

}catch(Exceptione){

e.printStackTrace();

}finally{

try{

if(null!=connection)

connection.close();

}catch(Throwableignore){

}

}

}

}

需要的包:active-core_5.1.0.jar,commons-logging.jar,geronimo-j2ee-management_1.0_spec-1.0.jar,jms.jar

相关推荐