首页 > 解决方案 > 如果未连接外部服务,如何使用 `ReceiveMessageAdvice` 添加智能“轮询”以防止轮询

问题描述

我当前的工作 XML 配置如下所示:

生产者 -> 网关 -> 队列通道(保留在 Oracle DB 上) -> 接收者服务 -> 具有外部会话的服务(例如 TCP/IP)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="outboundFixMessageService" class="com.myapp.OutboundFixMessageService"/>


  <int:gateway service-interface="com.myapp.queue.DpOutgoingMessageGateway"
               default-request-channel="outgoingChannel"></int:gateway>

  <int:channel id="outgoingChannel">
    <int:queue message-store="outgoingMessageChannelStore"/>
  </int:channel>

  <int:service-activator ref="outboundFixMessageService"
                         method="processOutgoingMessageString"
                         input-channel="outgoingChannel">
    <int:poller fixed-rate="10" time-unit="SECONDS" max-messages-per-poll="1000">
      <int:transactional propagation="NESTED" />
    </int:poller>
  </int:service-activator>

 <!-- Persisting queue -->
  <bean id="outgoingMessageChannelStore" class="org.springframework.integration.jdbc.store.JdbcChannelMessageStore">
    <property name="dataSource" ref="dataSource"/>
    <property name="channelMessageStoreQueryProvider" ref="jdbcChannelMessageStoreQueryProvider"/>
    <property name="region" value="TX_TIMEOUT"/>
  </bean>

  <bean id="jdbcChannelMessageStoreQueryProvider" class="org.springframework.integration.jdbc.store.channel.H2ChannelMessageStoreQueryProvider" />

  <int:transaction-synchronization-factory id="jdbcChannelMessageStoreFactory">
    <int:after-commit expression="@jdbcChannelMessageStore.removeFromIdCache(headers.id.toString())" />
    <int:after-rollback expression="@jdbcChannelMessageStore.removeFromIdCache(headers.id.toString())" />
  </int:transaction-synchronization-factory>
</beans>

现在,如果服务激活器没有与外部连接的活动会话,我想添加智能“轮询”ReceiveMessageAdvice防止轮询。outboundFixMessageService

但是我不知道如何配置它,我又找不到在哪里查找示例项目。

如果您能提出一些建议,那就太好了。

提前致谢

标签: springspring-bootspring-integration

解决方案


ReceiveMessageAdvice为您的目的覆盖的has 方法:

/**
 * Subclasses can decide whether to {@link MethodInvocation#proceed()} or not.
 * @param source the source of the message to receive.
 * @return true to proceed (default).
 */
default boolean beforeReceive(Object source) {
    return true;
}

因此,如果您有一个钩子来检查该会话的状态,您只需false从该方法返回并且不会发生轮询。


推荐阅读