SpringBoot接收UDP消息信息整合Integration

概述

由于HTTP协议对实时数据传输过于慢,为此采用UDP进行数据传输,来看看如何实现的

整合integration

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-ip</artifactId>
</dependency>

创建UDP服务

package io.freeyou.socket.udp.server;

import io.freeyou.modules.internetBehavior.server.InternetBehaviorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.Filter;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletContextListener;
import java.util.Map;

@Slf4j
@Component
public class UdpClientServer implements ServletContextListener {

    @Value("${freeyou.udp.port:3514}")
    private Integer udpPort;

    @Resource
    private InternetBehaviorService internetBehaviorService;

    @Bean("UnicastReceivingChannelAdapter1")
    public UnicastReceivingChannelAdapter getUnicastReceivingChannelAdapter() {
        UnicastReceivingChannelAdapter adapter = new  UnicastReceivingChannelAdapter(udpPort);
        adapter.setOutputChannelName("udpChannel");
        adapter.setReceiveBufferSize(2048000);
        log.info("实例化UDP端口:{}", udpPort);
        return adapter;
    }

    /**
     * 转换器
     */
    @Transformer(inputChannel = "udpChannel", outputChannel = "udpFilter")
    public String transformer(@Payload byte[] payload, @Headers Map<String, Object> headers) {
        String message = new String(payload);
        return message;
    }

    /**
     * 过滤器
     */
    @Filter(inputChannel = "udpFilter", outputChannel = "udpRouter")
    public boolean filter(String message, @Headers Map<String, Object> headers) {

        return true;
    }

    /**
     * 路由分发处理器
     */
    @Router(inputChannel = "udpRouter")
    public String router(String message, @Headers Map<String, Object> headers) {

        return "udpHandle1";
    }

    /**
     * 最终处理器1
     */
    @ServiceActivator(inputChannel = "udpHandle1")
    public void udpMessageHandle(String message) throws Exception {
        // 可以进行异步处理
        /*ObjectMapper objectMapper = new ObjectMapper();
        CameraReq req = objectMapper.readValue(message, CameraReq.class);
        newLocatorService.storeCameraInfo(req);*/
        log.debug("接收udp数据,message:" + message);
        internetBehaviorService.sendAliLogStore(message);
    }

    /**
     * 最终处理器2
     */
    @ServiceActivator(inputChannel = "udpHandle2")
    public void udpMessageHandle2(String message) throws Exception {
        log.info("UDP2:" + message);
    }
}

配置文件yml

freeyou:
    udp:
        port: 3514
        max-size: 4096

测试结果

在这里插入图片描述
在这里插入图片描述
UDP客户端工具sockettool下载地址
链接:https://pan.baidu.com/s/18zDilHG6CvMvxVmJlL-5Yw?pwd=xbqa
提取码:xbqa

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot中处理超过2200字节的UDP数据包时,可以使用Java的原生`DatagramSocket`和`DatagramPacket`来实现。以下是一个简单的示例代码: ```java import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @Component public class UdpReceiver { private static final int BUFFER_SIZE = 65507; // 设置合适的缓冲区大小 @Async public void startUdpServer() { try (DatagramSocket socket = new DatagramSocket(8888)) { byte[] buffer = new byte[BUFFER_SIZE]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); while (true) { socket.receive(packet); // 接收UDP数据包 String message = new String(packet.getData(), 0, packet.getLength()); System.out.println("Received message: " + message); // 处理接收到的数据 // 清空缓冲区 packet.setLength(buffer.length); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在Spring Boot的主类中,你可以将`UdpReceiver`注入为一个Bean,并通过`@Async`注解使其异步执行: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync public class Application { @Autowired private UdpReceiver udpReceiver; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @PostConstruct public void startUdpServer() { udpReceiver.startUdpServer(); } } ``` 这样,你就可以在Spring Boot应用中处理超过2200字节的UDP数据包了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redstone618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值