Commit 92d8a681 authored by zengtianlai3's avatar zengtianlai3

使用线程池替代原生线程

parent 905a535b
...@@ -4,11 +4,12 @@ import org.mybatis.spring.annotation.MapperScan; ...@@ -4,11 +4,12 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableAsync
@ServletComponentScan(basePackages ="iot.sixiang.license") @ServletComponentScan(basePackages ="iot.sixiang.license")
@SpringBootApplication @SpringBootApplication
@EnableScheduling @EnableScheduling
......
package iot.sixiang.license.config;
/**
* Title: ThreadPoolConfig
* Description: TODO
*
* @author tianlai3
* @date 2022-07-16 20:05:38
*/
import lombok.Data;
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.ThreadPoolExecutor;
@Data
@Configuration
@EnableAsync
public class ThreadPoolConfig {
private static final int corePoolSize = 1; // 核心线程数(默认线程数)
private static final int maxPoolSize = 2; // 最大线程数
private static final int keepAliveTime = 10; // 允许线程空闲时间(单位:默认为秒)
private static final int queueCapacity = 2; // 缓冲队列数
/**
* 默认异步线程池
* @return
*/
@Bean("taskExecutor")
public Executor taskExecutor(){
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setThreadNamePrefix("threadPoll-");
pool.setCorePoolSize(corePoolSize);
pool.setMaxPoolSize(maxPoolSize);
pool.setKeepAliveSeconds(keepAliveTime);
pool.setQueueCapacity(queueCapacity);
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
pool.initialize();
return pool;
}
}
package iot.sixiang.license.device;
import iot.sixiang.license.net.TcpServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* Title: AsyncTcpServer
* Description: TODO
*
* @author tianlai3
* @date 2022-07-16 20:30:51
*/
@Component
@Slf4j
public class AsyncTcpServer {
@Async("taskExecutor")
public void start(int port, DeviceChannelInitializer channelInitializer) {
TcpServer server = new TcpServer(port, channelInitializer);
server.start();
}
}
...@@ -5,7 +5,6 @@ import iot.sixiang.license.model.PageInfoModel; ...@@ -5,7 +5,6 @@ import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.model.SessionContext; import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.model.vo.DeviceDetailVo; import iot.sixiang.license.model.vo.DeviceDetailVo;
import iot.sixiang.license.model.vo.DeviceVo; import iot.sixiang.license.model.vo.DeviceVo;
import iot.sixiang.license.net.TcpServer;
import iot.sixiang.license.service.DeviceService; import iot.sixiang.license.service.DeviceService;
import iot.sixiang.license.util.CommonUtil; import iot.sixiang.license.util.CommonUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -15,7 +14,6 @@ import org.springframework.stereotype.Component; ...@@ -15,7 +14,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.*; import java.util.*;
@Component @Component
@Slf4j @Slf4j
public class DeviceManager { public class DeviceManager {
...@@ -23,13 +21,15 @@ public class DeviceManager { ...@@ -23,13 +21,15 @@ public class DeviceManager {
private Map<String, SessionContext> sessionContexts = null; private Map<String, SessionContext> sessionContexts = null;
private DeviceChannelInitializer channelInitializer; private DeviceChannelInitializer channelInitializer;
private TcpServer server = null; // private TcpServer server = null;
private int port = 18889; private int port = 18889;
private Map<String, DeviceVo> allDevice = null; private Map<String, DeviceVo> allDevice = null;
@Autowired @Autowired
private DeviceService deviceService; private DeviceService deviceService;
@Autowired @Autowired
private DeviceServerHandler handler; private DeviceServerHandler handler;
@Autowired
private AsyncTcpServer asyncTcpServer;
public DeviceManager() { public DeviceManager() {
sessionContexts = new HashMap<String, SessionContext>(); sessionContexts = new HashMap<String, SessionContext>();
...@@ -42,11 +42,11 @@ public class DeviceManager { ...@@ -42,11 +42,11 @@ public class DeviceManager {
initDevices(); initDevices();
} }
private void startTcpService() { private void startTcpService() {
sessionContexts = new HashMap<String, SessionContext>(); sessionContexts = new HashMap<String, SessionContext>();
channelInitializer = new DeviceChannelInitializer(handler); channelInitializer = new DeviceChannelInitializer(handler);
server = new TcpServer(port, channelInitializer); asyncTcpServer.start(port, channelInitializer);
server.start();
} }
public void initDevices() { public void initDevices() {
......
...@@ -20,13 +20,11 @@ public class TcpServer { ...@@ -20,13 +20,11 @@ public class TcpServer {
} }
public void start() { public void start() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//创建两个线程组 bossGroup、workerGroup //创建两个线程组 bossGroup、workerGroup
EventLoopGroup bossGroup = new NioEventLoopGroup(4); EventLoopGroup bossGroup = new NioEventLoopGroup(4);
EventLoopGroup workerGroup = new NioEventLoopGroup(4); EventLoopGroup workerGroup = new NioEventLoopGroup(4);
log.debug("Tcp服务,开始监听端口:{}",port); log.debug("Tcp服务,开始监听端口:{}", port);
//创建服务端的启动对象,设置参数 //创建服务端的启动对象,设置参数
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
//设置两个线程组boosGroup和workerGroup //设置两个线程组boosGroup和workerGroup
...@@ -56,7 +54,4 @@ public class TcpServer { ...@@ -56,7 +54,4 @@ public class TcpServer {
} }
} }
} }
});
thread.start();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment