Spring Integration TCP. Get connection ID of the connected clients

僤鯓⒐⒋嵵緔 提交于 2019-12-22 12:58:25

问题


I have a problem here with the dynamic TCP connection approach (Spring-IP Dynamic FTP Sample). When a message is received, I want to get the TCP connection details for the received message. this way I can keep track in my application of the sender sending that message. But in Service activator I am not able to get this detail.

Also need the connection details when my TCP client is connected to the server. This way if the server wants to initiate the communication, it will have the connection details.

For info my application has more than one TCP clients and servers.


回答1:


Got an answer reply in another post from Mr. Gary Russell.

Answer

For normal request/reply processing, using an inbound gateway, the framework will take care of routing the service activator reply to the correct socket. It does this by using the connection id header.

If you need to provide arbitrary replies (e.g. more than one reply for a message, you have to use inbound and outbound channel adapters and your application is responsible for setting up the connection id header.

There are two ways to access the required header in a POJO invoked by a service activator:

public void foo(byte[] payload, @Header(IpHeaders.CONNECTION_ID) String connectionId) {
    ...
}

public void foo(Message<byte[]> message) {
    String connectionId = message.getHeaders().get(...);
}

Then, when you send your replies, you need to set that header somehow.

EDIT

Below Is My Implementation

To get all the connected clients simply get the ServerConnectionFactory from the context and access the method .getConnectedClients(). It returns the list connectionIds for each connected client.

AbstractServerConnectionFactory connFactory = (AbstractServerConnectionFactory) appContext.getBean("server");
List<String> openConns = connFactory.getOpenConnectionIds();

As mentioned above in Gary's response, use this connectionId and set it in conneciton header while sending the message to a client. Sample code as follows.

MessageChannel serverOutAdapter = null;
try{
    serverOutAdapter = (MessageChannel) appContext.getBean("toObAdapter");
}catch(Exception ex){
    LOGGER.error(ex.getMessage());
    throw ex;
}

    if(null == serverOutAdapter){
        throw new Exception("output channel not available");
    }

    AbstractServerConnectionFactory connFactory = (AbstractServerConnectionFactory) appContext.getBean("serverConnFactoryBeanId");
    List<String> openConns = connFactory.getOpenConnectionIds();
    if(null == openConns || openConns.size() == 0){

        throw new Exception("No Client connection registered");
    }

    for (String connId: openConns) {
        MessageBuilder<String> mb = MessageBuilder.withPayload(message).setHeader(IpHeaders.CONNECTION_ID, connId);
        serverOutAdapter.send(mb.build());
    }

Note 1: If u want to send messages from the server then be cautious to configure the server and client connection factories in a way that they do not time-out. i.e put so-keep-alive = true in client connection factory.

Note 2: If the server has to communicate with the client then make sure that the client connects to the server as soon as the context is loaded. Because Spring-IP client connection factory connects only when the first message is sent out. In order to connect client after context load, put client-mode="true" in tcp client context for the "tcp-outbound-channel-adapter".



来源:https://stackoverflow.com/questions/39062895/spring-integration-tcp-get-connection-id-of-the-connected-clients

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