Guest User

Untitled

a guest
Jul 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. public class Person implements Serializable {
  2. private final Long id;
  3. private String name;
  4.  
  5. public Person(Long id, String name) {
  6. this.id = id;
  7. this.name = name;
  8. }
  9.  
  10. public Long getId() {
  11. return id;
  12. }
  13.  
  14. public String getName() {
  15. return name;
  16. }
  17.  
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21.  
  22. public String toString() {
  23. return getName();
  24. }
  25. }
  26.  
  27. public class SimpleClient {
  28. public static void main(String[] args) throws Exception {
  29. EventLoopGroup group = new NioEventLoopGroup();
  30. try {
  31. new Bootstrap().group(group)
  32. .channel(NioSocketChannel.class)
  33. .handler(new ChannelInitializer<SocketChannel>() {
  34. @Override
  35. public void initChannel(SocketChannel socketChannel) {
  36. ChannelPipeline pipeline = socketChannel.pipeline();
  37. pipeline.addLast(new ObjectEncoder());
  38. pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
  39. pipeline.addLast(new ChannelInboundHandlerAdapter() {
  40. @Override
  41. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  42. ctx.writeAndFlush(new Person(1L, "John"));
  43. }
  44.  
  45. @Override
  46. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  47. Person person = (Person) msg;
  48. System.out.println(person);
  49. }
  50. });
  51. }
  52. })
  53. .connect("localhost", 8080)
  54. .sync()
  55. .channel()
  56. .closeFuture()
  57. .sync();
  58.  
  59. } finally {
  60. group.shutdownGracefully();
  61. }
  62. }
  63. }
  64.  
  65. public class SimpleServer {
  66. public static void main(String[] args) throws Exception {
  67. EventLoopGroup group = new NioEventLoopGroup();
  68. try {
  69. new ServerBootstrap().group(group)
  70. .channel(NioServerSocketChannel.class)
  71. .childHandler(new ChannelInitializer<SocketChannel>() {
  72. @Override
  73. public void initChannel(SocketChannel socketChannel) {
  74. ChannelPipeline pipeline = socketChannel.pipeline();
  75. pipeline.addLast(new ObjectEncoder());
  76. pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
  77. pipeline.addLast(new ChannelInboundHandlerAdapter() {
  78. @Override
  79. public void channelRead(ChannelHandlerContext ctx, Object msg) {
  80. Person person = (Person) msg;
  81. System.out.println(person);
  82.  
  83. person.setName("John Doe");
  84. ctx.writeAndFlush(person)
  85. .addListener(ChannelFutureListener.CLOSE);
  86. }
  87.  
  88. @Override
  89. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
  90. cause.printStackTrace();
  91. ctx.channel().close();
  92. }
  93. });
  94. }
  95. })
  96. .bind("localhost", 8080)
  97. .sync()
  98. .channel()
  99. .closeFuture()
  100. .syncUninterruptibly();
  101.  
  102. } finally {
  103. group.shutdownGracefully();
  104. }
  105. }
  106. }
Add Comment
Please, Sign In to add comment