How to add Smart "Polling" with `ReceiveMessageAdvice` to prevent polling if an external service is not connected

Viewed 169

My current working XML configuration looks like this:

Producer -> Gateway -> Queue Channel (persisted over Oracle DB) -> Receiver Service -> Service with External Session(e.g 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>

Now I would like to add Smart "Polling" with ReceiveMessageAdvice to prevent polling if service activator outboundFixMessageService does not have an active session with the external connection.

However I do not know how to configure this and again I could not find example project where to look it up.

Would be great if you could suggest something.

Thanks in advance

1 Answers

The ReceiveMessageAdvice has method to override for your purpose:

/**
 * 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;
}

So, if you have a hook to check the state for that session, you just return false from that method and polling is not going to happen.

Related