Mar*_*thy 5 java spring udp spring-mvc
我已经使用 spring boot(版本 1.5.3)框架实现了一个网络应用程序。现在我需要一个 udp 服务器来接收来自客户端的传入消息。如何将此功能添加到我的基于 Spring Boot 的项目中?
我按照How to Implement UDP in Spring Framework链接作为参考,但无法获得wrt spring boot
谁能帮我理解这一点吗
谢谢马鲁西
小智 2
依赖于 spring boot 集成和 spring-ip。
<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>
Run Code Online (Sandbox Code Playgroud)
然后使用“UnicastReceivingChannelAdapter”创建简单的“IntegrationFlow”并将其关联起来,创建一个用于接收 UDP 消息的 bean
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
还可以使用使用“UnicastSendingMessageHandler”的简单客户端将消息发送到 UDP 服务器。
UnicastSendingMessageHandler handler =
new UnicastSendingMessageHandler("localhost", 11111);
String payload = "Hello world";
handler.handleMessage(MessageBuilder.withPayload(payload).build());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3466 次 |
| 最近记录: |