Fix AsyncEchoTest
This commit is contained in:
parent
450c250a0f
commit
3a00e57002
|
@ -40,18 +40,22 @@ public class AsyncEchoClient {
|
|||
}
|
||||
}
|
||||
|
||||
public String sendMessage(String message) throws Exception {
|
||||
public String sendMessage(String message) {
|
||||
byte[] byteMsg = message.getBytes();
|
||||
ByteBuffer buffer = ByteBuffer.wrap(byteMsg);
|
||||
Future<Integer> writeResult = client.write(buffer);
|
||||
|
||||
//run some code
|
||||
writeResult.get();
|
||||
try {
|
||||
writeResult.get();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
buffer.flip();
|
||||
Future<Integer> readResult = client.read(buffer);
|
||||
|
||||
//run some code
|
||||
readResult.get();
|
||||
try {
|
||||
readResult.get();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
String echo = new String(buffer.array()).trim();
|
||||
buffer.clear();
|
||||
return echo;
|
||||
|
|
|
@ -58,18 +58,19 @@ public class AsyncEchoServer2 {
|
|||
|
||||
@Override
|
||||
public void completed(Integer result, Map<String, Object> attachment) {
|
||||
String action = (String) attachment.get("action");
|
||||
Map<String, Object> actionInfo = attachment;
|
||||
String action = (String) actionInfo.get("action");
|
||||
if ("read".equals(action)) {
|
||||
ByteBuffer buffer = (ByteBuffer) attachment.get("buffer");
|
||||
ByteBuffer buffer = (ByteBuffer) actionInfo.get("buffer");
|
||||
buffer.flip();
|
||||
attachment.put("action", "write");
|
||||
clientChannel.write(buffer, attachment, this);
|
||||
actionInfo.put("action", "write");
|
||||
clientChannel.write(buffer, actionInfo, this);
|
||||
buffer.clear();
|
||||
} else if ("write".equals(action)) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(32);
|
||||
attachment.put("action", "read");
|
||||
attachment.put("buffer", buffer);
|
||||
clientChannel.read(buffer, attachment, this);
|
||||
actionInfo.put("action", "read");
|
||||
actionInfo.put("buffer", buffer);
|
||||
clientChannel.read(buffer, actionInfo, this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -81,6 +82,7 @@ public class AsyncEchoServer2 {
|
|||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
new AsyncEchoServer2();
|
||||
}
|
||||
|
@ -95,4 +97,4 @@ public class AsyncEchoServer2 {
|
|||
|
||||
return builder.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue