From ed1a0022b9875dd8b168bd48eded37218940f103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sat, 19 Aug 2017 11:00:34 +0300 Subject: [PATCH] #596 Add more logging to Reactor --- .../java/com/iluwatar/reactor/app/AppClient.java | 10 ++++++---- .../com/iluwatar/reactor/framework/NioReactor.java | 5 +++-- .../java/com/iluwatar/reactor/app/ReactorTest.java | 13 +++++++++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/reactor/src/main/java/com/iluwatar/reactor/app/AppClient.java b/reactor/src/main/java/com/iluwatar/reactor/app/AppClient.java index 4db4f5e2a..446628769 100644 --- a/reactor/src/main/java/com/iluwatar/reactor/app/AppClient.java +++ b/reactor/src/main/java/com/iluwatar/reactor/app/AppClient.java @@ -65,6 +65,7 @@ public class AppClient { * @throws IOException if any I/O error occurs. */ public void start() throws IOException { + LOGGER.info("Starting logging clients"); service.execute(new TcpLoggingClient("Client 1", 6666)); service.execute(new TcpLoggingClient("Client 2", 6667)); service.execute(new UdpLoggingClient("Client 3", 6668)); @@ -81,16 +82,17 @@ public class AppClient { try { service.awaitTermination(1000, TimeUnit.SECONDS); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("exception awaiting termination", e); } } + LOGGER.info("Logging clients stopped"); } private static void artificialDelayOf(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("sleep interrupted", e); } } @@ -119,7 +121,7 @@ public class AppClient { PrintWriter writer = new PrintWriter(outputStream); sendLogRequests(writer, socket.getInputStream()); } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("error sending requests", e); throw new RuntimeException(e); } } @@ -185,7 +187,7 @@ public class AppClient { artificialDelayOf(100); } } catch (IOException e1) { - e1.printStackTrace(); + LOGGER.error("error sending packets", e1); } } } diff --git a/reactor/src/main/java/com/iluwatar/reactor/framework/NioReactor.java b/reactor/src/main/java/com/iluwatar/reactor/framework/NioReactor.java index bc5b27494..a315389a3 100644 --- a/reactor/src/main/java/com/iluwatar/reactor/framework/NioReactor.java +++ b/reactor/src/main/java/com/iluwatar/reactor/framework/NioReactor.java @@ -94,7 +94,7 @@ public class NioReactor { LOGGER.info("Reactor started, waiting for events..."); eventLoop(); } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("exception in event loop", e); } }); } @@ -112,6 +112,7 @@ public class NioReactor { selector.wakeup(); reactorMain.awaitTermination(4, TimeUnit.SECONDS); selector.close(); + LOGGER.info("Reactor stopped"); } /** @@ -206,7 +207,7 @@ public class NioReactor { try { key.channel().close(); } catch (IOException e1) { - e1.printStackTrace(); + LOGGER.error("error closing channel", e1); } } } diff --git a/reactor/src/test/java/com/iluwatar/reactor/app/ReactorTest.java b/reactor/src/test/java/com/iluwatar/reactor/app/ReactorTest.java index 413bbc017..7aa80f8cc 100644 --- a/reactor/src/test/java/com/iluwatar/reactor/app/ReactorTest.java +++ b/reactor/src/test/java/com/iluwatar/reactor/app/ReactorTest.java @@ -24,10 +24,13 @@ package com.iluwatar.reactor.app; import java.io.IOException; +import com.iluwatar.reactor.framework.NioReactor; import org.junit.Test; import com.iluwatar.reactor.framework.SameThreadDispatcher; 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 { + private static final Logger LOGGER = LoggerFactory.getLogger(ReactorTest.class); + /** * Test the application using pooled thread dispatcher. * @@ -44,6 +49,7 @@ public class ReactorTest { */ @Test public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException { + LOGGER.info("testAppUsingThreadPoolDispatcher start"); App app = new App(new ThreadPoolDispatcher(2)); app.start(); @@ -54,12 +60,13 @@ public class ReactorTest { try { Thread.sleep(2000); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("sleep interrupted", e); } client.stop(); app.stop(); + LOGGER.info("testAppUsingThreadPoolDispatcher stop"); } /** @@ -70,6 +77,7 @@ public class ReactorTest { */ @Test public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException { + LOGGER.info("testAppUsingSameThreadDispatcher start"); App app = new App(new SameThreadDispatcher()); app.start(); @@ -80,11 +88,12 @@ public class ReactorTest { try { Thread.sleep(2000); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("sleep interrupted", e); } client.stop(); app.stop(); + LOGGER.info("testAppUsingSameThreadDispatcher stop"); } }