Skip to content

Commit

Permalink
ServerSocketAppenderBase now uses context executor service
Browse files Browse the repository at this point in the history
  • Loading branch information
ceharris committed Apr 23, 2013
1 parent 85afb53 commit 72c454e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

import javax.net.ServerSocketFactory;

Expand Down Expand Up @@ -51,9 +50,7 @@ public abstract class ServerSocketAppenderBase<E> extends AppenderBase<E> {
private int clientQueueSize = DEFAULT_CLIENT_QUEUE_SIZE;

private String address;
private ThreadPoolFactoryBean threadPool;

private ExecutorService executor;
private ServerRunner<RemoteReceiverClient> runner;

@Override
Expand All @@ -63,10 +60,10 @@ public void start() {
ServerSocket socket = getServerSocketFactory().createServerSocket(
getPort(), getBacklog(), getInetAddress());
ServerListener<RemoteReceiverClient> listener = createServerListener(socket);
executor = getThreadPool().createExecutor();
runner = createServerRunner(listener, executor);

runner = createServerRunner(listener, getContext().getExecutorService());
runner.setContext(getContext());
executor.execute(runner);
getContext().getExecutorService().execute(runner);
super.start();
}
catch (Exception ex) {
Expand All @@ -91,7 +88,6 @@ public void stop() {
if (!isStarted()) return;
try {
runner.stop();
executor.shutdownNow();
super.stop();
}
catch (IOException ex) {
Expand Down Expand Up @@ -219,25 +215,5 @@ public void setClientQueueSize(int clientQueueSize) {
this.clientQueueSize = clientQueueSize;
}

/**
* Gets the server's thread pool configuration.
* @return thread pool configuration; if no thread pool configuration was
* provided, a default configuration is returned
*/
public ThreadPoolFactoryBean getThreadPool() {
if (threadPool == null) {
return new ThreadPoolFactoryBean();
}
return threadPool;
}

/**
* Sets the server's thread pool configuration.
* @param threadPool the configuration to set
*/
public void setThreadPool(ThreadPoolFactoryBean threadPool) {
this.threadPool = threadPool;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package ch.qos.logback.core.net.server;

import java.util.List;
import java.util.concurrent.ExecutorService;

import ch.qos.logback.core.Context;
import ch.qos.logback.core.ContextBase;
Expand All @@ -29,14 +30,28 @@
public class MockContext extends ContextBase {

private final MockStatusManager statusManager = new MockStatusManager();
private final ExecutorService executorService;

private Status lastStatus;

public MockContext() {
this(new MockExecutorService());
}

public MockContext(ExecutorService executorService) {
this.executorService = executorService;
}

@Override
public StatusManager getStatusManager() {
return statusManager;
}

@Override
public ExecutorService getExecutorService() {
return executorService;
}

public Status getLastStatus() {
return lastStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,24 @@ public class ServerSocketAppenderBaseFunctionalTest {

private static final int EVENT_COUNT = 10;

private MockContext context = new MockContext();
private ExecutorService executor = Executors.newCachedThreadPool();
private MockContext context = new MockContext(executor);
private ServerSocket serverSocket;
private ExecutorService executor = Executors.newFixedThreadPool(2);
private InstrumentedServerSocketAppenderBase appender;

@Before
public void setUp() throws Exception {

serverSocket = ServerSocketUtil.createServerSocket();

appender = new InstrumentedServerSocketAppenderBase(serverSocket);

appender.setThreadPool(new ThreadPoolFactoryBean() {
@Override
public ExecutorService createExecutor() {
return executor;
}
});

appender = new InstrumentedServerSocketAppenderBase(serverSocket);
appender.setContext(context);
}

@After
public void tearDown() throws Exception {
executor.awaitTermination(1000, TimeUnit.MILLISECONDS);
executor.shutdownNow();
executor.awaitTermination(10000, TimeUnit.MILLISECONDS);
assertTrue(executor.isTerminated());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,13 @@ public class ServerSocketAppenderBaseTest {
private MockServerListener<RemoteReceiverClient> listener =
new MockServerListener<RemoteReceiverClient>();

private MockThreadPoolFactoryBean threadPool =
new MockThreadPoolFactoryBean();

private ServerSocket serverSocket;
private InstrumentedServerSocketAppenderBase appender;

@Before
public void setUp() throws Exception {
serverSocket = ServerSocketUtil.createServerSocket();
appender = new InstrumentedServerSocketAppenderBase(serverSocket, listener, runner);
appender.setThreadPool(threadPool);
appender.setContext(context);
}

Expand Down

0 comments on commit 72c454e

Please sign in to comment.