当前位置:   article > 正文

Python对接ActiveMQ消息队列_python链接jms activemq

python链接jms activemq

activeMQ官方文档:Using the API — Stomp 7.0.0 documentation

ActiveMQ支持Python访问,提供了基于STOMP协议(端口为61613)的库。

ActiveMQ的官文Cross Language Clients中给出了更详细的介绍,并附有示例代码,如下图:

第一行为常规Python访问,第二行为使用Jython访问的方式,四个操作。

安装命令: pip install setup.py

发送消息到队列中:

  1. import stomp
  2. conn = stomp.Connection()
  3. conn.set_listener('', MyListener())
  4. conn.start()
  5. conn.connect('admin', 'password', wait=True)
  6. conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')
  7. conn.disconnect()

 

ActiveMQ官网的四个测试:

发送消息到队列Queue属于 点对点模式,不可以重复消费;

发送消息到主题Topic属于 发布/订阅模式,可以重复消费;

  1. # Send a Message to an Apache ActiveMQ Queue
  2. import stomp
  3. conn = stomp.Connection10()
  4. conn.start()
  5. conn.connect()
  6. conn.send('SampleQueue', 'Simples Assim')
  7. conn.disconnect()
  8. # Receive a Message from an Apache ActiveMQ Queue
  9. import stomp
  10. import time
  11. class SampleListener(object):
  12. def on_message(self, headers, msg):
  13. print(msg)
  14. conn = stomp.Connection10()
  15. conn.set_listener('SampleListener', SampleListener())
  16. conn.start()
  17. conn.connect()
  18. conn.subscribe('SampleQueue')
  19. time.sleep(1) # secs
  20. conn.disconnect()
  21. # Send a Message to an Apache ActiveMQ Topic
  22. import stomp
  23. conn = stomp.Connection10()
  24. conn.start()
  25. conn.connect()
  26. conn.send('/topic/SampleTopic', 'Simples Assim')
  27. conn.disconnect()
  28. # Receive a Message from an Apache ActiveMQ Topic (1)
  29. import stomp
  30. import time
  31. class SampleListener(object):
  32. def on_message(self, headers, msg):
  33. print(msg)
  34. conn = stomp.Connection10()
  35. conn.set_listener('SampleListener', SampleListener())
  36. conn.start()
  37. conn.connect()
  38. conn.subscribe('/topic/SampleTopic')
  39. time.sleep(1) # secs
  40. conn.disconnect()
  41. # Receive a Message from an Apache ActiveMQ Topic (2)
  42. import stomp
  43. import time
  44. class SampleListener(object):
  45. def on_message(self, headers, msg):
  46. print(msg)
  47. conn = stomp.Connection10()
  48. conn.set_listener('SampleListener', SampleListener())
  49. conn.start()
  50. conn.connect(headers={'client-id':'SampleClient'})
  51. conn.subscribe(destination='/topic/SampleTopic', headers={'activemq.subscriptionName':'SampleSubscription'})
  52. time.sleep(1) # secs
  53. conn.disconnect()

 在连接的时候,遇到了第三方发布了一个java代码,他们使用的是tcp协议,坑死我了:

  1. package Com;
  2. //import org.apache.activemq.ActiveMQConnectionFactory;
  3. //import javax.jms.*;
  4. //
  5. //class Consumer {
  6. // public static void main(String[] args) throws JMSException {
  7. // //1.创建connectionfacoty
  8. // ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("账号","密码","tcp://**.***.**.**:****");
  9. ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin","admin","tcp://127.0.0.1:61613");
  10. // //2.创建connnect,并启动connnect
  11. // Connection connection = connectionFactory.createConnection();
  12. // connection.start();
  13. //
  14. // //3.创建session,第一个参数是是否使用事务,第二个参数是确认机制
  15. // Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
  16. // //4.创建目的地【消费者与生产者的目的地相同才能进行消息传递】
  17. Destination destination = session.createTopic("主题");
  18. // Destination destination = session.createTopic("SampleQueue");
  19. // //5.创建消费者,第一个参数是目的地,此时创建的消费者要与目的地进行绑定。
  20. // MessageConsumer consumer = session.createConsumer(destination);
  21. // //6.使用消费者接受消息消息
  22. // try {
  23. // TextMessage message = (TextMessage) consumer.receive();
  24. // System.out.println(message.getText());
  25. // }catch (Exception exception){
  26. // System.out.println(exception.getMessage());
  27. // }
  28. // //8.提交事务
  29. // //session.commit();
  30. // //9.关闭资源
  31. // // session.close();
  32. // // connection.close();
  33. // }
  34. //}
  35. import org.apache.activemq.ActiveMQConnectionFactory;
  36. import javax.jms.*;
  37. class Consumer {
  38. public static void main(String[] args) throws JMSException {
  39. //1.创建connectionfacoty
  40. ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("thrid","thrid123","tcp://60.191.78.86:8182");
  41. //2.创建connnect,并启动connnect
  42. Connection connection = connectionFactory.createConnection();
  43. connection.start();
  44. //3.创建session,第一个参数是是否使用事务,第二个参数是确认机制
  45. Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
  46. //4.创建目的地【消费者与生产者的目的地相同才能进行消息传递】
  47. Destination destination = session.createTopic("IOT_THIRD_WARN");
  48. //5.创建消费者,第一个参数是目的地,此时创建的消费者要与目的地进行绑定。
  49. MessageConsumer consumer = session.createConsumer(destination);
  50. //6.使用消费者接受消息消息
  51. while (true){
  52. TextMessage message = (TextMessage) consumer.receive();
  53. System.out.println(message.getText());
  54. }
  55. //8.提交事务
  56. //session.commit();
  57. //9.关闭资源
  58. // session.close();
  59. // connection.close();
  60. }
  61. }

结果我这边没有发现,所以一直卡住调不通,最后发现他们没有开通stomp协议,也没有开通协议端口,所以就导致java能通,但是python不通的bug,还有一个注意点:这个/topic或者/queue 要和发送消息的保持一致,不然还是会收不到消息。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/295857
推荐阅读
相关标签
  

闽ICP备14008679号