ActiveMQ接收消息+发送消息的简单实例

ActiveMQ的一个简单实例-ActiveMQ接收+发送消息 JMS消息框架——ActiveMQ

最近由于公司项目需要 -ActiveMQ接收+发送消息,用的是ActiveMQ。由于这方面网上的例子不是很多,而且有的也不完整。于是经过几天的摸索学习,在网上找到了合适的方案。

我的 IT技术资源库 http://itlib.tk/

ProducerTool.java用于发送消息:

java 代码
  1. packagehomework;
  2. importjavax.jms.Connection;
  3. importjavax.jms.DeliveryMode;
  4. importjavax.jms.Destination;
  5. importjavax.jms.JMSException;
  6. importjavax.jms.MessageProducer;
  7. importjavax.jms.Session;
  8. importjavax.jms.TextMessage;
  9. importorg.apache.activemq.ActiveMQConnection;
  10. importorg.apache.activemq.ActiveMQConnectionFactory;
  11. publicclassProducerTool{
  12. privateStringuser=ActiveMQConnection.DEFAULT_USER;
  13. privateStringpassword=ActiveMQConnection.DEFAULT_PASSWORD;
  14. privateStringurl=ActiveMQConnection.DEFAULT_BROKER_URL;
  15. privateStringsubject="TOOL.DEFAULT";
  16. privateDestinationdestination=null;
  17. privateConnectionconnection=null;
  18. privateSessionsession=null;
  19. privateMessageProducerproducer=null;
  20. //初始化
  21. privatevoidinitialize()throwsJMSException,Exception{
  22. ActiveMQConnectionFactoryconnectionFactory=newActiveMQConnectionFactory(
  23. user,password,url);
  24. connection=connectionFactory.createConnection();
  25. session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
  26. destination=session.createQueue(subject);
  27. producer=session.createProducer(destination);
  28. producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  29. }
  30. //发送消息
  31. publicvoidproduceMessage(Stringmessage)throwsJMSException,Exception{
  32. initialize();
  33. TextMessagemsg=session.createTextMessage(message);
  34. connection.start();
  35. System.out.println("Producer:->Sendingmessage:"+message);
  36. producer.send(msg);
  37. System.out.println("Producer:->Messagesentcomplete!");
  38. }
  39. //关闭连接
  40. publicvoidclose()throwsJMSException{
  41. System.out.println("Producer:->Closingconnection");
  42. if(producer!=null)
  43. producer.close();
  44. if(session!=null)
  45. session.close();
  46. if(connection!=null)
  47. connection.close();
  48. }
  49. }

ConsumerTool.java用于接受消息,我用的是基于消息监听的机制,需要实现MessageListener接口,这个接口有个onMessage方法,当接受到消息的时候会自动调用这个函数对消息进行处理。

java 代码
  1. packagehomework;
  2. importjavax.jms.Connection;
  3. importjavax.jms.Destination;
  4. importjavax.jms.JMSException;
  5. importjavax.jms.MessageConsumer;
  6. importjavax.jms.Session;
  7. importjavax.jms.MessageListener;
  8. importjavax.jms.Message;
  9. importjavax.jms.TextMessage;
  10. importorg.apache.activemq.ActiveMQConnection;
  11. importorg.apache.activemq.ActiveMQConnectionFactory;
  12. publicclassConsumerToolimplementsMessageListener{
  13. privateStringuser=ActiveMQConnection.DEFAULT_USER;
  14. privateStringpassword=ActiveMQConnection.DEFAULT_PASSWORD;
  15. privateStringurl=ActiveMQConnection.DEFAULT_BROKER_URL;
  16. privateStringsubject="TOOL.DEFAULT";
  17. privateDestinationdestination=null;
  18. privateConnectionconnection=null;
  19. privateSessionsession=null;
  20. privateMessageConsumerconsumer=null;
  21. //初始化
  22. privatevoidinitialize()throwsJMSException,Exception{
  23. ActiveMQConnectionFactoryconnectionFactory=newActiveMQConnectionFactory(
  24. user,password,url);
  25. connection=connectionFactory.createConnection();
  26. session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
  27. destination=session.createQueue(subject);
  28. consumer=session.createConsumer(destination);
  29. }
  30. //消费消息
  31. publicvoidconsumeMessage()throwsJMSException,Exception{
  32. initialize();
  33. connection.start();
  34. System.out.println("Consumer:->Beginlistening...");
  35. //开始监听
  36. consumer.setMessageListener(this);
  37. //Messagemessage=consumer.receive();
  38. }
  39. //关闭连接
  40. publicvoidclose()throwsJMSException{
  41. System.out.println("Consumer:->Closingconnection");
  42. if(consumer!=null)
  43. consumer.close();
  44. if(session!=null)
  45. session.close();
  46. if(connection!=null)
  47. connection.close();
  48. }
  49. //消息处理函数
  50. publicvoidonMessage(Messagemessage){
  51. try{
  52. if(messageinstanceofTextMessage){
  53. TextMessagetxtMsg=(TextMessage)message;
  54. Stringmsg=txtMsg.getText();
  55. System.out.println("Consumer:->Received:"+msg);
  56. }else{
  57. System.out.println("Consumer:->Received:"+message);
  58. }
  59. }catch(JMSExceptione){
  60. //TODOAuto-generatedcatchblock
  61. e.printStackTrace();
  62. }
  63. }
  64. }

如果想主动的去接受消息,而不用消息监听的话,把consumer.setMessageListener(this)改为Message message = consumer.receive(),手动去调用MessageConsumer的receive方法即可。

下面是测试类Test.java:

java 代码
  1. packagehomework;
  2. importjavax.jms.JMSException;
  3. publicclassTest{
  4. /**
  5. *@paramargs
  6. */
  7. publicstaticvoidmain(String[]args)throwsJMSException,Exception{
  8. //TODOAuto-generatedmethodstub
  9. ConsumerToolconsumer=newConsumerTool();
  10. ProducerToolproducer=newProducerTool();
  11. //开始监听
  12. consumer.consumeMessage();
  13. //延时500毫秒之后发送消息
  14. Thread.sleep(500);
  15. producer.produceMessage("Hello,world!");
  16. producer.close();
  17. //延时500毫秒之后停止接受消息
  18. Thread.sleep(500);
  19. consumer.close();
  20. }
  21. }

ActiveMQ的一个简单实例-ActiveMQ接收+发送消息


IT技术资源库 http://itlib.tk/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值