반응형
연결을 허용하기 위해 봄 부팅에서 Tcp 연결을 만드는 방법은 무엇입니까?
저는 이 일을 겪었고, 제가 필요하다는 것을 이해했습니다.TcpReceivingChannelAdapter
연결을 허용합니다.하지만 저는 그것을 어떻게 진행해야 할지 모르겠습니다.
누가 이것에 대해 저를 안내해 주시겠습니까?
XML 구성을 사용하는 일부 포인터는 tcp-client-server 샘플을 참조하십시오.
Java 구성을 위한 간단한 Spring Boot 앱이 있습니다.
package com.example;
import java.net.Socket;
import javax.net.SocketFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.transformer.ObjectToStringTransformer;
import org.springframework.messaging.MessageChannel;
@SpringBootApplication
public class So39290834Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(So39290834Application.class, args);
Socket socket = SocketFactory.getDefault().createSocket("localhost", 9999);
socket.getOutputStream().write("foo\r\n".getBytes());
socket.close();
Thread.sleep(1000);
context.close();
}
@Bean
public TcpNetServerConnectionFactory cf() {
return new TcpNetServerConnectionFactory(9999);
}
@Bean
public TcpReceivingChannelAdapter inbound(AbstractServerConnectionFactory cf) {
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(cf);
adapter.setOutputChannel(tcpIn());
return adapter;
}
@Bean
public MessageChannel tcpIn() {
return new DirectChannel();
}
@Transformer(inputChannel = "tcpIn", outputChannel = "serviceChannel")
@Bean
public ObjectToStringTransformer transformer() {
return new ObjectToStringTransformer();
}
@ServiceActivator(inputChannel = "serviceChannel")
public void service(String in) {
System.out.println(in);
}
}
언급URL : https://stackoverflow.com/questions/39290834/how-to-create-a-tcp-connection-in-spring-boot-to-accept-connections
반응형
'sourcecode' 카테고리의 다른 글
Spring Boot - /health 끝점의 위치를 /ping/me로 변경합니다. (0) | 2023.07.07 |
---|---|
GeoFire를 Firestore와 함께 사용할 수 있는 방법이 있습니까? (0) | 2023.07.07 |
SQL Server Management Studio에서 마우스 오른쪽 버튼으로 스크립트 변경 테이블 사용 안 함 (0) | 2023.07.07 |
최대 절전 모드에서 MySQL이 아닌 다른 표준 시간대를 사용하는 경우 날짜를 뺀 날짜를 저장/검색합니다. (0) | 2023.07.07 |
제공자 설치 관리자 모듈을 로드하지 못했습니다.허용 가능한 모듈을 찾을 수 없습니다.로컬 버전은 0이고 원격 버전은 0 오류 변동입니다. (0) | 2023.07.07 |