0

I wrote simple sample to read text from console and send it to the rabbitMq server:

@Configuration
@EnableIntegration
@IntegrationComponentScan
public class Config {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Bean
    public IntegrationFlow fromConsoleToRabbitFlow() {
        return IntegrationFlows.from(consoleSource(), c -> c.id("consoleInput")
                .poller(Pollers.fixedRate(1000))
                .autoStartup(true)
        ).channel("consoleOutputChannel")
                .handle(Amqp.outboundAdapter(amqpTemplate).routingKey("my_spring_integration_queue"))
                .get();
    }

    public MessageSource<String> consoleSource() {
        return CharacterStreamReadingMessageSource.stdin();
    }

}

It looks like almost working solution but I can't find my_spring_integration_queue in rabbitmq admin console:

enter image description here

But I can't find anything related to 'my_spring_integration_queue' on other tab. Where can I find it ?

I expect that application will create queue if it doesn't exist. I was not able to find a method for send into the queur so I used .routingKey method. I also tried .exchangeName method but it lead to the:

32019-08-27 13:26:15.972 ERROR 16372 --- [ 127.0.0.1:5672] o.s.a.r.c.CachingConnectionFactory       : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'my_spring_integration_queue' in vhost '/', class-id=60, method-id=40)

P.S.

the Queue tab looks like this:

enter image description here

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

1

You either need to add the queue manually or use a RabbitAdmin @Bean to automatically declare it for you - the admin will find all beans of type Queue and declare them.

If you are using Spring Boot, it will automatically configure an admin bean for you so you just need the Queue @Bean.

See Configuring the Broker.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Do I have to pass 2 parameters: exchange_name and routing_key? And I then I need somehow **bind** exchange and to the queue ? – gstackoverflow Aug 27 '19 at 12:46
  • It depends; all queues are bound to the default exchange with the queue name as the routing key. Or you can create an exchange and bind the queue with a routing key that is different to the queue name; then you must send to the exchange with the right routing key. I suggest you read the Spring AMQP reference manual and go though the [RabbitMQ tutorials](https://www.rabbitmq.com/getstarted.html) to learn more. The RabbitAdmin will find exchange and binding beans too - see the link in my answer. – Gary Russell Aug 27 '19 at 12:51
  • Lets say I have queue with name **myQueue**. Do I have to write **.handle(Amqp.outboundAdapter(amqpTemplate).routingKey("myQueue")** ? Do I have to pass any exchange? – gstackoverflow Aug 27 '19 at 12:56
  • As I said; yes that will work because `myQueue` is bound to the default exchange with its name as the routing key. The framework will send the message to the default exchange by default. – Gary Russell Aug 27 '19 at 13:05