Server增加setThreads方法,可以动态修改线程池的大小
This commit is contained in:
@@ -31,7 +31,7 @@ public class Context {
|
|||||||
protected final long serverStartTime;
|
protected final long serverStartTime;
|
||||||
|
|
||||||
//Server的线程池
|
//Server的线程池
|
||||||
protected final ExecutorService executor;
|
protected final ThreadPoolExecutor executor;
|
||||||
|
|
||||||
//ByteBuffer的容量,默认8K
|
//ByteBuffer的容量,默认8K
|
||||||
protected final int bufferCapacity;
|
protected final int bufferCapacity;
|
||||||
@@ -69,7 +69,7 @@ public class Context {
|
|||||||
//JSON操作工厂
|
//JSON操作工厂
|
||||||
protected final JsonFactory jsonFactory;
|
protected final JsonFactory jsonFactory;
|
||||||
|
|
||||||
public Context(long serverStartTime, Logger logger, ExecutorService executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool, ObjectPool<Response> responsePool,
|
public Context(long serverStartTime, Logger logger, ThreadPoolExecutor executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool, ObjectPool<Response> responsePool,
|
||||||
final int maxbody, Charset charset, InetSocketAddress address, final PrepareServlet prepare, final int readTimeoutSecond, final int writeTimeoutSecond) {
|
final int maxbody, Charset charset, InetSocketAddress address, final PrepareServlet prepare, final int readTimeoutSecond, final int writeTimeoutSecond) {
|
||||||
this.serverStartTime = serverStartTime;
|
this.serverStartTime = serverStartTime;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
|||||||
protected int threads;
|
protected int threads;
|
||||||
|
|
||||||
//线程池
|
//线程池
|
||||||
protected ExecutorService executor;
|
protected ThreadPoolExecutor executor;
|
||||||
|
|
||||||
//ByteBuffer池大小
|
//ByteBuffer池大小
|
||||||
protected int bufferPoolSize;
|
protected int bufferPoolSize;
|
||||||
@@ -113,7 +113,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
|||||||
final AtomicInteger counter = new AtomicInteger();
|
final AtomicInteger counter = new AtomicInteger();
|
||||||
final Format f = createFormat();
|
final Format f = createFormat();
|
||||||
final String n = name;
|
final String n = name;
|
||||||
this.executor = Executors.newFixedThreadPool(threads, (Runnable r) -> {
|
this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads, (Runnable r) -> {
|
||||||
Thread t = new WorkThread(executor, r);
|
Thread t = new WorkThread(executor, r);
|
||||||
t.setName(n + "-ServletThread-" + f.format(counter.incrementAndGet()));
|
t.setName(n + "-ServletThread-" + f.format(counter.incrementAndGet()));
|
||||||
return t;
|
return t;
|
||||||
@@ -166,6 +166,13 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
|||||||
return this.context;
|
return this.context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setThreads(int threads) {
|
||||||
|
int oldthreads = this.threads;
|
||||||
|
this.context.executor.setCorePoolSize(threads);
|
||||||
|
this.threads = threads;
|
||||||
|
logger.info("[" + Thread.currentThread().getName() + "] " + this.getClass().getSimpleName() + " change threads from " + oldthreads + " to " + threads);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) {
|
public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) {
|
||||||
this.prepare.addServlet(servlet, attachment, conf, mappings);
|
this.prepare.addServlet(servlet, attachment, conf, mappings);
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class HttpContext extends Context {
|
|||||||
|
|
||||||
protected final ConcurrentHashMap<Class, Creator> asyncHandlerCreators = new ConcurrentHashMap<>();
|
protected final ConcurrentHashMap<Class, Creator> asyncHandlerCreators = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public HttpContext(long serverStartTime, Logger logger, ExecutorService executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool,
|
public HttpContext(long serverStartTime, Logger logger, ThreadPoolExecutor executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool,
|
||||||
ObjectPool<Response> responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare,
|
ObjectPool<Response> responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare,
|
||||||
int readTimeoutSecond, int writeTimeoutSecond) {
|
int readTimeoutSecond, int writeTimeoutSecond) {
|
||||||
super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset,
|
super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ package org.redkale.net.sncp;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import org.redkale.net.*;
|
import org.redkale.net.*;
|
||||||
import org.redkale.util.ObjectPool;
|
import org.redkale.util.ObjectPool;
|
||||||
@@ -21,7 +21,7 @@ import org.redkale.util.ObjectPool;
|
|||||||
*/
|
*/
|
||||||
public class SncpContext extends Context {
|
public class SncpContext extends Context {
|
||||||
|
|
||||||
public SncpContext(long serverStartTime, Logger logger, ExecutorService executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool,
|
public SncpContext(long serverStartTime, Logger logger, ThreadPoolExecutor executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool,
|
||||||
ObjectPool<Response> responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare,
|
ObjectPool<Response> responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare,
|
||||||
int readTimeoutSecond, int writeTimeoutSecond) {
|
int readTimeoutSecond, int writeTimeoutSecond) {
|
||||||
super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset,
|
super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset,
|
||||||
|
|||||||
Reference in New Issue
Block a user