Search in sources :

Example 31 with Response

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

the class ExchangeCodecTest method test_Encode_Response.

@Test
public void test_Encode_Response() throws IOException {
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
    Channel channel = getCliendSideChannel(url);
    Response response = new Response();
    response.setHeartbeat(true);
    response.setId(1001L);
    response.setStatus((byte) 20);
    response.setVersion("11");
    Person person = new Person();
    response.setResult(person);
    codec.encode(channel, encodeBuffer, response);
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    // encode resault check need decode
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Response obj = (Response) codec.decode(channel, decodeBuffer);
    Assertions.assertEquals(response.getId(), obj.getId());
    Assertions.assertEquals(response.getStatus(), obj.getStatus());
    Assertions.assertEquals(response.isHeartbeat(), obj.isHeartbeat());
    Assertions.assertEquals(person, obj.getResult());
// encode response verson ??
// Assertions.assertEquals(response.getProtocolVersion(), obj.getVersion());
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Channel(org.apache.dubbo.remoting.Channel) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.jupiter.api.Test)

Example 32 with Response

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

the class ExchangeCodecTest method test_Header_Response_NoSerializationFlag.

@Test
public void test_Header_Response_NoSerializationFlag() throws IOException {
    // 00000010-response/oneway/hearbeat=false/noset |20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0x02, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Person person = new Person();
    byte[] request = getRequestBytes(person, header);
    Response obj = (Response) decode(request);
    Assertions.assertEquals(20, obj.getStatus());
    Assertions.assertEquals(person, obj.getResult());
    System.out.println(obj);
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Test(org.junit.jupiter.api.Test)

Example 33 with Response

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

the class WrappedChannelHandler method getPreferredExecutorService.

/**
 * Currently, this method is mainly customized to facilitate the thread model on consumer side.
 * 1. Use ThreadlessExecutor, aka., delegate callback directly to the thread initiating the call.
 * 2. Use shared executor to execute the callback.
 *
 * @param msg
 * @return
 */
public ExecutorService getPreferredExecutorService(Object msg) {
    if (msg instanceof Response) {
        Response response = (Response) msg;
        DefaultFuture responseFuture = DefaultFuture.getFuture(response.getId());
        // a typical scenario is the response returned after timeout, the timeout response may has completed the future
        if (responseFuture == null) {
            return getSharedExecutorService();
        } else {
            ExecutorService executor = responseFuture.getExecutor();
            if (executor == null || executor.isShutdown()) {
                executor = getSharedExecutorService();
            }
            return executor;
        }
    } else {
        return getSharedExecutorService();
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) ExecutorService(java.util.concurrent.ExecutorService) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture)

Example 34 with Response

use of org.apache.dubbo.remoting.exchange.Response 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)

Example 35 with Response

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

the class HeaderExchangeHandler method handleRequest.

void handleRequest(final ExchangeChannel channel, Request req) throws RemotingException {
    Response res = new Response(req.getId(), req.getVersion());
    if (req.isBroken()) {
        Object data = req.getData();
        String msg;
        if (data == null) {
            msg = null;
        } else if (data instanceof Throwable) {
            msg = StringUtils.toString((Throwable) data);
        } else {
            msg = data.toString();
        }
        res.setErrorMessage("Fail to decode request due to: " + msg);
        res.setStatus(Response.BAD_REQUEST);
        channel.send(res);
        return;
    }
    // find handler by message class.
    Object msg = req.getData();
    try {
        CompletionStage<Object> future = handler.reply(channel, msg);
        future.whenComplete((appResult, t) -> {
            try {
                if (t == null) {
                    res.setStatus(Response.OK);
                    res.setResult(appResult);
                } else {
                    res.setStatus(Response.SERVICE_ERROR);
                    res.setErrorMessage(StringUtils.toString(t));
                }
                channel.send(res);
            } catch (RemotingException e) {
                logger.warn("Send result to consumer failed, channel is " + channel + ", msg is " + e);
            }
        });
    } catch (Throwable e) {
        res.setStatus(Response.SERVICE_ERROR);
        res.setErrorMessage(StringUtils.toString(e));
        channel.send(res);
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) RemotingException(org.apache.dubbo.remoting.RemotingException)

Aggregations

Response (org.apache.dubbo.remoting.exchange.Response)39 Request (org.apache.dubbo.remoting.exchange.Request)21 Test (org.junit.jupiter.api.Test)20 Channel (org.apache.dubbo.remoting.Channel)12 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)9 IOException (java.io.IOException)6 RemotingException (org.apache.dubbo.remoting.RemotingException)6 AppResponse (org.apache.dubbo.rpc.AppResponse)6 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)5 TMessage (org.apache.thrift.protocol.TMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 URL (org.apache.dubbo.common.URL)4 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)3 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)3 TApplicationException (org.apache.thrift.TApplicationException)3 ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)2