配色: 字号:
activeMQ学习过程
2016-11-24 | 阅:  转:  |  分享 
  
下载activeMQ去官方网站下载:http://activemq.apache.org/?启动服务系统会自动执行启动过程,当然一般安装失败
的情况是没有装JVM环境,启动成功应该是这样打开浏览器输入http://localhost:8161/admin/默认配置是这个,
当然你也可以更改这个配置服务端启动完毕java项目新建项目ActiveMQ_Test导入jar包导入apache-activemq
-5.5.1\lib目录下需要用到的jar文件新建class文件Sender:packagecom.xuwei.activemq
;importjavax.jms.Connection;importjavax.jms.ConnectionFactory;i
mportjavax.jms.DeliveryMode;importjavax.jms.Destination;import
javax.jms.MessageProducer;importjavax.jms.Session;importjavax.j
ms.TextMessage;importorg.apache.activemq.ActiveMQConnection;impo
rtorg.apache.activemq.ActiveMQConnectionFactory;publicclassSen
der{privatestaticfinalintSEND_NUMBER=5;publicstaticvoi
dmain(String[]args){//ConnectionFactory:连接工厂,JMS用它创建连接Con
nectionFactoryconnectionFactory;//Connection:JMS客户端到JMSProv
ider的连接Connectionconnection=null;//Session:一个发送或接收消息的线程S
essionsession;//Destination:消息的目的地;消息发送给谁.Destinationdestin
ation;//MessageProducer:消息发送者MessageProducerproducer;//Text
Messagemessage;//构造ConnectionFactory实例对象,此处采用ActiveMq的实现jarco
nnectionFactory=newActiveMQConnectionFactory(ActiveMQConnecti
on.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://loca
lhost:61616");try{//构造从工厂得到连接对象connection=connectionFactor
y.createConnection();//启动connection.start();//获取操作连接session
=connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDG
E);//获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的cons
ole配置destination=session.createQueue("FirstQueue");//得到消息生成者
【发送者】producer=session.createProducer(destination);//设置不持久化,此
处学习,实际根据项目决定producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT
);//构造消息,此处写死,项目就是参数,或者方法获取sendMessage(session,producer);ses
sion.commit();}catch(Exceptione){e.printStackTrace();}fin
ally{try{if(null!=connection)connection.close();}catch
(Throwableignore){}}}publicstaticvoidsendMessage(Session
session,MessageProducerproducer)throwsException{for(inti
=1;i<=SEND_NUMBER;i++){TextMessagemessage=session.cre
ateTextMessage("ActiveMq发送的消息"+i);//发送消息到目的地方System.out.pri
ntln("发送消息:"+"ActiveMq发送的消息"+i);producer.send(message);}}
}Receiver:packagecom.xuwei.activemq;importjavax.jms.Connection;
importjavax.jms.ConnectionFactory;importjavax.jms.Destination;i
mportjavax.jms.MessageConsumer;importjavax.jms.Session;importj
avax.jms.TextMessage;importorg.apache.activemq.ActiveMQConnectio
n;importorg.apache.activemq.ActiveMQConnectionFactory;publiccla
ssReceiver{publicstaticvoidmain(String[]args){//Connect
ionFactory:连接工厂,JMS用它创建连接ConnectionFactoryconnectionFactory;
//Connection:JMS客户端到JMSProvider的连接Connectionconnection=n
ull;//Session:一个发送或接收消息的线程Sessionsession;//Destination:消息
的目的地;消息发送给谁.Destinationdestination;//消费者,消息接收者MessageConsume
rconsumer;connectionFactory=newActiveMQConnectionFactory(Ac
tiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWOR
D,"tcp://localhost:61616");try{//构造从工厂得到连接对象connection=co
nnectionFactory.createConnection();//启动connection.start();//
获取操作连接session=connection.createSession(Boolean.FALSE,Session.
AUTO_ACKNOWLEDGE);//获取session注意参数值xingbo.xu-queue是一个服务器的queue,须
在在ActiveMq的console配置destination=session.createQueue("FirstQueu
e");consumer=session.createConsumer(destination);while(true)
{//设置接收者接收消息的时间,为了便于测试,这里谁定为100sTextMessagemessage=(TextMes
sage)consumer.receive(100000);if(null!=message){System.out
.println("收到消息"+message.getText());}else{break;}}}catch(Exceptione){e.printStackTrace();}finally{try{if(null!=connection)connection.close();}catch(Throwableignore){}}}}运行错误信息原因:activeMQ服务没开启!当程序关闭时:
献花(0)
+1
(本文系樱梦雪苹宝首藏)