问题
I am developing an web application which supports TCP connection by Spring Integration. It has two functions.
- send messages to Server and receive reply from Server at a time
- just receive messages from Server
(The application on server is not developed by Spring Integration.)
In this case, TCP Adapters should be used and I need provide to collaborate TCP Outbound and Inbound Channel Adapters. But, my application has a few restrictions.
- this application has no database.
- messages' payload format is already specified. (It means I can not add any correlation data such as a transaction id to payload .
So, I think that collaborating TCP Outbound and Inbound Channel Adapters is difficult. Then, I plan to extend TCP Outbound gateway to add just receiving messages function. How should I extend it ? (or do you have other ideas?)
Thanks in advance.
回答1:
If you can determine the message type, something like this should work...
public class ExtendedTcpOutpboundGateway extends TcpOutboundGateway {
private final MessageChannel unsolicitedMessageChannel;
public ExtendedTcpOutpboundGateway(MessageChannel unsolicitedMessageChannel) {
this.unsolicitedMessageChannel = unsolicitedMessageChannel;
}
@Override
public boolean onMessage(Message<?> message) {
if (isUnsolicitedMessage(message)) {
this.messagingTemplate.send(this.unsolicitedMessageChannel, message);
return false;
}
else {
return super.onMessage(message);
}
}
private boolean isUnsolicitedMessage(Message<?> message) {
// TODO Add logic here to determine message type
return false;
}
}
If you can't tell the difference, it's not clear how you could implement your requirements.
来源:https://stackoverflow.com/questions/46021319/how-to-extend-tcp-outbound-gateway-to-just-receive-messages-in-spring-integratio