java socket连接池_Java Socket 连接池 :

本文介绍了一个Java Socket连接池的实现,旨在提高服务器并发性能。通过使用ExecutorService线程池,配合不同类型的队列策略,实现了动态调整连接数量,并处理超时和异常情况,提升了服务器的吞吐量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java Socket 连接池 :

目的 为了提高 服务器的并发性能

开源好伟大 ,再次感谢不吝惜自己代码的大拿们,对个人用c++ 写服务器有很大帮助,大大提高了服务器的吞吐量。

package com.nielsenccdata.lbr.core.socket;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;

import java.util.concurrent.ArrayBlockingQueue;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.RejectedExecutionException;

import java.util.concurrent.SynchronousQueue;

import java.util.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

*

* 创建socketconnect的对象,该处是socket连接池及缓冲池

*

* @author sunny.sun

* @version 1.0

*

*/

public class SocketServerManager extends Thread {

private static final Logger logger = LoggerFactory

.getLogger(SocketServerManager.class);

/**

* 端口号

*/

private int port;

/**

* 最大连接数

*/

private int max_conn;

/**

* 缓冲区大小

* */

private int max_cache;

/**

* socket 超时时间

* */

private int timeout;

/**

* server对象

*/

private ServerSocket server;

/**

* 线程池对象

*/

private ExecutorService exService;

public int getMax_cache() {

return max_cache;

}

public void setMax_cache(int max_cache) {

this.max_cache = max_cache;

}

public int getTimeout() {

return timeout;

}

public void setTimeout(int timeout) {

this.timeout = timeout;

}

public int getPort() {

return port;

}

public void setPort(int port) {

this.port = port;

}

public int getMax_conn() {

return max_conn;

}

public void setMax_conn(int max_conn) {

this.max_conn = max_conn;

}

public ServerSocket getServer() {

return server;

}

/**

*

* 该构造方法提供设置最大连接数,最大缓存数,超时时间,此处把socket服务端作为一个单独进程;

* 而把socket连接进程放置到线程池和缓存池,以提高性能

*

* @param port

* 端口号

* @param max_conn

* 最大连接池

* @param max_cache

* 最大缓存池

* @param timeout

* 超时时间

*/

public SocketServerManager(int port, int max_conn, int max_cache,

int timeout) {

this.port = port;

this.max_conn = max_conn;

this.timeout = timeout <= 0 ? 0 : timeout;

this.max_cache = max_cache;

// 将socket连接放到线程池中,如果设置最大连接数为0时,使用自动分配线程

if (max_conn <= 0) {

exService = Executors.newCachedThreadPool();

} else {

// 如果没有设置缓存值,则随系统资源调配

if (max_cache <= 0) {

exService = new ThreadPoolExecutor(0, max_conn, 60000L,

TimeUnit.MILLISECONDS, new SynchronousQueue());// 线程池

return;

}

// 队列池,将无法进入线程池的内容存放socketadapte队列池中

else {

ArrayBlockingQueue socketadaptes = new ArrayBlockingQueue(

max_cache);

exService = new ThreadPoolExecutor(0, max_conn, 60000L,

TimeUnit.MILLISECONDS, socketadaptes);// 线程池

}

}

// 启动

this.start();

}

@Override

public void run() {

// TODO Auto-generated method stub

try {

server = new ServerSocket(port);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

logger.error("port " + port + " is used ,the server is close");

return;

}

label: while (true) {

try {

Socket socket = null;

try {

socket = server.accept();

logger.info("the socket [" + socket.getInetAddress() + ":"

+ socket.getPort() + "] has been connection ");

socket.setKeepAlive(true);

if (timeout != 0) {

socket.setSoTimeout(timeout);

}

try {

// 将此处的socket监听放置到进程池

exService.execute(new SocketAdapt(socket));

logger.info("the socket [" + socket.getInetAddress()

+ ":" + socket.getPort()

+ "] has been put in ExecutorService");

} catch (RejectedExecutionException e) {

// TODO: handle exception

e.fillInStackTrace();

throw new Exception(

"becaue the \""

+ e.getMessage()

+ "\" the thread is full and the thread cache is full ;");

}

} catch (SocketException e) {

// TODO Auto-generated catch block

e.printStackTrace();

if (socket != null)

socket.close();

throw new Exception("because the error \"" + e.getMessage()

+ "\" so close this socket");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

if (socket != null)

socket.close();

throw new Exception("because the error \"" + e.getMessage()

+ "\" so close this socket");

}

} catch (Exception e) {

e.fillInStackTrace();

// TODO: handle exception

logger.error(e.getMessage());

// 一旦发生异常,还继续进行

continue label;

}

}

}

public static void main(String[] args) {

new SocketServerManager(5566, 100, 10, 30000);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值