【问题标题】:inbound and Outbound Gateway AMQP annotation入站和出站网关 AMQP 注释
【发布时间】:2014-08-31 16:13:24
【问题描述】:

我有一个使用 xml 配置的工作 spring 集成 + rabbitmq 应用程序。现在,我将它们转换为 java config 注释。一些主要的 amqp 对象(如 QueueTopicExchangeBinding)有可用的类和 java 注释。但是,在将 inbound-gatewayoutbound-gateway 转换为 java 注释或类实现时,我找不到任何参考。

这是我的实现: // 网关.xml

<int-amqp:outbound-gateway request-channel="requestChannel" reply-channel="responseChannel" exchange-name="${exchange}" routing-key-expression="${routing}"/>


<int-amqp:inbound-gateway request-channel="inboundRequest"
    queue-names="${queue}" connection-factory="rabbitConnectionFactory"
    reply-channel="inboundResponse" message-converter="compositeMessageConverter"/>

是否可以将它们转换为java注解或类实现(bean等)?

补充:我目前正在使用spring boot + spring integration

【问题讨论】:

  • 在 XML 版本本身中,我们如何提供交换名称?我想收听绑定到主题交换的队列。

标签: java rabbitmq spring-integration amqp spring-boot


【解决方案1】:

如果你看看Spring Integration Java DSL,那就太好了。

它为 AMQP 提供了一些流畅性:

@Bean
public IntegrationFlow amqpFlow() {
     return IntegrationFlows.from(Amqp.inboundGateway(this.rabbitConnectionFactory, queue()))
           .transform("hello "::concat)
           .transform(String.class, String::toUpperCase)
           .get();
}

@Bean
public IntegrationFlow amqpOutboundFlow() {
       return IntegrationFlows.from(Amqp.channel("amqpOutboundInput", this.rabbitConnectionFactory))
               .handle(Amqp.outboundAdapter(this.amqpTemplate).routingKeyExpression("headers.routingKey"))
               .get();
}

从注解的角度来看,您应该直接使用 Spring Integration 中的类来配置类似的东西:

@Bean
public AmqpInboundGateway amqpInbound() {
    AmqpInboundGateway gateway = new AmqpInboundGateway(new SimpleMessageListenerContainer(this.rabbitConnectionFactory));
    gateway.setRequestChannel(inboundChanne());
    return gateway;
}

@Bean
@ServiceActivator(inputChannel = "amqpOutboundChannel")
public AmqpOutboundEndpoint amqpOutbound() {
    AmqpOutboundEndpoint handler = new AmqpOutboundEndpoint(this.rabbitTemplate);
    handler.setOutputChannel(amqpReplyChannel());
    return handler;
}

【讨论】:

  • 谢谢你。我正在尝试使用这些,但是我不知道在哪里声明 InboudGateway 的队列名称。在 xml 配置中,您确实需要声明网关将获取消息的队列名称。
  • SimpleMessageListenerContainer 对此负责。 xml 标签解析器会为您做这件事。这就是为什么我建议迁移到 DSL 以避免对所有 bolraplate 代码进行这种 Java 配置,例如仅用于 queueName 的容器配置
猜你喜欢
  • 2016-01-09
  • 2016-05-07
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多