Skip to content

Commit 1736f68

Browse files
Merge remote-tracking branch 'origin/master'
2 parents 5925abc + 3a00e57 commit 1736f68

10 files changed

Lines changed: 204 additions & 492 deletions

File tree

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoClient.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,22 @@ private void start() {
4040
}
4141
}
4242

43-
public String sendMessage(String message) throws Exception {
43+
public String sendMessage(String message) {
4444
byte[] byteMsg = message.getBytes();
4545
ByteBuffer buffer = ByteBuffer.wrap(byteMsg);
4646
Future<Integer> writeResult = client.write(buffer);
4747

48-
//run some code
49-
writeResult.get();
48+
try {
49+
writeResult.get();
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
}
5053
buffer.flip();
5154
Future<Integer> readResult = client.read(buffer);
52-
53-
//run some code
54-
readResult.get();
55+
try {
56+
readResult.get();
57+
} catch (Exception e) {
58+
e.printStackTrace();
5559
String echo = new String(buffer.array()).trim();
5660
buffer.clear();
5761
return echo;

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,19 @@ class ReadWriteHandler implements CompletionHandler<Integer, Map<String, Object>
5858

5959
@Override
6060
public void completed(Integer result, Map<String, Object> attachment) {
61-
String action = (String) attachment.get("action");
61+
Map<String, Object> actionInfo = attachment;
62+
String action = (String) actionInfo.get("action");
6263
if ("read".equals(action)) {
63-
ByteBuffer buffer = (ByteBuffer) attachment.get("buffer");
64+
ByteBuffer buffer = (ByteBuffer) actionInfo.get("buffer");
6465
buffer.flip();
65-
attachment.put("action", "write");
66-
clientChannel.write(buffer, attachment, this);
66+
actionInfo.put("action", "write");
67+
clientChannel.write(buffer, actionInfo, this);
6768
buffer.clear();
6869
} else if ("write".equals(action)) {
6970
ByteBuffer buffer = ByteBuffer.allocate(32);
70-
attachment.put("action", "read");
71-
attachment.put("buffer", buffer);
72-
clientChannel.read(buffer, attachment, this);
71+
actionInfo.put("action", "read");
72+
actionInfo.put("buffer", buffer);
73+
clientChannel.read(buffer, actionInfo, this);
7374
}
7475

7576
}
@@ -81,6 +82,7 @@ public void failed(Throwable exc, Map<String, Object> attachment) {
8182

8283
}
8384

85+
8486
public static void main(String[] args) {
8587
new AsyncEchoServer2();
8688
}
@@ -95,4 +97,4 @@ public static Process start() throws IOException, InterruptedException {
9597

9698
return builder.start();
9799
}
98-
}
100+
}

0 commit comments

Comments
 (0)