How to extend TCP Outbound gateway to just receive messages in Spring Integration

北战南征 提交于 2021-02-04 21:52:20

问题


I am developing an web application which supports TCP connection by Spring Integration. It has two functions.

  1. send messages to Server and receive reply from Server at a time
  2. 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.

  1. this application has no database.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!