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

出站通道适配器

以下示例展示了 AMQP 出站通道适配器的可用属性:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow amqpOutbound(AmqpTemplate amqpTemplate,
        MessageChannel amqpOutboundChannel) {
    return IntegrationFlow.from(amqpOutboundChannel)
            .handle(Amqp.outboundAdapter(amqpTemplate)
                        .routingKey("queue1")) // default exchange - route to queue 'queue1'
            .get();
}
@Bean
@ServiceActivator(inputChannel = "amqpOutboundChannel")
public AmqpOutboundEndpoint amqpOutbound(AmqpTemplate amqpTemplate) {
    AmqpOutboundEndpoint outbound = new AmqpOutboundEndpoint(amqpTemplate);
    outbound.setRoutingKey("queue1"); // default exchange - route to queue 'queue1'
    return outbound;
}

@Bean
public MessageChannel amqpOutboundChannel() {
    return new DirectChannel();
}
<int-amqp:outbound-channel-adapter id="outboundAmqp"             (1)
                               channel="outboundChannel"         (2)
                               amqp-template="myAmqpTemplate"    (3)
                               exchange-name=""                  (4)
                               exchange-name-expression=""       (5)
                               order="1"                         (6)
                               routing-key=""                    (7)
                               routing-key-expression=""         (8)
                               default-delivery-mode""           (9)
                               confirm-correlation-expression="" (10)
                               confirm-ack-channel=""            (11)
                               confirm-nack-channel=""           (12)
                               confirm-timeout=""                (13)
                               wait-for-confirm=""               (14)
                               return-channel=""                 (15)
                               error-message-strategy=""         (16)
                               header-mapper=""                  (17)
                               mapped-request-headers=""         (18)
                               lazy-connect="true"               (19)
                               multi-send="false"/>              (20)
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 发送消息时使用的固定路由键。 默认情况下,这是一个空的 String。 与 'routing-key-expression' 互斥。 可选。
8 一个 SpEL 表达式,用于在发送消息时确定要使用的路由键,其中消息作为根对象(例如,'payload.key')。 默认为空 String。 与 'routing-key' 互斥。 可选。
9 消息的默认传递模式:PERSISTENTNON_PERSISTENT。 如果 header-mapper 设置了传递模式,则会被覆盖。 如果存在 Spring Integration 消息头 amqp_deliveryMode,则 DefaultHeaderMapper 将设置该值。 如果未提供此属性且头部映射器也未设置它,则默认值取决于底层 Spring AMQP MessagePropertiesConverter(由 RabbitTemplate 使用)。 如果完全未进行自定义,则默认为 PERSISTENT。 可选。
10 定义关联数据的表达式。当提供此配置时,将配置底层的 AMQP 模板以接收发布者确认。需要专用的 RabbitTemplateCachingConnectionFactory,并将 publisherConfirms 属性设置为 true。当收到发布者确认且提供了关联数据时,它将根据确认类型写入confirm-ack-channelconfirm-nack-channel。确认的负载是关联数据,如该表达式所定义。该消息的 'amqp_publishConfirm' 头设置为 true (ack) 或 false (nack)。示例:headers['myCorrelationData']payload。版本 4。1 引入了 amqp_publishConfirmNackCause 消息头。它包含用于发布者确认的 'nack' 的 cause。从版本 4 开始。2,如果表达式解析为 Message<?> 实例(例如 #this),则在 ack/nack 通道上发出的消息将基于该消息,并添加额外的标头。以前,无论类型如何,新消息都是使用关联数据作为其有效负载创建的。另请参阅 发布确认和返回的替代机制。 Optional.
11 用于发送正向(ack)发布者确认的通道。 有效负载是由 confirm-correlation-expression 定义的相关数据。 如果表达式为 #root#this,则消息将从原始消息构建,并将 amqp_publishConfirm 标头设置为 true。 另请参阅 发布者确认和返回的替代机制。 可选(默认为 nullChannel)。
12 发送负(nack)发布确认的通道。 有效载荷是由 confirm-correlation-expression 定义的关联数据(如果未配置 ErrorMessageStrategy)。 如果表达式为 #root#this,消息将从原始消息构建,并将 amqp_publishConfirm 头设置为 false。 当存在 ErrorMessageStrategy 时,消息是一个带有 NackedAmqpMessageException 有效载荷的 ErrorMessage。 另请参阅 发布确认和返回的替代机制。 可选(默认为 nullChannel)。
13 设置后,如果在指定的毫秒时间内未收到发布者确认,适配器将合成一个否定确认(nack)。 待处理的确认每 50% 的该值时间检查一次,因此实际发送 nack 的时间将在该值的 1 倍到 1.5 倍之间。 另请参阅 发布者确认和返回的替代机制。 默认值为无(不会生成 nack)。
14 当设置为 true 时,调用线程将阻塞,等待发布者的确认。 这需要为 confirms 配置 RabbitTemplate,同时也需要 confirm-correlation-expression。 线程将最多阻塞 confirm-timeout(默认情况下为 5 秒)。 如果发生超时,将抛出 MessageTimeoutException。 如果启用了返回功能且消息被返回,或者在等待确认期间发生任何其他异常,将抛出 MessageHandlingException,并附带相应的消息。
15 用于发送返回消息的通道。 如果提供该配置,底层的 AMQP 模板将被配置为将不可投递的消息返回给适配器。 当未配置 ErrorMessageStrategy 时,消息将从从 AMQP 接收的数据构建,并包含以下附加标头:amqp_returnReplyCodeamqp_returnReplyTextamqp_returnExchangeamqp_returnRoutingKey。 如果存在 ErrorMessageStrategy,则消息为带有 ReturnedAmqpMessageException 有效负载的 ErrorMessage。 另请参阅 发布确认和返回的替代机制。 可选。
16 用于在发送已返回或负确认消息时构建 ErrorMessage 实例的 ErrorMessageStrategy 实现的引用。
17 对用于发送 AMQP 消息的AmqpHeaderMapper的引用。 默认情况下,仅将标准 AMQP 属性(如contentType)复制到 Spring Integration MessageHeaders。 默认`DefaultAmqpHeaderMapper`不会复制任何用户定义的头部到消息中。 如果提供了'request-header-names',则不允许使用。 可选。
18 以逗号分隔的 AMQP 头名称列表,用于从 MessageHeaders 映射到 AMQP 消息。 如果提供了 'header-mapper' 引用,则不允许使用此属性。 此列表中的值也可以是用于匹配头名称的简单模式(例如 "*""thing1*, thing2""*thing1")。
19 当设置为 false 时,端点会在应用程序上下文初始化期间尝试连接到代理。 这允许快速检测错误的配置,但如果代理宕机,也会导致初始化失败。 当设置为 true(默认值)时,连接将在发送第一条消息时建立(如果尚未由其他组件建立)。
20 当设置为true时,类型为Iterable<Message<?>>的有效负载将作为离散消息在单个RabbitTemplate调用的范围内通过同一通道发送。 需要一个RabbitTemplate。 当wait-for-confirms为true时,在消息发送后会调用RabbitTemplate.waitForConfirmsOrDie()。 使用事务模板时,发送操作将在新事务或已启动的事务(如果存在)中执行。
return-channel

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