gpt4 book ai didi

java - 如何为Python发送者创建一个Spring Boot RabbitMQ消费者?

转载 作者:行者123 更新时间:2023-12-02 11:27:24 25 4
gpt4 key购买 nike

我想开发一个应用程序,其中Python代码使用rabbitmq发送消息,而消费者是Spring bootrabbitmq代码。

发件人.py

#!/usr/bin/env python
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='topic_logs',
exchange_type='topic')

routing_key = sys.argv[1] if len(sys.argv) > 2 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='topic_logs',
routing_key=routing_key,
body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()

如何使用 Spring Boot 配置 RabbitMQ 接收器?接收端需要哪些必要的配置?请帮忙。

最佳答案

@SpringBootApplication
public class So49512910Application {

public static void main(String[] args) {
SpringApplication.run(So49512910Application.class, args);
}

@Bean
public Queue queue() {
return new Queue("someQueue");
}

@Bean
public TopicExchange exchange() {
return new TopicExchange("topic_logs");
}

@Bean
public Binding binding() {
return BindingBuilder.bind(queue()).to(exchange()).with("whatever.topic.pattern.you.want.to.match");
}

@RabbitListener(queues = "someQueue")
public void listener(String in) {
System.out.println(in);
}

}

或者,如果交换已经存在...

@SpringBootApplication
public class So49512910Application {

public static void main(String[] args) {
SpringApplication.run(So49512910Application.class, args);
}

@Bean
public Queue queue() {
return new Queue("someQueue");
}

@Bean
public Binding binding() {
return new Binding("someQueue", DestinationType.QUEUE, "topic_logs", "rk.pattern", null);
}

@RabbitListener(queues = "someQueue")
public void listener(String in) {
System.out.println(in);
}

}

关于java - 如何为Python发送者创建一个Spring Boot RabbitMQ消费者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49512910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com