[英]How to create a spring boot rabbitmq consumer for a python sender?
I want develop an application where in the python code sends the message using rabbitmq and the consumer is Spring boot rabbitmq code. 我想开发一个应用程序,其中在python代码中使用rabbitmq发送消息,而使用者是Spring boot rabbitmq代码。
sender.py sender.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()
How do I configure a rabbitmq receiver using spring boot? 如何使用Spring Boot配置Rabbitmq接收器? What are the necessary configurations required at the receiver side? 接收方需要哪些必要的配置? Please help. 请帮忙。
@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);
}
}
Or, if the exchange already exists... 或者,如果交换已经存在...
@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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.