如果未连接外部服务,如何使用 `ReceiveMessageAdvice` 添加 Smart "Polling" 以防止轮询
How to add Smart "Polling" with `ReceiveMessageAdvice` to prevent polling if an external service is not connected
我当前的工作 XML 配置如下所示:
生产者 -> 网关 -> 队列通道(通过 Oracle 数据库持久化) -> 接收者服务 -> 具有外部会话的服务(例如 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>
现在我想添加 Smart "Polling" 和 ReceiveMessageAdvice
以防止在服务激活器 outboundFixMessageService
没有与外部连接的活动会话时进行轮询。
但是我不知道如何配置它,而且我找不到示例项目在哪里查找。
如果您能提出一些建议,那就太好了。
提前致谢
ReceiveMessageAdvice
有为您的目的覆盖的方法:
/**
* 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;
}
因此,如果您有一个钩子来检查该会话的状态,您只需从该方法 return false
并且不会进行轮询。
我当前的工作 XML 配置如下所示:
生产者 -> 网关 -> 队列通道(通过 Oracle 数据库持久化) -> 接收者服务 -> 具有外部会话的服务(例如 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>
现在我想添加 Smart "Polling" 和 ReceiveMessageAdvice
以防止在服务激活器 outboundFixMessageService
没有与外部连接的活动会话时进行轮询。
但是我不知道如何配置它,而且我找不到示例项目在哪里查找。
如果您能提出一些建议,那就太好了。
提前致谢
ReceiveMessageAdvice
有为您的目的覆盖的方法:
/**
* 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;
}
因此,如果您有一个钩子来检查该会话的状态,您只需从该方法 return false
并且不会进行轮询。