Search in sources :

Example 21 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class NettyTransporterTest method shouldConnectToNetty4Server.

@Test
public void shouldConnectToNetty4Server() throws Exception {
    final CountDownLatch lock = new CountDownLatch(1);
    int port = NetUtils.getAvailablePort();
    URL url = new URL("telnet", "localhost", port, new String[] { Constants.BIND_PORT_KEY, String.valueOf(port) });
    new NettyTransporter().bind(url, new ChannelHandlerAdapter() {

        @Override
        public void connected(Channel channel) {
            lock.countDown();
        }
    });
    new NettyTransporter().connect(url, new ChannelHandlerAdapter() {

        @Override
        public void sent(Channel channel, Object message) throws RemotingException {
            channel.send(message);
            channel.close();
        }
    });
    lock.await();
}
Also used : ChannelHandlerAdapter(org.apache.dubbo.remoting.transport.ChannelHandlerAdapter) Channel(org.apache.dubbo.remoting.Channel) RemotingException(org.apache.dubbo.remoting.RemotingException) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 22 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class HeaderExchangeChannelTest method requestTest03.

@Test
public void requestTest03() throws RemotingException {
    Assertions.assertThrows(RemotingException.class, () -> {
        channel = new MockChannel() {

            @Override
            public void send(Object req) throws RemotingException {
                throw new RemotingException(channel.getLocalAddress(), channel.getRemoteAddress(), "throw error");
            }
        };
        header = new HeaderExchangeChannel(channel);
        Object requestob = new Object();
        header.request(requestob, 1000);
    });
}
Also used : RemotingException(org.apache.dubbo.remoting.RemotingException) Test(org.junit.jupiter.api.Test)

Example 23 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class HeaderExchangeHandlerTest method test_received_request_event_other_discard.

@Test
public void test_received_request_event_other_discard() throws RemotingException {
    final Request request = new Request();
    request.setTwoWay(true);
    request.setEvent("my event");
    final Channel mchannel = new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Assertions.fail();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler() {

        @Override
        public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
            Assertions.fail();
            throw new RemotingException(channel, "");
        }

        @Override
        public void received(Channel channel, Object message) throws RemotingException {
            Assertions.fail();
            throw new RemotingException(channel, "");
        }
    });
    hexhandler.received(mchannel, request);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) RemotingException(org.apache.dubbo.remoting.RemotingException) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Test(org.junit.jupiter.api.Test)

Example 24 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class NettyChannel method send.

@Override
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + PayloadDropper.getRequestWithoutData(message) + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    if (!success) {
        throw new RemotingException(this, "Failed to send message " + PayloadDropper.getRequestWithoutData(message) + " to " + getRemoteAddress() + "in timeout(" + timeout + "ms) limit");
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) RemotingException(org.apache.dubbo.remoting.RemotingException)

Example 25 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class DeprecatedExchangeCodec method encodeResponse.

protected void encodeResponse(Channel channel, OutputStream os, Response res) throws IOException {
    try {
        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] = serialization.getContentTypeId();
        if (res.isHeartbeat())
            header[2] |= FLAG_EVENT;
        // set response status.
        byte status = res.getStatus();
        header[3] = status;
        // set request id.
        Bytes.long2bytes(res.getId(), header, 4);
        UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
        ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
        // encode response data or error message.
        if (status == Response.OK) {
            if (res.isHeartbeat()) {
                encodeHeartbeatData(channel, out, res.getResult());
            } else {
                encodeResponseData(channel, out, res.getResult());
            }
        } else
            out.writeUTF(res.getErrorMessage());
        out.flushBuffer();
        bos.flush();
        bos.close();
        byte[] data = bos.toByteArray();
        checkPayload(channel, data.length);
        Bytes.int2bytes(data.length, header, 12);
        // write
        // write header.
        os.write(header);
        // write data.
        os.write(data);
    } catch (Throwable t) {
        // send error message to Consumer, otherwise, Consumer will wait until timeout.
        if (!res.isEvent() && res.getStatus() != Response.BAD_RESPONSE) {
            try {
                // FIXME log error info in Codec and put all error handle logic in IoHanndler?
                logger.warn("Fail to encode response: " + res + ", send bad_response info instead, cause: " + t.getMessage(), t);
                Response r = new Response(res.getId(), res.getVersion());
                r.setStatus(Response.BAD_RESPONSE);
                r.setErrorMessage("Failed to send response: " + res + ", cause: " + StringUtils.toString(t));
                channel.send(r);
                return;
            } catch (RemotingException e) {
                logger.warn("Failed to send bad_response info back: " + res + ", cause: " + e.getMessage(), e);
            }
        }
        // Rethrow exception
        if (t instanceof IOException) {
            throw (IOException) t;
        } else if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else if (t instanceof Error) {
            throw (Error) t;
        } else {
            throw new RuntimeException(t.getMessage(), t);
        }
    }
}
Also used : Serialization(org.apache.dubbo.common.serialize.Serialization) Response(org.apache.dubbo.remoting.exchange.Response) ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) RemotingException(org.apache.dubbo.remoting.RemotingException) UnsafeByteArrayOutputStream(org.apache.dubbo.common.io.UnsafeByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

RemotingException (org.apache.dubbo.remoting.RemotingException)39 IOException (java.io.IOException)13 Request (org.apache.dubbo.remoting.exchange.Request)7 Response (org.apache.dubbo.remoting.exchange.Response)6 RpcException (org.apache.dubbo.rpc.RpcException)6 Channel (org.apache.dubbo.remoting.Channel)5 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)5 Test (org.junit.jupiter.api.Test)5 InetSocketAddress (java.net.InetSocketAddress)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeoutException (org.apache.dubbo.remoting.TimeoutException)3 Transporter (org.apache.dubbo.remoting.Transporter)3 ExchangeClient (org.apache.dubbo.remoting.exchange.ExchangeClient)3 AppResponse (org.apache.dubbo.rpc.AppResponse)3 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)3 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)3 ChannelFuture (io.netty.channel.ChannelFuture)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2