非常基本的netty用法:对接收对象的调用方法

时间:2014-07-24 06:48:07

标签: java multithreading sockets client-server netty

对于Netty ObjectEcho示例,分支4.0,服务器从哪里接收客户端的对象?更重要的是,在ObjectEchoServerObjectEchoServerHandler中可以调用接收对象的方法吗?

服务器上正在接收对象:

BUILD SUCCESSFUL
Total time: 2 seconds
Jul 23, 2014 11:44:04 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0x93baa167] REGISTERED
Jul 23, 2014 11:44:04 PM io.netty.handler.logging.LoggingHandler bind
INFO: [id: 0x93baa167] BIND(0.0.0.0/0.0.0.0:4454)
Jul 23, 2014 11:44:04 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0x93baa167, /0:0:0:0:0:0:0:0:4454] ACTIVE
Jul 23, 2014 11:44:10 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0x93baa167, /0:0:0:0:0:0:0:0:4454] RECEIVED: [id: 0x8c8385a6, /127.0.0.1:33803 => /127.0.0.1:4454]
Jul 23, 2014 11:44:10 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0x8c8385a6, /127.0.0.1:33803 => /127.0.0.1:4454] REGISTERED
Jul 23, 2014 11:44:10 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0x8c8385a6, /127.0.0.1:33803 => /127.0.0.1:4454] ACTIVE
Jul 23, 2014 11:44:10 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0x8c8385a6, /127.0.0.1:33803 => /127.0.0.1:4454] WRITE: Sun Jun 16 16:15:54 PST 1878
Jul 23, 2014 11:44:10 PM io.netty.handler.logging.LoggingHandler flush
INFO: [id: 0x8c8385a6, /127.0.0.1:33803 => /127.0.0.1:4454] FLUSH
Jul 23, 2014 11:44:10 PM io.netty.handler.logging.LoggingHandler close
INFO: [id: 0x8c8385a6, /127.0.0.1:33803 => /127.0.0.1:4454] CLOSE()
Jul 23, 2014 11:44:10 PM io.netty.handler.logging.LoggingHandler channelInactive
INFO: [id: 0x8c8385a6, /127.0.0.1:33803 :> /127.0.0.1:4454] INACTIVE
Jul 23, 2014 11:44:10 PM io.netty.handler.logging.LoggingHandler channelUnregistered
INFO: [id: 0x8c8385a6, /127.0.0.1:33803 :> /127.0.0.1:4454] UNREGISTERED
^Cthufir@dur:~/NetBeansProjects/ObjectEchoServer$ 
thufir@dur:~/NetBeansProjects/ObjectEchoServer$ 

服务器如何处理收到的对象?如何调用对象中的方法?

我绝对意识到这是一个非常基本的问题。我已翻阅book on netty,但无法找到解释此基本功能的相关部分。

我修改了服务器代码,如下所示:

package io.netty.example.objectecho;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.logging.Logger;

/**
 * Handles both client-side and server-side handler depending on which
 * constructor was called.
 */
public class ObjectEchoServerHandler extends ChannelInboundHandlerAdapter {

    private static final Logger log = Logger.getLogger(ObjectEchoServerHandler.class.getName());

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        log.warning(msg.toString());
        ctx.write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        log.info("finished reading..?");
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

但似乎无法像我期望的那样调用msg.toString(),也无法记录“读完”。每次收到一个对象时,服务器都应该处理该对象并将其传递给另一个类 - 但是如何?

我如何至少在接收对象上调用toString?它显然正在接收,它按原样记录。这发生在服务器类中,而不是处理程序?

1 个答案:

答案 0 :(得分:0)

客户输出:

BUILD SUCCESSFUL
Total time: 1 second
Jul 25, 2014 10:18:59 PM io.netty.example.objectecho.ObjectEchoClientHandler channelActive
INFO: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
Jul 25, 2014 10:18:59 PM io.netty.example.objectecho.ObjectEchoClientHandler channelRead

服务器输出:

BUILD SUCCESSFUL
Total time: 1 second
Jul 25, 2014 10:18:48 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0xe7cb1e59] REGISTERED
Jul 25, 2014 10:18:48 PM io.netty.handler.logging.LoggingHandler bind
INFO: [id: 0xe7cb1e59] BIND(0.0.0.0/0.0.0.0:4454)
Jul 25, 2014 10:18:48 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0xe7cb1e59, /0:0:0:0:0:0:0:0:4454] ACTIVE
Jul 25, 2014 10:18:59 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0xe7cb1e59, /0:0:0:0:0:0:0:0:4454] RECEIVED: [id: 0xc5d3dfbf, /127.0.0.1:33250 => /127.0.0.1:4454]
Jul 25, 2014 10:18:59 PM io.netty.example.objectecho.ObjectEchoServerHandler channelRead
WARNING: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
Jul 25, 2014 10:18:59 PM io.netty.example.objectecho.ObjectEchoServerHandler channelReadComplete
INFO: finished reading..?
Jul 25, 2014 10:18:59 PM io.netty.example.objectecho.ObjectEchoServerHandler channelRead
WARNING: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
Jul 25, 2014 10:18:59 PM io.netty.example.objectecho.ObjectEchoServerHandler channelReadComplete
INFO: finished reading..?

我认为这是相同的代码......不确定问题是什么。建立无限循环:)

服务器处理程序:

package io.netty.example.objectecho;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.logging.Logger;

/**
 * Handles both client-side and server-side handler depending on which
 * constructor was called.
 */
public class ObjectEchoServerHandler extends ChannelInboundHandlerAdapter {

    private static final Logger log = Logger.getLogger(ObjectEchoServerHandler.class.getName());

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        log.warning(msg.toString());
        ctx.write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        log.info("finished reading..?");
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}