Мы сталкиваемся с проблемой, когда соединение, созданное в одном потоке, используется с другим потоком, когда появляется новый запрос. Это происходит, хотя каждый запрос должен иметь собственное независимое соединение. Общий. 2.1.0
[*] Jdk 1.8
ниже приведен наш код tcpclientconfig:
Код: Выделить всё
@EnableIntegration
@Configuration
@RequiredArgsConstructor
@Slf4j
@ComponentScan
public class TcpClientConfig implements ApplicationEventPublisherAware {
private final Properties properties;
private static final long TIMEOUT = 20000L;
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@Bean
public AbstractClientConnectionFactory clientConnectionFactory() {
TcpNioClientConnectionFactory tcpNioClientConnectionFactory = new TcpNioClientConnectionFactory(properties.getIp(), properties.getPort());
tcpNioClientConnectionFactory.setUsingDirectBuffers(true);
tcpNioClientConnectionFactory.setSingleUse(true);
tcpNioClientConnectionFactory.setSerializer(new ByteArrayCrSerializer());
tcpNioClientConnectionFactory.setDeserializer(new ByteArrayCrSerializer());
tcpNioClientConnectionFactory.setApplicationEventPublisher(applicationEventPublisher);
return tcpNioClientConnectionFactory;
}
public void closeConnection() throws Exception {
try {
TcpConnectionSupport oldConnection = clientConnectionFactory().getConnection();
if (oldConnection != null) {
oldConnection.close();
}
} catch (Exception e){
throw new Exception("error", e);
}
}
@Bean
public MessageChannel outboundChannel() {
return new DirectChannel();
}
@Bean
public QueueChannel inboundChannel() {
return new QueueChannel();
}
@Bean
@ServiceActivator(inputChannel = "outboundChannel")
public MessageHandler outboundGateway(AbstractClientConnectionFactory clientConnectionFactory) {
TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
tcpOutboundGateway.setConnectionFactory(clientConnectionFactory);
tcpOutboundGateway.setRequestTimeout(TIMEOUT);
tcpOutboundGateway.setRemoteTimeout(TIMEOUT);
tcpOutboundGateway.setReplyChannel(inboundChannel());
return tcpOutboundGateway;
}
@EventListener
public void handleTcpConnectionEvent(TcpConnectionOpenEvent event) {
log.info("============================== TCP Connection Opened : {} ==============================", event.getConnectionId());
}
@EventListener
public void handleTcpConnectionCloseEvent(TcpConnectionCloseEvent event) {
log.info("============================== TCP Connection Closed : {} ==============================", event.getConnectionId());
}
}
@Service
@Slf4j
@RequiredArgsConstructor
@ComponentScan
public class TcpMessageService {
private final TcpClientConfig tcpClientConfig;
public void sendMessage(byte[] tcpData) {
Message message = MessageBuilder.withPayload(tcpData).build();
tcpClientConfig.outboundChannel().send(message);
}
public void sendAck() {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
ByteArrayCrSerializer serializer = new ByteArrayCrSerializer();
serializer.serialize(ACK.getBytes(StandardCharsets.UTF_8), byteArrayOutputStream);
byte[] serializedAck = byteArrayOutputStream.toByteArray();
Message ackMessage = MessageBuilder.withPayload(serializedAck).build();
tcpClientConfig.outboundChannel().send(ackMessage);
} catch (IOException e) {
log.error("error", e);
}
}
public Message receiveMessage() {
return tcpClientConfig.inboundChannel().receive(TIMEOUT);
}
}
< /code>
[желаемое решение] < /p>
Нам нужен способ обеспечить, чтобы каждый поток создавал и использует свое собственное соединение, не обменивая его с другими потоками. Каждый новый запрос из другого потока всегда должен устанавливать свежее соединение, полностью независимо от любых предыдущих соединений. Любые предложения или альтернативные подходы были бы очень оценены!
Подробнее здесь: https://stackoverflow.com/questions/795 ... ration-tcp