首先了解几个概念:
ThreadPoolTaskExecutor是spring core包中的,而ThreadPoolExecutor是JDK中的JUC。ThreadPoolTaskExecutor是对ThreadPoolExecutor进行了封装处理
@Async注解使用条件:指定执行线程池,当其被标注在类或者方法上,用于实现方法的异步执行,当被标注在类上,表明类中的所有方法都被指定的异步执行器执行
@Async注解一般用在类的方法上,如果用在类上,那么这个类所有的方法都是异步执行的;
所使用的@Async注解方法的类对象应该是Spring容器管理的bean对象;
调用异步方法类上需要配置上注解@EnableAsync
① 我们先准备个线程池,供UDP调用:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
@Data
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "threadpool",ignoreInvalidFields = true)
public class ThreadPoolProperties {
private int corePoolSize=5;
private int keepAliveSeconds = 60;
private int maxPoolSize=Runtime.getRuntime().availableProcessors();
private int queueuCapacity=25;
private String threadNamePrefix="dachao-";
private Class<? extends RejectedExecutionHandler> rejectedExecutionHandler = ThreadPoolExecutor.CallerRunsPolicy.class;
}
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionHandler;
@Configuration
@EnableAsync
@EnableConfigurationProperties({
ThreadPoolProperties.class})
public class ExecutorConfig {
@Autowired
private ThreadPoolProperties threadPoolProperties;
public ExecutorConfig(){
}
@Bean("getasyncserviceexecutor")
public Executor asyncServiceExecutor(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(this.threadPoolProperties.getCorePoolSize());
executor.setKeepAliveSeconds(this.threadPoolProperties.getKeepAliveSeconds());
executor.setMaxPoolSize(this.threadPoolProperties.getMaxPoolSize());
executor.setThreadNamePrefix(this.threadPoolProperties.getThreadNamePrefix());
RejectedExecutionHandler handler =
(RejectedExecutionHandler) BeanUtils.instantiateClass(this.threadPoolProperties.getRejectedExecutionHandler());
executor.setRejectedExecutionHandler(handler);
return executor;
}
}
② 我们正式进入UDP部分
一、我们先写一个类,在系统启动执行,多线程启动UDP服务端
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class UdpRunner implements InitializingBean {
@Autowired
private UDPProcess udpProcess;
@Override
public void afterPropertiesSet() throws Exception {
new Thread(udpProcess).start();
}
}
二、写一个UDP服务器端(最主要的类)
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
@Component
@Slf4j
public class UDPProcess implements Runnable {
/***
*这里的端口配置在yml里面(这个随项目需求而配置,非固定)
*port 9999 maxudpdatasize 8192
**/
@Value("${udp.port}")
private Integer port;
@Value("${udp.maxudpdatasize}")
private Integer maxUdpDataSize;
private DatagramSocket socket;
@Autowired
private Process process;
@Override
public void run() {
try {
log.info("========当前启动线程:{}=========",Thread.currentThread().getName());
this.socket = new DatagramSocket(port);
log.info("=========UDP连接成功,占用端口:{}=========", socket.getLocalPort());
} catch (SocketException e) {
e.printStackTrace();
log.error("=========UDP连接失败=========");
}
log.info("=========创建数据包,用于接收客户端数据=========");
while (true) {
byte[] buffer = new byte[maxUdpDataSize];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
log.info("=========等待接收UDP数据包=========");
try {
socket.receive(packet);
process.processRun(packet);
} catch (IOException e) {
log.info("=========接收UDP数据包失败!=========");
e.printStackTrace();
}
}
}
}
三、接收的数据处理类,这里使用@Async(“getasyncserviceexecutor”)获取多线程异步推送数据
package com.sinux.cete.socket.udp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sinux.cete.socket.socketio.entity.PushMessage;
import com.sinux.cete.socket.socketio.service.ISocketIOService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
/**
* @author Admin
*/
@Component
@Slf4j
public class Process {
@Autowired
private ISocketIOService socketIOService;
@Async("getasyncserviceexecutor")
public void processRun(DatagramPacket packet) {
byte[] buffer = packet.getData();
try {
String jsonString = new String(buffer,"UTF-8");
log.info("=====从{}:{}接收到UDP数据包:{}==================",packet.getAddress().getHostAddress(),packet.getPort(),jsonString);
//TODO 使用SOCKET.IO推送数据
//1.2 将json字符串转成Person对象
//Person person = JSONArray.parseObject(json, Person.class);
PushMessage pushMessage = JSONArray.parseObject(jsonString,PushMessage.class);
socketIOService.pushMessageToUser(pushMessage);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
四、可以写个测试类,用于本地测试(也可不写,非必须)模拟发送数据,只要知道UDP服务端启动的ip和端口就可以发送数据过去
package com.sinux.cete.socket.udp;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sinux.cete.socket.socketio.entity.PushMessage;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;
/**
* @author Admin
*/
public class UdpClient {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket();
int i = 1;
while(true){
System.out.println("早安,中国"+i);
PushMessage pushMessage = new PushMessage();
pushMessage.setName("东方不败!");
pushMessage.setAge(12);
pushMessage.setBalance(2222L);
pushMessage.setPhone("12312312312");
pushMessage.setIp("23423");
Object obj = JSONArray.toJSON(pushMessage);
String s= obj.toString();
byte[] buffer = (s).getBytes("UTF-8");
DatagramPacket packet = new DatagramPacket(buffer,buffer.length, InetAddress.getByName("localhost"),9999);
socket.send(packet);
i++;
TimeUnit.SECONDS.sleep(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:udp是启动在它自己的端口(例如我们设定为9999 )而不是本项目的端口
结构图: