- 创建一个类BIOServer
@Component
public class BioServer {
@Autowired
AsyncService asyncService;
public ServerSocket serverSocket = null;
public void BioServerListen() throws IOException {
try{
serverSocket = new ServerSocket(8888);
System.out.println("服务启动了");
while(true){
final Socket socket = serverSocket.accept();
System.out.println("连接到一个客户端");
asyncService.dataHandler(socket);
}
}catch (IOException exception){
}
}
}
- AsyncService(为了偷懒没有接口+实现类的形式),调用其中使用了 异步方法处理,从而实现多线程可连接多个客户端
package com.mql.nettybio.BioServer;
import com.mql.nettybio.mapper.AdcpMapper;
import com.mql.nettybio.bean.ADCP_DataInfo;
import com.mql.nettybio.config.GetDataInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.text.ParseException;
@Component
public class AsyncService {
@Autowired
AdcpMapper adcpMapper;
private Logger logger = Logger.getLogger(AsyncService.class);
@Async("asyncServiceExecutor")
public void dataHandler(Socket socket){
System.out.println("Spring自带的线程池" + Thread.currentThread().getName());
InputStream inputStream = null;
try {
byte[] bytes = new byte[1024];
inputStream = socket.getInputStream();
while(true){
int read = inputStream.read(bytes);
if(read!=-1){
String dataTemp = new String (bytes,0,read,"GBK");
logger.info(dataTemp);
}else {
break;
}
}
} catch (IOException | ParseException exception) {
exception.printStackTrace();
logger.error(exception);
}finally {
System.out.println("关闭和client的连接");
logger.info("关闭和client的连接");
try{
if(inputStream!=null){
inputStream.close();
}
}catch (Exception exception){
exception.printStackTrace();
}
try{
if(socket!=null){
socket.close();
}
}catch (Exception exception){
exception.printStackTrace();
}
}
}
}
- 异步线程的配置类
package com.mql.nettybio.config;
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;
@Configuration
@EnableAsync
public class ExecutorConfig {
@Bean
public Executor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(99999);
executor.setThreadNamePrefix("async-service-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
- 在SpiringBoot启动类中调用
package com.mql.nettybio;
import com.mql.nettybio.BioServer.BioServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.io.IOException;
@SpringBootApplication
@EnableScheduling
public class NettyBioApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(NettyBioApplication.class, args).getBean(BioServer.class).BioServerListen();
}
}