如何为python发件人创建一个spring boot rabbitmq使用者? [英] How to create a spring boot rabbitmq consumer for a python sender?

查看:171
本文介绍了如何为python发件人创建一个spring boot rabbitmq使用者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want develop an application where in the python code sends the message using rabbitmq and the consumer is Spring boot rabbitmq code.

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()

如何使用spring boot配置rabbitmq接收器?接收方需要哪些必要的配置?请帮忙。

How do I configure a rabbitmq receiver using spring boot? 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);
    }

}

这篇关于如何为python发件人创建一个spring boot rabbitmq使用者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆