#596 Add more logging to Reactor

This commit is contained in:
Ilkka Seppälä
2017-08-19 11:00:34 +03:00
parent 5db6776971
commit ed1a0022b9
3 changed files with 20 additions and 8 deletions

View File

@ -65,6 +65,7 @@ public class AppClient {
* @throws IOException if any I/O error occurs. * @throws IOException if any I/O error occurs.
*/ */
public void start() throws IOException { public void start() throws IOException {
LOGGER.info("Starting logging clients");
service.execute(new TcpLoggingClient("Client 1", 6666)); service.execute(new TcpLoggingClient("Client 1", 6666));
service.execute(new TcpLoggingClient("Client 2", 6667)); service.execute(new TcpLoggingClient("Client 2", 6667));
service.execute(new UdpLoggingClient("Client 3", 6668)); service.execute(new UdpLoggingClient("Client 3", 6668));
@ -81,16 +82,17 @@ public class AppClient {
try { try {
service.awaitTermination(1000, TimeUnit.SECONDS); service.awaitTermination(1000, TimeUnit.SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); LOGGER.error("exception awaiting termination", e);
} }
} }
LOGGER.info("Logging clients stopped");
} }
private static void artificialDelayOf(long millis) { private static void artificialDelayOf(long millis) {
try { try {
Thread.sleep(millis); Thread.sleep(millis);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); LOGGER.error("sleep interrupted", e);
} }
} }
@ -119,7 +121,7 @@ public class AppClient {
PrintWriter writer = new PrintWriter(outputStream); PrintWriter writer = new PrintWriter(outputStream);
sendLogRequests(writer, socket.getInputStream()); sendLogRequests(writer, socket.getInputStream());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOGGER.error("error sending requests", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -185,7 +187,7 @@ public class AppClient {
artificialDelayOf(100); artificialDelayOf(100);
} }
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); LOGGER.error("error sending packets", e1);
} }
} }
} }

View File

@ -94,7 +94,7 @@ public class NioReactor {
LOGGER.info("Reactor started, waiting for events..."); LOGGER.info("Reactor started, waiting for events...");
eventLoop(); eventLoop();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOGGER.error("exception in event loop", e);
} }
}); });
} }
@ -112,6 +112,7 @@ public class NioReactor {
selector.wakeup(); selector.wakeup();
reactorMain.awaitTermination(4, TimeUnit.SECONDS); reactorMain.awaitTermination(4, TimeUnit.SECONDS);
selector.close(); selector.close();
LOGGER.info("Reactor stopped");
} }
/** /**
@ -206,7 +207,7 @@ public class NioReactor {
try { try {
key.channel().close(); key.channel().close();
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); LOGGER.error("error closing channel", e1);
} }
} }
} }

View File

@ -24,10 +24,13 @@ package com.iluwatar.reactor.app;
import java.io.IOException; import java.io.IOException;
import com.iluwatar.reactor.framework.NioReactor;
import org.junit.Test; import org.junit.Test;
import com.iluwatar.reactor.framework.SameThreadDispatcher; import com.iluwatar.reactor.framework.SameThreadDispatcher;
import com.iluwatar.reactor.framework.ThreadPoolDispatcher; import com.iluwatar.reactor.framework.ThreadPoolDispatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* *
@ -36,6 +39,8 @@ import com.iluwatar.reactor.framework.ThreadPoolDispatcher;
*/ */
public class ReactorTest { public class ReactorTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ReactorTest.class);
/** /**
* Test the application using pooled thread dispatcher. * Test the application using pooled thread dispatcher.
* *
@ -44,6 +49,7 @@ public class ReactorTest {
*/ */
@Test @Test
public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException { public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException {
LOGGER.info("testAppUsingThreadPoolDispatcher start");
App app = new App(new ThreadPoolDispatcher(2)); App app = new App(new ThreadPoolDispatcher(2));
app.start(); app.start();
@ -54,12 +60,13 @@ public class ReactorTest {
try { try {
Thread.sleep(2000); Thread.sleep(2000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); LOGGER.error("sleep interrupted", e);
} }
client.stop(); client.stop();
app.stop(); app.stop();
LOGGER.info("testAppUsingThreadPoolDispatcher stop");
} }
/** /**
@ -70,6 +77,7 @@ public class ReactorTest {
*/ */
@Test @Test
public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException { public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException {
LOGGER.info("testAppUsingSameThreadDispatcher start");
App app = new App(new SameThreadDispatcher()); App app = new App(new SameThreadDispatcher());
app.start(); app.start();
@ -80,11 +88,12 @@ public class ReactorTest {
try { try {
Thread.sleep(2000); Thread.sleep(2000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); LOGGER.error("sleep interrupted", e);
} }
client.stop(); client.stop();
app.stop(); app.stop();
LOGGER.info("testAppUsingSameThreadDispatcher stop");
} }
} }