gpt4 book ai didi

java - 如何使用 spring-integration 创建异步单例套接字服务器?

转载 作者:行者123 更新时间:2023-12-02 11:07:40 24 4
gpt4 key购买 nike

我想使用spring-integration实现以下目标:拥有一个不断接收和写入数据的单例开放套接字,异步!

这意味着我必须打开一个不断从单个套接字读取数据的套接字,分派(dispatch)每条消息进行异步处理,并通过套接字返回异步响应。

如何实现异步模式?

特别是:如何使用序列化器/反序列化器?据我了解,序列化器仅在套接字连接上调用,因此在我的情况下仅在第一条消息开始时调用一次?

@Configuration
public class SocketConfig {
@Bean
public TcpConnectionFactoryFactoryBean tcpFactory(MyConverter converter) {
TcpConnectionFactoryFactoryBean fact = new TcpConnectionFactoryFactoryBean();
fact.setType("server");
fact.setPort(PORT);
fact.setUsingNio(true); //should I use true or false?
fact.setSingleUse(false); //keep socket constantly open
fact.setSerializer(converter);
fact.setDeserializer(converter);
return fact;
}

@Bean
public TcpInboundGateway serverGateway(
@Qualifier("tcpFactory") TcpConnectionFactoryFactoryBean factory,
@Qualifier("serverChannel") MessageChannel serverChannel) throws Exception {
TcpInboundGateway g = new TcpInboundGateway();
g.setConnectionFactory(factory.getObject());
g.setRequestChannel(serverChannel);
return g;
}

}

@MessageEndpoint
public class SocketEndpoint {

@ServiceActivator(inputChannel = "serverChannel")
public Object run(Object obj) {

}
}


@Service
public class MyConverter implements Serializer<Object>, Deserializer<Object> {
//read from socket
@Override
public Object deserialize(InputStream inputStream) {
}

//send back to socket
@Override
public void serialize(Object message, OutputStream outputStream) {
}
}

最佳答案

网关用于单独的请求/响应对。

如果您需要为单个请求发送多个响应,则必须使用 collaborating channel adapters as described in the documentation .

Collaborating adapters can also be used (server-side or client-side) for totally asynchronous communication (rather than with request/reply semantics).

On the server side, care must be taken to populate the ip_connectionId header because it is used to correlate the message to a connection. Messages that originate at the inbound adapter will automatically have the header set. If you wish to construct other messages to send, you will need to set the header. The header value can be captured from an incoming message.

关于java - 如何使用 spring-integration 创建异步单例套接字服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50833803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com