0

Given I have application with AMQP anonymous queue and fanout exchange:

@Bean
public Queue cacheUpdateAnonymousQueue() {
    return new AnonymousQueue();
}

public static final String CACHE_UPDATE_FANOUT_EXCHANGE = "cache.update.fanout";

@Bean
FanoutExchange cacheUpdateExchange() {
    return new FanoutExchange(CACHE_UPDATE_FANOUT_EXCHANGE);
}

@Bean
Binding cacheUpdateQueueToCacheUpdateExchange() {
    return bind(cacheUpdateAnonymousQueue())
            .to(cacheUpdateExchange());
}

and Spring Integration flow:

@Bean
public IntegrationFlow cacheOutputFlow() {
    return from(channelConfig.cacheUpdateOutputChannel())
            .transform(objectToJsonTransformer())
            .handle(outboundAdapter())
            .get();
}

And I use outbound adapter:

public MessageHandler outboundAdapter() {
    rabbitTemplate.setChannelTransacted(true);
    return outboundAdapter(rabbitTemplate)
            .exchangeName(CACHE_UPDATE_FANOUT_EXCHANGE)
            .get();
}

I can see in logs:

o.s.amqp.rabbit.core.RabbitTemplate: Executing callback on RabbitMQ Channel: Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,4), conn: Proxy@40976c4b Shared Rabbit Connection: SimpleConnection@1cfaa28d [delegate=amqp://guest@127.0.0.1:5672/, localPort= 56042]
o.s.amqp.rabbit.core.RabbitTemplate: Publishing message on exchange [cache.update.fanout], routingKey = []

but message is not delivered to queue bound to cache.update.fanout exchange.

When I set rabbitTemplate.setChannelTransacted(false); in outbound adapter, then I can see in logs:

o.s.amqp.rabbit.core.RabbitTemplate      : Executing callback on RabbitMQ Channel: Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@11a1389d Shared Rabbit Connection: SimpleConnection@444c6abf [delegate=amqp://guest@127.0.0.1:5672/, localPort= 56552]
o.s.amqp.rabbit.core.RabbitTemplate      : Publishing message on exchange [cache.update.fanout], routingKey = []

and message is delivered to queue.

Why is message not delivered in first case?

Why doesn't RabbitTemplate indicate something?

Patrik Mihalčin
  • 3,341
  • 7
  • 33
  • 68

1 Answers1

0

Your logs have different exchange names; I just tested it like this...

@SpringBootApplication
public class So60993877Application {

    public static void main(String[] args) {
        SpringApplication.run(So60993877Application.class, args);
    }

    @Bean
    public Queue cacheUpdateAnonymousQueue() {
        return new AnonymousQueue();
    }

    public static final String CACHE_UPDATE_FANOUT_EXCHANGE = "cache.update.fanout";

    @Bean
    FanoutExchange cacheUpdateExchange() {
        return new FanoutExchange(CACHE_UPDATE_FANOUT_EXCHANGE);
    }

    @Bean
    Binding cacheUpdateQueueToCacheUpdateExchange() {
        return BindingBuilder.bind(cacheUpdateAnonymousQueue())
                .to(cacheUpdateExchange());
    }

    @RabbitListener(queues = "#{cacheUpdateAnonymousQueue.name}")
    public void listen(String in) {
        System.out.println(in);
    }

    @Bean
    public ApplicationRunner runner(RabbitTemplate template) {
        return args -> {
            template.convertAndSend(CACHE_UPDATE_FANOUT_EXCHANGE, 
                cacheUpdateAnonymousQueue().getName(), "foo");
            template.setChannelTransacted(true);
            template.convertAndSend(CACHE_UPDATE_FANOUT_EXCHANGE, 
                cacheUpdateAnonymousQueue().getName(), "bar");
        };
    }

}

With no problems.

foo
bar

With confirms and returns enabled:

    @Bean
    public ApplicationRunner runner(RabbitTemplate template) {
        template.setReturnCallback((message, replyCode, replyText, exchange, routingKey) ->
            LOG.info("Return: " + message));
        template.setConfirmCallback((correlationData, ack, cause) ->
            LOG.info("Confirm: " + correlationData + ": " + ack));
        return args -> {
            template.convertAndSend(CACHE_UPDATE_FANOUT_EXCHANGE, cacheUpdateAnonymousQueue().getName(),
                    "foo", new CorrelationData("foo"));
//          template.setChannelTransacted(true);
            template.convertAndSend(CACHE_UPDATE_FANOUT_EXCHANGE, cacheUpdateAnonymousQueue().getName(),
                    "bar", new CorrelationData("bar"));
            template.convertAndSend("missingExchange", cacheUpdateAnonymousQueue().getName(), "baz",
                    new CorrelationData("baz"));
            Thread.sleep(5000);
        };
    }
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I made typo in second log entry, sorry Can this behavior be related to rabbitmq broker version? – Patrik Mihalčin Apr 02 '20 at 14:54
  • I very much doubt it. Try enabling publisher confirms and returns in the connection factory (and add listeners to the RabbitTemplate to display them) - but you can only use confirms if transacted is `false`. – Gary Russell Apr 02 '20 at 15:13
  • + I also use older rabbit dependencies: [INFO] | \- org.springframework.amqp:spring-rabbit:jar:1.7.6.RELEASE:compile [INFO] | +- org.springframework.amqp:spring-amqp:jar:1.7.6.RELEASE:compile [INFO] | +- com.rabbitmq:http-client:jar:1.1.1.RELEASE:compile [INFO] | \- com.rabbitmq:amqp-client:jar:4.0.3:compile – Patrik Mihalčin Apr 02 '20 at 15:19
  • I will try what you suggested – Patrik Mihalčin Apr 02 '20 at 15:19
  • Yikes!! That is 2 years old; you should at least upgrade to 1.7.14. However, I very much doubt that's the problem (but I will test it). – Gary Russell Apr 02 '20 at 15:21