com.alibaba.dubbo.remoting.exchange.Request Java Examples

The following examples show how to use com.alibaba.dubbo.remoting.exchange.Request. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExchangeCodecTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test 
public void test_Encode_Request() throws IOException{
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(2014);
    Channel channel = getCliendSideChannel(url);
    Request request = new Request();
    Person person = new Person();
    request.setData(person);
    
    codec.encode(channel, encodeBuffer, request);

    //encode resault check need decode
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Request obj = (Request)codec.decode(channel, decodeBuffer);
    Assert.assertEquals(request.isBroken(), obj.isBroken());
    Assert.assertEquals(request.isHeartbeat(), obj.isHeartbeat());
    Assert.assertEquals(request.isTwoWay(), obj.isTwoWay());
    Assert.assertEquals(person, obj.getData());
}
 
Example #2
Source File: ExchangeCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void encode(Channel channel, ChannelBuffer buffer, Object msg) throws IOException {
        if (msg instanceof Request) {
            encodeRequest(channel, buffer, (Request) msg);
        } else if (msg instanceof Response) {
            encodeResponse(channel, buffer, (Response) msg);
        } else {
            super.encode(channel, buffer, msg);
        }

        // TODO modified by lishen
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>> the resulting byte size of encoding is " + buffer.readableBytes());
        if (logger.isTraceEnabled()) {
            logger.trace("the resulting byte size of encoding is " + buffer.readableBytes());
        }

    }
 
Example #3
Source File: HeaderExchangeHandlerTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_received_request_twoway_error_reqeustBroken() throws RemotingException {
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(new BizException());
    request.setBroken(true);

    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel() {
        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response) message;
            Assert.assertEquals(request.getId(), res.getId());
            Assert.assertEquals(request.getVersion(), res.getVersion());
            Assert.assertEquals(Response.BAD_REQUEST, res.getStatus());
            Assert.assertNull(res.getResult());
            Assert.assertTrue(res.getErrorMessage().contains(BizException.class.getName()));
            count.incrementAndGet();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler());
    hexhandler.received(mchannel, request);
    Assert.assertEquals(1, count.get());
}
 
Example #4
Source File: ExchangeCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
    Request request = new Request(1L);
    request.setData("hello");
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    try {
        codec.encode(channel, encodeBuffer, request);
        Assert.fail();
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
    }

    Response response = new Response(1L);
    response.setResult("hello");
    encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    codec.encode(channel, encodeBuffer, response);
    Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
    Response receiveMessage = (Response) channel.getReceivedMessage();
    Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
    Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
 
Example #5
Source File: HeartBeatTaskTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testHeartBeat() throws Exception {
    url = url.addParameter(Constants.DUBBO_VERSION_KEY, "2.1.1");
    channel.setAttribute(
            HeaderExchangeHandler.KEY_READ_TIMESTAMP, System.currentTimeMillis());
    channel.setAttribute(
            HeaderExchangeHandler.KEY_WRITE_TIMESTAMP, System.currentTimeMillis());
    Thread.sleep( 2000L );
    task.run();
    List<Object> objects = channel.getSentObjects();
    Assert.assertTrue(objects.size() > 0);
    Object obj = objects.get(0);
    Assert.assertTrue(obj instanceof Request);
    Request request = (Request)obj;
    Assert.assertTrue(request.isHeartbeat());
}
 
Example #6
Source File: HeaderExchangeHandler.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public void caught(Channel channel, Throwable exception) throws RemotingException {
    if (exception instanceof ExecutionException) {
        ExecutionException e = (ExecutionException) exception;
        Object msg = e.getRequest();
        if (msg instanceof Request) {
            Request req = (Request) msg;
            if (req.isTwoWay() && ! req.isHeartbeat()) {
                Response res = new Response(req.getId(), req.getVersion());
                res.setStatus(Response.SERVER_ERROR);
                res.setErrorMessage(StringUtils.toString(e));
                channel.send(res);
                return;
            }
        }
    }
    ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    try {
        handler.caught(exchangeChannel, exception);
    } finally {
        HeaderExchangeChannel.removeChannelIfDisconnected(channel);
    }
}
 
Example #7
Source File: ThriftNativeCodec.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
    throws IOException {
    Invocation invocation = (Invocation) request.getData();
    TProtocol protocol = newProtocol(channel.getUrl(), buffer);
    try {
        protocol.writeMessageBegin(new TMessage(
            invocation.getMethodName(), TMessageType.CALL, 
            thriftSeq.getAndIncrement()));
        protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args"));
        for(int i = 0; i < invocation.getParameterTypes().length; i++) {
            Class<?> type = invocation.getParameterTypes()[i];

        }
    } catch (TException e) {
        throw new IOException(e.getMessage(), e);
    }

}
 
Example #8
Source File: ConnectionOrderedChannelHandler.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public void received(Channel channel, Object message) throws RemotingException {
    ExecutorService cexecutor = getExecutorService();
    try {
        cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message));
    } catch (Throwable t) {
        //fix, reject exception can not be sent to consumer because thread pool is full, resulting in consumers waiting till timeout. 修复,由于线程池已满,拒绝异常无法发送给使用者,导致使用者等待超时。
        if (message instanceof Request && t instanceof RejectedExecutionException) {
            Request request = (Request) message;
            if (request.isTwoWay()) {
                String msg = "Server side(" + url.getIp() + "," + url.getPort() + ") threadpool is exhausted ,detail msg:" + t.getMessage();
                Response response = new Response(request.getId(), request.getVersion());
                response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR);
                response.setErrorMessage(msg);
                channel.send(response);
                return;
            }
        }
        throw new ExecutionException(message, channel, getClass() + " error when process received event .", t);
    }
}
 
Example #9
Source File: HeaderExchangeHandlerTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_received_request_oneway() throws RemotingException{
    final Channel mchannel = new MockedChannel();
    
    final Person requestdata = new Person("charles");
    Request request = new Request();
    request.setTwoWay(false);
    request.setData(requestdata);
    
    ExchangeHandler exhandler = new MockedExchangeHandler(){
        public void received(Channel channel, Object message) throws RemotingException {
            Assert.assertEquals(requestdata, message);
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
}
 
Example #10
Source File: HeaderExchangeHandlerTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_received_request_twoway_error_reqeustBroken() throws RemotingException{
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(new BizException());
    request.setBroken(true);
    
    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel(){
        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response)message;
            Assert.assertEquals(request.getId(), res.getId());
            Assert.assertEquals(request.getVersion(), res.getVersion());
            Assert.assertEquals(Response.BAD_REQUEST, res.getStatus());
            Assert.assertNull(res.getResult());
            Assert.assertTrue(res.getErrorMessage().contains(BizException.class.getName()));
            count.incrementAndGet();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler());
    hexhandler.received(mchannel, request);
    Assert.assertEquals(1, count.get());
}
 
Example #11
Source File: ExchangeCodecTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
    Request request = new Request(1L);
    request.setData("hello");
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    try {
        codec.encode(channel, encodeBuffer, request);
        Assert.fail();
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
    }

    Response response = new Response(1L);
    response.setResult("hello");
    encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    codec.encode(channel, encodeBuffer, response);
    Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
    Response receiveMessage = (Response) channel.getReceivedMessage();
    Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
    Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
 
Example #12
Source File: ThriftNativeCodec.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
        throws IOException {
    Invocation invocation = (Invocation) request.getData();
    TProtocol protocol = newProtocol(channel.getUrl(), buffer);
    try {
        protocol.writeMessageBegin(new TMessage(
                invocation.getMethodName(), TMessageType.CALL,
                thriftSeq.getAndIncrement()));
        protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args"));
        for (int i = 0; i < invocation.getParameterTypes().length; i++) {
            Class<?> type = invocation.getParameterTypes()[i];

        }
    } catch (TException e) {
        throw new IOException(e.getMessage(), e);
    }

}
 
Example #13
Source File: HeaderExchangeChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    if (closed) {
        throw new RemotingException(this.getLocalAddress(), null, "Failed to send message " + message + ", cause: The channel " + this + " is closed!");
    }
    if (message instanceof Request
            || message instanceof Response
            || message instanceof String) {
        channel.send(message, sent);
    } else {
        Request request = new Request();
        request.setVersion("2.0.0");
        request.setTwoWay(false);
        request.setData(message);
        channel.send(request, sent);
    }
}
 
Example #14
Source File: HeaderExchangeHandlerTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_received_request_twoway_error_reqeustBroken() throws RemotingException{
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(new BizException());
    request.setBroken(true);
    
    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel(){
        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response)message;
            Assert.assertEquals(request.getId(), res.getId());
            Assert.assertEquals(request.getVersion(), res.getVersion());
            Assert.assertEquals(Response.BAD_REQUEST, res.getStatus());
            Assert.assertNull(res.getResult());
            Assert.assertTrue(res.getErrorMessage().contains(BizException.class.getName()));
            count.incrementAndGet();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler());
    hexhandler.received(mchannel, request);
    Assert.assertEquals(1, count.get());
}
 
Example #15
Source File: HeaderExchangeHandlerTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_received_request_oneway() throws RemotingException{
    final Channel mchannel = new MockedChannel();
    
    final Person requestdata = new Person("charles");
    Request request = new Request();
    request.setTwoWay(false);
    request.setData(requestdata);
    
    ExchangeHandler exhandler = new MockedExchangeHandler(){
        public void received(Channel channel, Object message) throws RemotingException {
            Assert.assertEquals(requestdata, message);
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
}
 
Example #16
Source File: HeaderExchangeHandlerTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_received_request_twoway_error_reqeustBroken() throws RemotingException{
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(new BizException());
    request.setBroken(true);
    
    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel(){
        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response)message;
            Assert.assertEquals(request.getId(), res.getId());
            Assert.assertEquals(request.getVersion(), res.getVersion());
            Assert.assertEquals(Response.BAD_REQUEST, res.getStatus());
            Assert.assertNull(res.getResult());
            Assert.assertTrue(res.getErrorMessage().contains(BizException.class.getName()));
            count.incrementAndGet();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler());
    hexhandler.received(mchannel, request);
    Assert.assertEquals(1, count.get());
}
 
Example #17
Source File: ThriftCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void encode( Channel channel, ChannelBuffer buffer, Object message )
        throws IOException {

    if ( message instanceof Request ) {
        encodeRequest( channel, buffer, ( Request ) message );
    }
    else if ( message instanceof Response ) {
        encodeResponse( channel, buffer, ( Response ) message );
    } else {
        throw new UnsupportedOperationException(
                new StringBuilder( 32 )
                        .append( "Thrift codec only support encode " )
                        .append( Request.class.getName() )
                        .append( " and " )
                        .append( Response.class.getName() )
                        .toString() );
    }

}
 
Example #18
Source File: ThriftCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void encode( Channel channel, ChannelBuffer buffer, Object message )
        throws IOException {

    if ( message instanceof Request ) {
        encodeRequest( channel, buffer, ( Request ) message );
    }
    else if ( message instanceof Response ) {
        encodeResponse( channel, buffer, ( Response ) message );
    } else {
        throw new UnsupportedOperationException(
                new StringBuilder( 32 )
                        .append( "Thrift codec only support encode " )
                        .append( Request.class.getName() )
                        .append( " and " )
                        .append( Response.class.getName() )
                        .toString() );
    }

}
 
Example #19
Source File: ThriftCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void encode( Channel channel, ChannelBuffer buffer, Object message )
        throws IOException {

    if ( message instanceof Request ) {
        encodeRequest( channel, buffer, ( Request ) message );
    }
    else if ( message instanceof Response ) {
        encodeResponse( channel, buffer, ( Response ) message );
    } else {
        throw new UnsupportedOperationException(
                new StringBuilder( 32 )
                        .append( "Thrift codec only support encode " )
                        .append( Request.class.getName() )
                        .append( " and " )
                        .append( Response.class.getName() )
                        .toString() );
    }

}
 
Example #20
Source File: ExchangeCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSerializaitonId() throws Exception {
    byte[] header = new byte[]{MAGIC_HIGH, MAGIC_LOW, (byte)0x8F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    Object obj =  decode(header);
    Assert.assertTrue(obj instanceof Request);
    Request request = (Request) obj;
    Assert.assertTrue(request.isBroken());
    Assert.assertTrue(request.getData() instanceof IOException);
    header = new byte[]{MAGIC_HIGH, MAGIC_LOW, (byte)0x1F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    obj = decode(header);
    Assert.assertTrue(obj instanceof Response);
    Response response = (Response) obj;
    Assert.assertEquals(response.getStatus(), Response.CLIENT_ERROR);
    Assert.assertTrue(response.getErrorMessage().contains("IOException"));
}
 
Example #21
Source File: DeprecatedExchangeCodec.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
protected void encodeRequest(Channel channel, OutputStream os, Request req) throws IOException {
    Serialization serialization = CodecSupport.getSerialization(channel.getUrl());
    // header.
    byte[] header = new byte[HEADER_LENGTH];
    // set magic number.
    Bytes.short2bytes(MAGIC, header);

    // set request and serialization flag.
    header[2] = (byte) (FLAG_REQUEST | serialization.getContentTypeId());

    if (req.isTwoWay()) header[2] |= FLAG_TWOWAY;
    if (req.isEvent()) header[2] |= FLAG_EVENT;

    // set request id.
    Bytes.long2bytes(req.getId(), header, 4);

    // encode request data.
    UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
    ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
    if (req.isEvent()) {
        encodeEventData(channel, out, req.getData());
    } else {
        encodeRequestData(channel, out, req.getData());
    }
    out.flushBuffer();
    bos.flush();
    bos.close();
    byte[] data = bos.toByteArray();
    checkPayload(channel, data.length);
    Bytes.int2bytes(data.length, header, 12);

    // write
    os.write(header); // write header.
    os.write(data); // write data.
}
 
Example #22
Source File: ExchangeCodecTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void test_Decode_Return_Request_Event_String() throws IOException{
    //|10011111|20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xff, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    String event = Request.READONLY_EVENT;
    byte[] request = getRequestBytes(event, header);
    
    Request obj = (Request)decode(request);
    Assert.assertEquals(event, obj.getData());
    Assert.assertEquals(true, obj.isTwoWay());
    Assert.assertEquals(true, obj.isEvent());
    Assert.assertEquals("2.0.0", obj.getVersion());
    System.out.println(obj);
}
 
Example #23
Source File: ExchangeCodecTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void test_Decode_Return_Request_Event_Object() throws IOException{
    //|10011111|20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xff, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Person person = new Person();
    byte[] request = getRequestBytes(person, header);
    
    Request obj = (Request)decode(request);
    Assert.assertEquals(person, obj.getData());
    Assert.assertEquals(true, obj.isTwoWay());
    Assert.assertEquals(true, obj.isEvent());
    Assert.assertEquals("2.0.0", obj.getVersion());
    System.out.println(obj);
}
 
Example #24
Source File: DecodeHandler.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void received(Channel channel, Object message) throws RemotingException {
    if (message instanceof Decodeable) {
        decode(message);
    }

    if (message instanceof Request) {
        decode(((Request)message).getData());
    }

    if (message instanceof Response) {
        decode( ((Response)message).getResult());
    }

    handler.received(channel, message);
}
 
Example #25
Source File: HeartbeatHandler.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
    public void received(Channel channel, Object message) throws RemotingException {
        setReadTimestamp(channel);
//        是否是心跳检测时间
        if (isHeartbeatRequest(message)) {
            Request req = (Request) message;
            if (req.isTwoWay()) {
                Response res = new Response(req.getId(), req.getVersion());
                res.setEvent(Response.HEARTBEAT_EVENT);
//                发送心跳检测请求
                channel.send(res);
                if (logger.isInfoEnabled()) {
//                    获取心跳检测时间 heartbeat属性值
                    int heartbeat = channel.getUrl().getParameter(Constants.HEARTBEAT_KEY, 0);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Received heartbeat from remote channel " + channel.getRemoteAddress()
                                + ", cause: The channel has no data-transmission exceeds a heartbeat period"
                                + (heartbeat > 0 ? ": " + heartbeat + "ms" : ""));
                    }
                }
            }
            return;
        }
//        是否是心跳检测的响应信息
        if (isHeartbeatResponse(message)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Receive heartbeat response in thread " + Thread.currentThread().getName());
            }
            return;
        }
//        =》com.alibaba.dubbo.remoting.transport.dispatcher.all.AllChannelHandler.received()
        handler.received(channel, message);
    }
 
Example #26
Source File: HeaderExchangeServer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void sendChannelReadOnlyEvent(){
    Request request = new Request();
    request.setEvent(Request.READONLY_EVENT);
    request.setTwoWay(false);
    request.setVersion(Version.getVersion());
    
    Collection<Channel> channels = getChannels();
    for (Channel channel : channels) {
        try {
            if (channel.isConnected())channel.send(request, getUrl().getParameter(Constants.CHANNEL_READONLYEVENT_SENT_KEY, true));
        } catch (RemotingException e) {
            logger.warn("send connot write messge error.", e);
        }
    }
}
 
Example #27
Source File: HeaderExchangeHandlerTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void test_received_request_event_readonly() throws RemotingException{
    final Request request = new Request();
    request.setTwoWay(true);
    request.setEvent(Request.READONLY_EVENT);
    
    final Channel mchannel = new MockedChannel();
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler());
    hexhandler.received(mchannel, request);
    Assert.assertTrue(mchannel.hasAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY));
}
 
Example #28
Source File: HeartbeatHandler.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void received(Channel channel, Object message) throws RemotingException {
    setReadTimestamp(channel);
    if (isHeartbeatRequest(message)) {
        Request req = (Request) message;
        if (req.isTwoWay()) {
            Response res = new Response(req.getId(), req.getVersion());
            res.setEvent(Response.HEARTBEAT_EVENT);
            channel.send(res);
            if (logger.isInfoEnabled()) {
                int heartbeat = channel.getUrl().getParameter(Constants.HEARTBEAT_KEY, 0);
                if(logger.isDebugEnabled()) {
                    logger.debug("Received heartbeat from remote channel " + channel.getRemoteAddress()
                                    + ", cause: The channel has no data-transmission exceeds a heartbeat period"
                                    + (heartbeat > 0 ? ": " + heartbeat + "ms" : ""));
                }
         }
        }
        return;
    }
    if (isHeartbeatResponse(message)) {
        if (logger.isDebugEnabled()) {
        	logger.debug(
                new StringBuilder(32)
                    .append("Receive heartbeat response in thread ")
                    .append(Thread.currentThread().getName())
                    .toString());
        }
        return;
    }
    handler.received(channel, message);
}
 
Example #29
Source File: DataSizeCodecWrapper.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
/**
 * deserialization operation
 * @param channel
 * @param input
 * @return
 * @throws IOException
 */
@Override
public Object decode(Channel channel, ChannelBuffer input) throws IOException {
    long startTime = System.currentTimeMillis();
    int index = input.readerIndex();
    Object ret = codec.decode(channel, input);
    int size = input.readerIndex() - index;
    long elapsed = System.currentTimeMillis() - startTime;
    if (ret instanceof Request) {
        // server-side deserialize the Request
        Object data = ((Request) ret).getData();
        if (data instanceof RpcInvocation) {
            RpcInvocation invocation = (RpcInvocation) data;
            invocation.setAttachment(AttachmentKeyConstants.SERVER_DESERIALIZE_SIZE,
                String.valueOf(size));
            invocation.setAttachment(AttachmentKeyConstants.SERVER_DESERIALIZE_TIME,
                String.valueOf(elapsed));
        }
    } else if (ret instanceof Response) {
        // client-side deserialize the Response
        Object result = ((Response) ret).getResult();
        if (result instanceof RpcResult) {
            RpcResult rpcResult = (RpcResult) result;
            rpcResult.setAttachment(AttachmentKeyConstants.CLIENT_DESERIALIZE_SIZE,
                String.valueOf(size));
            rpcResult.setAttachment(AttachmentKeyConstants.CLIENT_DESERIALIZE_TIME,
                String.valueOf(elapsed));
        }
    }
    return ret;
}
 
Example #30
Source File: DefaultFuture.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public DefaultFuture(Channel channel, Request request, int timeout){
    this.channel = channel;
    this.request = request;
    this.id = request.getId();
    this.timeout = timeout > 0 ? timeout : channel.getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
    // put into waiting map.
    FUTURES.put(id, this);
    CHANNELS.put(id, channel);
}