如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

出站网关

以下列表显示了 AMQP 出站网关的可能属性:spring-doc.cadn.net.cn

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

@MessagingGateway(defaultRequestChannel = "amqpOutbound.input")
public interface MyGateway {

    String sendToRabbit(String data);

}
@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 MessageChannel amqpOutboundChannel() {
    return new DirectChannel();
}

@MessagingGateway(defaultRequestChannel = "amqpOutboundChannel")
public interface MyGateway {

    String sendToRabbit(String data);

}
<int-amqp:outbound-gateway id="outboundGateway"               (1)
                           request-channel="myRequestChannel" (2)
                           amqp-template=""                   (3)
                           exchange-name=""                   (4)
                           exchange-name-expression=""        (5)
                           order="1"                          (6)
                           reply-channel=""                   (7)
                           reply-timeout=""                   (8)
                           requires-reply=""                  (9)
                           routing-key=""                     (10)
                           routing-key-expression=""          (11)
                           default-delivery-mode""            (12)
                           confirm-correlation-expression=""  (13)
                           confirm-ack-channel=""             (14)
                           confirm-nack-channel=""            (15)
                           confirm-timeout=""                 (16)
                           return-channel=""                  (17)
                           error-message-strategy=""          (18)
                           lazy-connect="true" />             (19)
1 此适配器的唯一 ID。 可选。
2 消息通道,消息将被发送至该通道以进行转换并发布到 AMQP 交换器。 必需。
3 对已配置的 AMQP 模板的 Bean 引用。 可选(默认为 amqpTemplate)。
4 要发送消息的 AMQP 交换器的名称。 如果未提供,消息将发送到默认的无名称交换器。 与 'exchange-name-expression' 互斥。 可选。
5 一个 SpEL 表达式,用于计算应发送消息的 AMQP 交换机的名称,其中消息作为根对象。 如果未提供,消息将发送到默认的无名称交换机。 与 'exchange-name' 互斥。 可选。
6 当注册多个消费者时,此消费者的订单顺序,从而实现负载均衡和故障转移。 可选(默认为 Ordered.LOWEST_PRECEDENCE [=Integer.MAX_VALUE])。
7 消息通道,用于在从 AMQP 队列接收并转换回复后发送这些回复。 可选。
8 网关在将回复消息发送到 reply-channel 时等待的时间。 仅当 reply-channel 可能阻塞时才适用此设置——例如,当前已满且容量有限的 QueueChannel。 默认为无限。
9 true 时,如果在 AmqpTemplate’s `replyTimeout 属性规定的时间内未收到回复消息,网关将抛出异常。 默认为 true
10 发送消息时使用的routing-key。 默认情况下,这是一个空的String。 与“routing-key-expression”互斥。 可选。
11 一个 SpEL 表达式,用于评估在发送消息时要使用的 routing-key,其中消息作为根对象(例如,'payload.key')。 默认情况下,这是一个空的 String。 与 'routing-key' 互斥。 可选的。
12 消息的默认传递模式:PERSISTENTNON_PERSISTENT。 如果 header-mapper 设置了传递模式,则会被覆盖。 如果存在 Spring Integration 消息头 amqp_deliveryMode,则 DefaultHeaderMapper 将设置该值。 如果未提供此属性且头部映射器也未设置它,则默认值取决于底层 Spring AMQP MessagePropertiesConverter(由 RabbitTemplate 使用)。 如果完全未进行自定义,则默认为 PERSISTENT。 可选。
13 自 4.0 版本起。2.定义关联数据的表达式。当提供此配置时,它将配置底层的 AMQP 模板以接收发布者确认。需要专用的 RabbitTemplateCachingConnectionFactory,并将 publisherConfirms 属性设置为 true。当收到发布者的确认且提供了关联数据时,根据确认类型的不同,它会被写入到 confirm-ack-channelconfirm-nack-channel 中。确认的负载是由此表达式定义的关联数据。消息的头部已设置为 'amqp_publishConfirm',其值为 true (ack) 或 false (nack)。对于 nack 次确认,Spring Integration 提供了一个额外的头信息 amqp_publishConfirmNackCause。示例:headers['myCorrelationData']payload。如果表达式解析为 Message<?> 实例(例如 #this),则从 ack/nack 通道发出的消息将基于该消息,并添加额外的头信息。以前,无论类型如何,新消息都是使用关联数据作为其有效负载创建的。另请参阅 发布确认和返回的替代机制。 Optional.
14 发送正向(ack)发布者确认的目标通道。 有效负载是由 confirm-correlation-expression 定义的相关数据。 如果表达式为 #root#this,消息将从原始消息构建,并将 amqp_publishConfirm 标头设置为 true。 另请参阅 发布者确认和返回的替代机制。 可选(默认为 nullChannel)。
15 用于发送负向(nack)发布者确认的通道。 有效负载是由 confirm-correlation-expression 定义的相关数据(如果未配置 ErrorMessageStrategy)。 如果表达式为 #root#this,则消息将从原始消息构建,并将 amqp_publishConfirm 头设置为 false。 当存在 ErrorMessageStrategy 时,消息是一个带有 NackedAmqpMessageException 有效负载的 ErrorMessage。 另请参阅 发布者确认和返回的替代机制。 可选(默认为 nullChannel)。
16 设置后,如果在指定的毫秒时间内未收到发布者确认,网关将合成一个否定确认(nack)。 待处理的确认每为此值的 50% 检查一次,因此实际发送 nack 的时间将在该值的 1 倍到 1.5 倍之间。 默认值为无(不会生成 nack)。
17 返回消息发送的通道。 如果提供此通道,底层的 AMQP 模板将配置为将无法投递的消息返回给适配器。 当未配置 ErrorMessageStrategy 时,消息将根据从 AMQP 接收的数据构建,并包含以下附加头信息:amqp_returnReplyCodeamqp_returnReplyTextamqp_returnExchangeamqp_returnRoutingKey。 如果存在 ErrorMessageStrategy,则消息是一个带有 ReturnedAmqpMessageException 负载的 ErrorMessage。 另请参阅 发布确认和返回的替代机制。 可选。
18 用于在发送已返回或负确认消息时构建 ErrorMessage 实例的 ErrorMessageStrategy 实现的引用。
19 当设置为 false 时,端点会在应用上下文初始化期间尝试连接到代理。 如果代理宕机,这将通过记录错误消息来实现“快速失败”的配置检测。 当设置为 true(默认值)时,连接将在发送第一条消息时建立(如果尚未建立,因为其他组件已建立它)。
return-channel

使用 return-channel 需要配合 RabbitTemplate,并将 mandatory 属性设置为 true,同时还需要一个 CachingConnectionFactory,并将其 publisherReturns 属性设置为 true。 当使用多个带返回值的出站端点时,每个端点都需要一个独立的 RabbitTemplatespring-doc.cadn.net.cn

底层 AmqpTemplate 的默认 replyTimeout 为五秒。 如果需要更长的超时时间,则必须在 template 上进行配置。

请注意,出站适配器与出站网关配置之间的唯一区别在于expectReply属性的设置。spring-doc.cadn.net.cn