RabbitMQ 出站网关使用 java 配置

RabbitMQ Outbound gateway using java configuration

RabbitMQ 出站网关的任何示例参考 post 消息到 rabbitmq 集群,我正在寻找 Java 配置 Spring Boot

参见 the reference manual。那里有 xml、Java 和 Java DSL 示例。

@Bean
@ServiceActivator(inputChannel = "amqpOutboundChannel")
public AmqpOutboundEndpoint amqpOutbound(AmqpTemplate amqpTemplate) {
    AmqpOutboundEndpoint outbound = new AmqpOutboundEndpoint(amqpTemplate);
    outbound.setExpectReply(true);
    outbound.setRoutingKey("foo"); // default exchange - route to queue 'foo'
    return outbound;
}

@Bean
public IntegrationFlow amqpOutbound(AmqpTemplate amqpTemplate) {
    return IntegrationFlows.from(amqpOutboundChannel())
            .handle(Amqp.outboundGateway(amqpTemplate)
                    .routingKey("foo")) // default exchange - route to queue 'foo'
            .get();
}

网关用于 request/reply 处理,而不仅仅是 "post" ing;如果你只是发送,你需要一个通道适配器。

@Bean
@ServiceActivator(inputChannel = "amqpOutboundChannel")
public AmqpOutboundEndpoint amqpOutbound(AmqpTemplate amqpTemplate) {
    AmqpOutboundEndpoint outbound = new AmqpOutboundEndpoint(amqpTemplate);
    outbound.setRoutingKey("foo"); // default exchange - route to queue 'foo'
    return outbound;
}

@Bean
public IntegrationFlow amqpOutbound(AmqpTemplate amqpTemplate) {
    return IntegrationFlows.from(amqpOutboundChannel())
            .handle(Amqp.outboundAdapter(amqpTemplate)
                        .routingKey("foo")) // default exchange - route to queue 'foo'
            .get();
}