Java Code Examples for com.alibaba.dubbo.remoting.exchange.Request#setData()

The following examples show how to use com.alibaba.dubbo.remoting.exchange.Request#setData() . 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 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 2
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 3
Source File: HeaderExchangeHandlerTest.java    From dubbo3 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 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 5
Source File: ExchangeCodecTest.java    From dubbox-hystrix 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 6
Source File: ExchangeCodecTest.java    From dubbox-hystrix 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 7
Source File: HeaderExchangeHandlerTest.java    From dubbox-hystrix 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 8
Source File: HeaderExchangeHandlerTest.java    From dubbox-hystrix 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 9
Source File: HeaderExchangeChannel.java    From dubbox-hystrix 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 10
Source File: HeaderExchangeChannel.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
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(Version.getProtocolVersion());
        request.setTwoWay(false);
        request.setData(message);
        channel.send(request, sent);
    }
}
 
Example 11
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 12
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 13
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 14
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 15
Source File: ExchangeCodecTest.java    From dubbo3 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 16
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 17
Source File: ExchangeCodecTest.java    From dubbo-2.6.5 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 18
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 19
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_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() {
        @Override
        public void received(Channel channel, Object message) throws RemotingException {
            Assert.assertEquals(requestdata, message);
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
}
 
Example 20
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private Request createRequest() {

        RpcInvocation invocation = new RpcInvocation();

        invocation.setMethodName( "echoString" );

        invocation.setArguments( new Object[]{ "Hello, World!" } );

        invocation.setParameterTypes( new Class<?>[]{ String.class } );

        invocation.setAttachment( Constants.INTERFACE_KEY, Demo.Iface.class.getName() );

        Request request = new Request( 1L );

        request.setData( invocation );

        return request;

    }