简体   繁体   English

如何为python接收器编写spring boot rabbitmq发送器?

[英]How do I write a spring boot rabbitmq sender for a python receiver?

I want to write an application wherein I need a producer sending messages using spring boot rabbitmq and the receiver is written in python. 我想编写一个应用程序,其中我需要一个生产者使用spring boot rabbitmq发送消息,而接收者是用python编写的。 The receiver part was easy- 接收器部分很容易-

receive.py receive.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')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

binding_keys = sys.argv[1:]
if not binding_keys:
    sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
    sys.exit(1)

for binding_key in binding_keys:
    channel.queue_bind(exchange='topic_logs',
                   queue=queue_name,
                   routing_key=binding_key)

print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))

channel.basic_consume(callback,
                  queue=queue_name,
                  no_ack=True)

channel.start_consuming()

How to write a spring boot rabbitmq sender code for this? 如何为此编写一个Spring Boot Rabbitmq发送者代码? What are the necessary things to be declared there? 在那声明什么必要的东西? Please help. 请帮忙。

Spring Boot is Java application, first of all. 首先,Spring Boot是Java应用程序。

So, you need to make yourself familiar with that language. 因此,您需要使自己熟悉该语言。

And the choice is correct: you really can send to the RabbitMQ from Java and receive from the queue in any other client. 选择是正确的:您确实可以从Java发送到RabbitMQ并从任何其他客户端的队列中接收。

Spring Boot provides for you RabbitTemplate bean. Spring Boot为您提供RabbitTemplate bean。 So, if the story is about to send you just need to inject such a bean and use its API to send: 因此,如果故事即将发送,您只需要注入这样的bean并使用其API发送即可:

@Autowired
RabbitTemplate rabbitTemplate;
...
this.rabbitTemplate.convertAndSend("topic_logs", binding_key, data);

See Reference Manual for more info. 有关更多信息,请参见参考手册

With spring integration, first you need to create a configuration class: 通过spring集成,首先需要创建一个配置类:

@Configuration
public class RabbitConfig {

    @Autowired
    private ConnectionFactory connectionFactory;

    @Bean
    DirectExchange dropfileExchange() {
        return new DirectExchange("exchange_name", true, false);
    }

    @Bean
    public Queue dropfileQueue() {
        return new Queue("queue_name", true);
    }

    @Bean
    Binding dropfileExchangeBinding(DirectExchange dropfileExchange, Queue dropfileQueue) {
        return BindingBuilder.bind(dropfileQueue).to(dropfileExchange).with("key_name");
    }

    @Bean
    public RabbitTemplate dropfileExchangeTemplate() {
        RabbitTemplate rt = new RabbitTemplate(connectionFactory);
        rt.setExchange("exchange_name");
        rt.setRoutingKey("key_name");
        rt.setConnectionFactory(connectionFactory);
        return rt;
    }

    @Bean
    public RabbitMessagingTemplate rabbitMessagingTemplate() {
        return new RabbitMessagingTemplate(dropfileExchangeTemplate());
    }
}

Then create a gateway service: 然后创建一个网关服务:

@MessagingGateway
public interface DropfileMessageGateway {
        @Gateway(requestChannel = "channel_name")
        void generate(String payload);
}

And then specify the producer flow using Java DSL as follows: 然后使用Java DSL指定生产者流程,如下所示:

@Bean
    public IntegrationFlow toOutboundQueueFlow() {
        return IntegrationFlows.from("channel_name")
                .transform(Transformers.toJson())
                .handle(Amqp.outboundAdapter(rabbitConfig.dropfileExchangeTemplate())).get();

    }

That will read a message from the channel, transform it to JSON and then dispatch it using an outbound adapter to the rabbit exchange. 这将从通道中读取一条消息,将其转换为JSON,然后使用出站适配器将其分发到Rabbit交换。

Note that the messages go to the channel via the messaging gateway, so the channel name in both should be the same. 请注意,消息通过消息传递网关到达通道,因此两者中的通道名称应相同。

Do not forget to include the appropriate dependencies. 不要忘记包括适当的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM