配置:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-ip</artifactId> <version>5.1.0.RELEASE</version> </dependency>
服务端:
@Bean public IntegrationFlow processUniCastUdpMessage() { return IntegrationFlows .from(new UnicastReceivingChannelAdapter(11111)) .handle("UDPServer", "handleMessage") .get(); } @Service public class UDPServer { public void handleMessage(Message message) { String data = new String((byte[]) message.getPayload()); System.out.print(data); } }
客户端:
UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler("localhost", 11111); String payload = "Hello world"; handler.handleMessage(MessageBuilder.withPayload(payload).build());
还没有测试行不行,不过感觉应该没问题
标签:UDP,springboot,处理,integration,new,handleMessage,public,String From: https://www.cnblogs.com/tju1895/p/17705727.html