8000 #596 Add more logging to Reactor · sithuhlaing/java-design-patterns@ed1a002 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed1a002

Browse files
committed
iluwatar#596 Add more logging to Reactor
1 parent 5db6776 commit ed1a002

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

reactor/src/main/java/com/iluwatar/reactor/app/AppClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static void main(String[] args) throws IOException {
6565
* @throws IOException if any I/O error occurs.
6666
*/
6767
public void start() throws IOException {
68+
LOGGER.info("Starting logging clients");
6869
service.execute(new TcpLoggingClient("Client 1", 6666));
6970
service.execute(new TcpLoggingClient("Client 2", 6667));
7071
service.execute(new UdpLoggingClient("Client 3", 6668));
@@ -81,16 +82,17 @@ public void stop() {
8182
try {
8283
service.awaitTermination(1000, TimeUnit.SECONDS);
8384
} catch (InterruptedException e) {
84-
e.printStackTrace();
85+
LOGGER.error("exception awaiting termination", e);
8586
}
8687
}
88+
LOGGER.info("Logging clients stopped");
8789
}
8890

8991
private static void artificialDelayOf(long millis) {
9092
try {
9193
Thread.sleep(millis);
9294
} catch (InterruptedException e) {
93-
e.printStackTrace();
95+
LOGGER.error("sleep interrupted", e);
9496
}
9597
}
9698

@@ -119,7 +121,7 @@ public void run() {
119121
PrintWriter writer = new PrintWriter(outputStream);
120122
sendLogRequests(writer, socket.getInputStream());
121123
} catch (IOException e) {
122-
e.printStackTrace();
124+
LOGGER.error("error sending requests", e);
123125
throw new RuntimeException(e);
124126
}
125127
}
@@ -185,7 +187,7 @@ public void run() {
185187
artificialDelayOf(100);
186188
}
187189
} catch (IOException e1) {
188-
e1.printStackTrace();
190+
LOGGER.error("error sending packets", e1);
189191
}
190192
}
191193
}

reactor/src/main/java/com/iluwatar/reactor/framework/NioReactor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void start() throws IOException {
9494
LOGGER.info("Reactor started, waiting for events...");
9595
eventLoop();
9696
} catch (IOException e) {
97-
e.printStackTrace();
97+
LOGGER.error("exception in event loop", e);
9898
}
9999
});
100100
}
@@ -112,6 +112,7 @@ public void stop() throws InterruptedException, IOException {
112112
selector.wakeup();
113113
reactorMain.awaitTermination(4, TimeUnit.SECONDS);
114114
selector.close();
115+
LOGGER.info("Reactor stopped");
115116
}
116117

117118
/**
@@ -206,7 +207,7 @@ private void onChannelReadable(SelectionKey key) {
206207
try {
207208
key.channel().close();
208209
} catch (IOException e1) {
209-
e1.printStackTrace();
210+
LOGGER.error("error closing channel", e1);
210211
}
211212
}
212213
}

reactor/src/test/java/com/iluwatar/reactor/app/ReactorTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424

2525
import java.io.IOException;
2626

27+
import com.iluwatar.reactor.framework.NioReactor;
2728
import org.junit.Test;
2829

2930
import com.iluwatar.reactor.framework.SameThreadDispatcher;
3031
import com.iluwatar.reactor.framework.ThreadPoolDispatcher;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3134

3235
/**
3336
*
@@ -36,6 +39,8 @@
3639
*/
3740
public class ReactorTest {
3841

42+
private static final Logger LOGGER = LoggerFactory.getLogger(ReactorTest.class);
43+
3944
/**
4045
* Test the application using pooled thread dispatcher.
4146
*
@@ -44,6 +49,7 @@ public class ReactorTest {
4449
*/
4550
@Test
4651
public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException {
52+
LOGGER.info("testAppUsingThreadPoolDispatcher start");
4753
App app = new App(new ThreadPoolDispatcher(2));
4854
app.start();
4955

@@ -54,12 +60,13 @@ public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedEx
5460
try {
5561
Thread.sleep(2000);
5662
} catch (InterruptedException e) {
57-
e.printStackTrace();
63+
LOGGER.error("sleep interrupted", e);
5864
}
5965

6066
client.stop();
6167

6268
app.stop();
69+
LOGGER.info("testAppUsingThreadPoolDispatcher stop");
6370
}
6471

6572
/**
@@ -70,6 +77,7 @@ public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedEx
7077
*/
7178
@Test
7279
public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException {
80+
LOGGER.info("testAppUsingSameThreadDispatcher start");
7381
App app = new App(new SameThreadDispatcher());
7482
app.start();
7583

@@ -80,11 +88,12 @@ public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedEx
8088
try {
8189
Thread.sleep(2000);
8290
} catch (InterruptedException e) {
83-
e.printStackTrace();
91+
LOGGER.error("sleep interrupted", e);
8492
}
8593

8694
client.stop();
8795

8896
app.stop();
97+
LOGGER.info("testAppUsingSameThreadDispatcher stop");
8998
}
9099
}

0 commit comments

Comments
 (0)
0