简体   繁体   中英

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. The receiver part was easy-

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? What are the necessary things to be declared there? Please help.

Spring Boot is Java application, first of all.

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.

Spring Boot provides for you RabbitTemplate bean. So, if the story is about to send you just need to inject such a bean and use its API to send:

@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:

@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:

@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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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