Search in sources :

Example 41 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method test_Encode_Request.

@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);
    Assertions.assertEquals(request.isBroken(), obj.isBroken());
    Assertions.assertEquals(request.isHeartbeat(), obj.isHeartbeat());
    Assertions.assertEquals(request.isTwoWay(), obj.isTwoWay());
    Assertions.assertEquals(person, obj.getData());
}
Also used : Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.jupiter.api.Test)

Example 42 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Return_Request_Event_Object.

@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) 0xe2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Person person = new Person();
    byte[] request = getRequestBytes(person, header);
    System.setProperty("deserialization.event.size", "100");
    Request obj = (Request) decode(request);
    Assertions.assertEquals(person, obj.getData());
    Assertions.assertTrue(obj.isTwoWay());
    Assertions.assertTrue(obj.isEvent());
    Assertions.assertEquals(Version.getProtocolVersion(), obj.getVersion());
    System.out.println(obj);
    System.clearProperty("deserialization.event.size");
}
Also used : Request(org.apache.dubbo.remoting.exchange.Request) Test(org.junit.jupiter.api.Test)

Example 43 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Return_Request_Object.

@Test
public void test_Decode_Return_Request_Object() throws IOException {
    // |10011111|20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xc2, 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);
    Assertions.assertEquals(person, obj.getData());
    Assertions.assertTrue(obj.isTwoWay());
    Assertions.assertFalse(obj.isHeartbeat());
    Assertions.assertEquals(Version.getProtocolVersion(), obj.getVersion());
    System.out.println(obj);
}
Also used : Request(org.apache.dubbo.remoting.exchange.Request) Test(org.junit.jupiter.api.Test)

Example 44 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class AllChannelHandler2 method received.

@Override
public void received(Channel channel, Object message) throws RemotingException {
    ExecutorService executor = getPreferredExecutorService(message);
    try {
        Future<?> future = executor.submit(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message));
        long timeout = this.url.getParameter("timeout", 1000) + 90;
        TIME_OUT_TIMER.newTimeout(t -> {
            if (!future.isDone() && (!future.isCancelled())) {
                try {
                    future.cancel(true);
                } catch (Throwable ex) {
                // ignore
                }
            }
        }, timeout, TimeUnit.MILLISECONDS);
    } catch (Throwable t) {
        if (message instanceof Request && t instanceof RejectedExecutionException) {
            sendFeedback(channel, (Request) message, t);
            return;
        }
        throw new ExecutionException(message, channel, getClass() + " error when process received event .", t);
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Request(org.apache.dubbo.remoting.exchange.Request) ChannelEventRunnable(org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(org.apache.dubbo.remoting.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 45 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeaderExchangeChannel method send.

@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);
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) RemotingException(org.apache.dubbo.remoting.RemotingException) Request(org.apache.dubbo.remoting.exchange.Request)

Aggregations

Request (org.apache.dubbo.remoting.exchange.Request)51 Test (org.junit.jupiter.api.Test)30 Response (org.apache.dubbo.remoting.exchange.Response)21 Channel (org.apache.dubbo.remoting.Channel)18 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)10 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)9 RemotingException (org.apache.dubbo.remoting.RemotingException)7 TMessage (org.apache.thrift.protocol.TMessage)7 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 AppResponse (org.apache.dubbo.rpc.AppResponse)6 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)6 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)6 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)5 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 URL (org.apache.dubbo.common.URL)4 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)4 IOException (java.io.IOException)3