I see quite a few articles about Spring Framework, but I can't find any articles about Spring Integration, probably because they are not major in Japan, so I would like to post them irregularly. (I'm not saying I'll post: sunglasses :)
TcpOutBoundGateway.The handleRequestMessage of the TcpOutboundGateway.
In this method, we are sending a message to the server using TcpConnection obtained from ClientConnectionFactory.
↓ TcpOutboundGateway message transmitter (partially omitted)
TcpOutboundGateway.java
protected Object handleRequestMessage(Message<?> requestMessage) {
TcpConnection connection = null;
String connectionId = null;
try {
connection = this.connectionFactory.getConnection();
AsyncReply reply = new AsyncReply(this.remoteTimeoutExpression.getValue(this.evaluationContext,
requestMessage, Long.class));
connectionId = connection.getConnectionId();
this.pendingReplies.put(connectionId, reply);
if (logger.isDebugEnabled()) {
logger.debug("Added pending reply " + connectionId);
}
connection.send(requestMessage);
Normally, it is not necessary to check when sending, but depending on the case, there may be a requirement such as "monitor the status of a component and do not send a message depending on that status." That's the deal I'm working on right now: v:
For the time being, there are two methods I have come up with.
Well, it's a straightforward idea ...
Extend TcpOutboundGateway to override handleRequestMessage.
Send processing is performed only when the return value of getStatus is true.
CustomizedTcpOutboundGateway.java
public class CustomizedTcpOutboundGateway extends TcpOutboundGateway {
@Override
public Object handleRequestMessage(Message<?> message) {
if (getStatus()) {
return super.handleRequestMessage(message);
} else {
return new GenericMessage<String>("can not send");
}
}
The return value of the original handleRequestMessage is of type ʻObject, but in the case of a normal system, the received message is returned, so an appropriate Message` object is returned. This depends on your requirements.
The method of ↑ is fine, but if you don't want to extend the framework class too much, there is a way to utilize ʻInterceptor`. The procedure is as follows.
First, extend TcpConnectionInterceptor and insert a check process when sending a message.
TcpConnectionInterceptor.java
public class TcpConnectionInterceptor extends TcpConnectionInterceptorSupport {
@Override
public void send(Message<?> message) throws Exception {
if (getStatus()) {
super.send(message);
}
}
//Abbreviation
Send a message with the value of getStatus (). Since TcpConnectionInterceptorSupport has multiple methods around the connection, it is possible to insert processing at timings other than before sending like this time: hugging :.
Next, create a class that implements the TcpConnectionInterceptorFactory that returns the TcpConnectionInterceptor that you just created. The implementation is appropriate.
CustomizedTcpConnectionInterceptorFactory.java
public class CustomizedTcpConnectionInterceptorFactory implements TcpConnectionInterceptorFactory {
private final TcpConnectionInterceptorSupport interceptor;
public CustomizedTcpConnectionInterceptorFactory(TcpConnectionInterceptorSupport interceptor) {
this.interceptor = interceptor;
}
@Override
public TcpConnectionInterceptorSupport getInterceptor() {
return interceptor;
}
}
After that, pass this to TcpConnectionInterceptorFactoryChain and set it to ClientConnectionFactory.
<int-ip:tcp-connection-factory type="client" host="localhost" port="56789"
deserializer="deserializer" serializer="serializer" interceptor-factory-chain="interceptorFactoryChain" />
<bean id="interceptorFactoryChain"
class="org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain">
<property name="interceptors">
<array>
<bean
class="com.neriudon.sample.interceptor.CustomizedTcpConnectionInterceptorFactory"
c:interceptor-ref="tcpConnectionInterceptor" />
</array>
</property>
</bean>
<bean id="tcpConnectionInterceptor"
class="com.neriudon.sample.interceptor.TcpConnectionInterceptor" />
The ClientConnectionFactory settings are appropriate. : frowning2:
This method is a little complicated, but is it an advantage that you don't mess with the original Gateway?
As a memo because this function was needed for the project I am currently involved in.
Even if you google with the class name of Spring Integration, there are many cases where Japanese sites do not hit, so please try Spring Integration as well. : kissing_heart:
Recommended Posts