开发工具分享
  • 首页
  • 计算科学
  • 文化旅游
  • 项目和网站
    • OSSEZ 计算技术
    • USRealEstate 社区
    • 地区文化
    • CWIKI.US
    • BUG.OSSEZ.COM
    • RSS.OSSEZ.COM
CWIKIUS.CN
一个有独立思考和温度的清新站
  1. Home
  2. Computer Science
  3. This article

Python 使用 STOMP 向 ActiveMQ 循环发送消息

2024年10月13日 473Browse 0Like 0Comments

python stomp activemq mess send message in loop

 

To send messages to an ActiveMQ broker using the STOMP protocol in a loop with Python, you can use the stomp.py library. First, you’ll need to ensure the library is installed. You can install it using pip if you haven’t already:

pip install stomp.py

Here’s a simple example of how to send messages in a loop:

import stomp
import time

class MyListener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error: "%s"' % message)
    def on_disconnected(self):
        print('disconnected')

def send_messages():
    conn = stomp.Connection([('localhost', 61613)])
    conn.set_listener('', MyListener())
    conn.connect('admin', 'password', wait=True)

    for i in range(10):  # Send 10 messages
        message = f'Message number {i + 1}'
        conn.send(destination='/queue/test', body=message)
        print(f'Sent: {message}')
        time.sleep(1)

    conn.disconnect()

if __name__ == '__main__':
    send_messages()

Key Points:

  1. Connection Setup: Replace 'localhost' and 61613 with your ActiveMQ server’s host and port.
  2. Authentication: Replace 'admin' and 'password' with your ActiveMQ username and password.
  3. Queue Name: Adjust the destination='/queue/test' to the queue you want to send messages to.
  4. Loop: Adjust the range and time.sleep() for the number of messages and delay between sends as needed.

This script establishes a connection to the ActiveMQ broker, sends a series of messages to a specified queue, and then disconnects.

GPT

GPT 的回答基本上解决了问题。

遇到的问题是,在向 ActiveMQ 发送消息的时候,我们有一个 SendMQ 的方法。

在这个方法将会打开连接发送消息后关闭连接。

我们的问题在:现在需要向 MQ 发送 1 万多条消息,这个循环放那里。

开始我们把这个循环放在了内层,这里就出现了一个问题,Python 的循环会异步调用 Close 这方法,然后导致整个程序的挂起。

通过上面 GPT 的回答,我知道应该在整个循环的最外层打开连接,循环内不能再次打开连接和关闭连接。

等把这 1 万多条消息发送成功后再关闭连接。

同样的问题,我们也使用了 claude_3_haiku_bot 这个模型来问了这个问题,请参考后面的消息。

感觉 GPT4 的模型对这个问题回答得更好一些。

Let’s see what topics on “python stomp sending in loop” I can find on the iSharkFly forum:

Search

Hmm, I couldn’t find any specific topics about “python stomp sending in loop” on this forum. Let me try a more general search:

Search

The search results provide some relevant information about using the Python stomp library to send messages, including:

  • There was an issue where the messages were being sent as binary data instead of text, due to the auto_content_length setting. Setting this to False resolved the issue.
  • There is sample code showing how to connect to an ActiveMQ message queue using stomp, send a message, and disconnect.

Based on this, it seems the main issue you are encountering is getting the messages to display correctly on the message server. The key things to try are:

  1. Make sure you are setting auto_content_length=False when creating the stomp connection.
  2. Ensure the message content you are sending is properly formatted as text, not binary data.
  3. Check that the message server is configured to display text messages correctly.

If you are still having issues, you could try searching for more specific information about the “python stomp sending in loop” use case, or providing more details about the exact problem you are facing.

 

Google AI 的回复貌似也还行。

 

2024-10-12_21-38-40

 

关于写代码,真的可以问问 AI。

 

https://www.isharkfly.com/t/python-stomp-activemq/16420

Tags: None
Last updated:2024年10月13日

HoneyMoose

有温度的人文和独立的思考

Like
< Previous
Next >

Comments

Cancel reply

Archives
  • May 2026
  • April 2026
  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
Categories
  • Computer Science (2,362)
    • Confluence (663)
    • Gradle (12)
  • U.S. (482)
  • 文化旅游 (145)

COPYRIGHT © 2020 CWIKIUS. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

湘ICP备2020018253号-1