我有一个服务器和多个客户端,服务器将发送响应并等待确认,另外我想永远保持该连接以用于下一条消息和确认我应该如何在Spring Integration中创建这些连接。我读到了Spring集成,我无法找到保持连接的解决方案。
public class ClientCall {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx = new AnnotationConfigApplicationContext(GatewayConfig.class);
GatewayService gatewayService = ctx.getBean(GatewayService.class);
//int i=0;
Message message = new Message();
/*while(i<4)
{*/
message.setPayload("It's working");
gatewayService.sendMessage(message);
/* i++;
}*/
}
}
@Service
public interface GatewayService<T> {
public void sendMessage(final T payload);
public void receiveMessage(String response);
}
@EnableIntegration
@IntegrationComponentScan
@Configuration
@ComponentScan(basePackages = "com.gateway.service")
public class GatewayConfig {
// @Value("${listen.port:6788}")
private int port = 6785;
@Autowired
private GatewayService<Message> gatewayService;
@MessagingGateway(defaultRequestChannel = "sendMessageChannel")
public interface Gateway {
void viaTcp(String payload);
}
@Bean
public AbstractClientConnectionFactory clientCF() {
TcpNetClientConnectionFactory clientConnectionFactory = new TcpNetClientConnectionFactory("localhost",this.port);
clientConnectionFactory.setSingleUse(true);
return clientConnectionFactory;
}
@Bean
@ServiceActivator(inputChannel = "sendMessageChannel")
public MessageHandler tcpOutGateway(AbstractClientConnectionFactory connectionFactory) {
TcpOutboundGateway outGateway = new TcpOutboundGateway();
outGateway.setConnectionFactory(connectionFactory);
outGateway.setAsync(true);
outGateway.setOutputChannel(receiveMessageChannel());
outGateway.setRequiresReply(true);
outGateway.setReplyChannel(receiveMessageChannel());
return outGateway;
}
@Bean
public MessageChannel sendMessageChannel() {
DirectChannel channel = new DirectChannel();
return channel;
}
@Bean
public MessageChannel receiveMessageChannel() {
DirectChannel channel = new DirectChannel();
return channel;
}
@Transformer(inputChannel = "receiveMessageChannel", outputChannel = "processMessageChannel")
public String convert(byte[] bytes) {
return new String(bytes);
}
@ServiceActivator(inputChannel = "processMessageChannel")
public void upCase(String response) {
gatewayService.receiveMessage(response);
}
@Transformer(inputChannel = "errorChannel", outputChannel = "processMessageChannel")
public void convertError(byte[] bytes) {
String str = new String(bytes);
System.out.println("Error: " + str);
}
}
public class Message {
private String payload;
// getter setter
}
@Service
public class GatewayServiceImpl implements GatewayService<Message> {
@Autowired
private Gateway gateway;
@Autowired
private GatewayContextManger<String, Object> gatewayContextManger;
@Override
public void sendMessage(final Message message) {
new Thread(new Runnable() {
@Override
public void run() {
gateway.viaTcp(message.getPayload());
}
}).start();
}
@Override
public void receiveMessage(final String response) {
new Thread(new Runnable() {
@Override
public void run() {
Message message = new Message();
message.setPayload(response);
Object obj = message;
//Object obj = gatewayContextManger.get(message.getPayload());
synchronized (message) {
obj.notify();
System.out.println("Message Received : "+message.getPayload());
}
}
}).start();
}
}
答案 0 :(得分:0)
您有:clientConnectionFactory.setSingleUse(true);
这意味着请求后将关闭连接;保持假(默认)以保持连接打开。