com.alibaba.dubbo.remoting.RemotingException Java Examples

The following examples show how to use com.alibaba.dubbo.remoting.RemotingException. 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: FileExchangeGroup.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
public ExchangePeer joinExchange(URL url, ExchangeHandler handler) throws RemotingException {
    ExchangePeer peer = super.join(url, handler);
    try {
        String full = url.toFullString();
        String[] lines = IOUtils.readLines(file);
        for (String line : lines) {
            if (full.equals(line)) {
                return peer;
            }
        }
        IOUtils.appendLines(file, new String[]{full});
    } catch (IOException e) {
        throw new RemotingException(new InetSocketAddress(NetUtils.getLocalHost(), 0), getUrl().toInetSocketAddress(), e.getMessage(), e);
    }
    return peer;
}
 
Example #2
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 #3
Source File: NettyChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
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(Constants.TIMEOUT_KEY, Constants.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 " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example #4
Source File: InvokerTelnetHandlerTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testInvokeDefaultSService() throws RemotingException {
    mockInvoker = mock(Invoker.class);
    given(mockInvoker.getInterface()).willReturn(DemoService.class);
    given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:20886/demo"));
    given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok"));
    mockChannel = mock(Channel.class);
    given(mockChannel.getAttribute("telnet.service")).willReturn("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService");
    given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
    given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));

    DubboProtocol.getDubboProtocol().export(mockInvoker);
    String result = invoke.telnet(mockChannel, "DemoService.echo(\"ok\")");
    assertTrue(result.contains("Use default service com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService.\r\n\"ok\"\r\n"));
}
 
Example #5
Source File: ThriftProtocol.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
private ExchangeServer getServer(URL url) {
    //默认开启server关闭时发送readonly事件
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    String str = url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_SERVER);

    if (str != null && str.length() > 0 && ! ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str))
        throw new RpcException("Unsupported server type: " + str + ", url: " + url);

    ExchangeServer server;
    try {
        server = Exchangers.bind(url, handler);
    } catch (RemotingException e) {
        throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e);
    }
    str = url.getParameter(Constants.CLIENT_KEY);
    if (str != null && str.length() > 0) {
        Set<String> supportedTypes = ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions();
        if (!supportedTypes.contains(str)) {
            throw new RpcException("Unsupported client type: " + str);
        }
    }
    return server;
}
 
Example #6
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 #7
Source File: AllChannelHandler.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 {
//            com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run()
            cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message));
        } catch (Throwable t) {
            //TODO A temporary solution to the problem that the exception information can not be sent to the opposite end after the thread pool is full. Need a refactoring 线程池满后异常信息不能发送到另一端的问题的临时解决方案。需要一个重构
            //fix The thread pool is full, refuses to call, does not return, and causes the consumer to wait for time out 修复线程池已满,拒绝调用,不返回,并导致使用者等待超时
        	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 #8
Source File: NettyChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
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(Constants.TIMEOUT_KEY, Constants.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 " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example #9
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 #10
Source File: ListTelnetHandlerTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testListDetail() throws RemotingException {
    int port = NetUtils.getAvailablePort();
    mockInvoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(mockInvoker.getInterface()).andReturn(DemoService.class).anyTimes();
    EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:"+port+"/demo")).anyTimes();
    EasyMock.expect(mockInvoker.invoke((Invocation) EasyMock.anyObject())).andReturn(new RpcResult("ok")).anyTimes();
    mockChannel = EasyMock.createMock(Channel.class);
    EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
    EasyMock.replay(mockChannel, mockInvoker);
    DubboProtocol.getDubboProtocol().export(mockInvoker);
    String result = list.telnet(mockChannel, "-l");
    assertEquals("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService -> dubbo://127.0.0.1:"+port+"/demo", result);
    EasyMock.reset(mockChannel);
}
 
Example #11
Source File: AbstractServer.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
public void connected(Channel ch) throws RemotingException {
    Collection<Channel> channels = getChannels();
    if (accepts > 0 && channels.size() > accepts) {
        logger.error("Close channel " + ch + ", cause: The server " + ch.getLocalAddress() + " connections greater than max config " + accepts);
        ch.close();
        return;
    }
    super.connected(ch);
}
 
Example #12
Source File: DubboProtocol.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Object reply(ExchangeChannel channel, Object message) throws RemotingException {
    if (message instanceof Invocation) {
        Invocation inv = (Invocation) message;
        Invoker<?> invoker = getInvoker(channel, inv);
        //如果是callback 需要处理高版本调用低版本的问题
        if (Boolean.TRUE.toString().equals(inv.getAttachments().get(IS_CALLBACK_SERVICE_INVOKE))){
            String methodsStr = invoker.getUrl().getParameters().get("methods");
            boolean hasMethod = false;
            if (methodsStr == null || methodsStr.indexOf(",") == -1){
                hasMethod = inv.getMethodName().equals(methodsStr);
            } else {
                String[] methods = methodsStr.split(",");
                for (String method : methods){
                    if (inv.getMethodName().equals(method)){
                        hasMethod = true;
                        break;
                    }
                }
            }
            if (!hasMethod){
                logger.warn(new IllegalStateException("The methodName "+inv.getMethodName()+" not found in callback service interface ,invoke will be ignored. please update the api interface. url is:" + invoker.getUrl()) +" ,invocation is :"+inv );
                return null;
            }
        }
        RpcContext.getContext().setRemoteAddress(channel.getRemoteAddress());
        return invoker.invoke(inv);
    }
    throw new RemotingException(channel, "Unsupported request: " + message == null ? null : (message.getClass().getName() + ": " + message) + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress());
}
 
Example #13
Source File: LogTelnetHandlerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeLogLevel() throws RemotingException {
    mockChannel = EasyMock.createMock(Channel.class);
    EasyMock.replay(mockChannel);
    String result = log.telnet(mockChannel, "error");
    assertTrue(result.contains("\r\nCURRENT LOG LEVEL:ERROR"));
    String result2 = log.telnet(mockChannel, "warn");
    assertTrue(result2.contains("\r\nCURRENT LOG LEVEL:WARN"));
    EasyMock.reset(mockChannel);
}
 
Example #14
Source File: ConnectChannelHandlerTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ExecutionException.class)
public void test_Disconnect_Execute_Error() throws RemotingException{
    handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url);
    ThreadPoolExecutor executor = (ThreadPoolExecutor)getField(handler, "connectionExecutor", 1);
    executor.shutdown();
    handler.disconnected(new MockedChannel());
}
 
Example #15
Source File: AbstractClient.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    if (send_reconnect && !isConnected()){
        connect();
    }
    Channel channel = getChannel();
    //TODO getChannel返回的状态是否包含null需要改进
    if (channel == null || ! channel.isConnected()) {
      throw new RemotingException(this, "message can not send, because channel is closed . url:" + getUrl());
    }
    channel.send(message, sent);
}
 
Example #16
Source File: ListTelnetHandlerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testListService() throws RemotingException {
    mockInvoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(mockInvoker.getInterface()).andReturn(DemoService.class).anyTimes();
    EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:20885/demo")).anyTimes();
    EasyMock.expect(mockInvoker.invoke((Invocation) EasyMock.anyObject())).andReturn(new RpcResult("ok")).anyTimes();
    mockChannel = EasyMock.createMock(Channel.class);
    EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService").anyTimes();
    EasyMock.replay(mockChannel, mockInvoker);
    DubboProtocol.getDubboProtocol().export(mockInvoker);
    String result = list.telnet(mockChannel, "DemoService");
    assertEquals(methodsName, result);
    EasyMock.reset(mockChannel, mockInvoker);
}
 
Example #17
Source File: HeartBeatExchangeHandler.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void received( Channel channel, Object message ) throws RemotingException {
    if ( message instanceof Request ) {
        Request req = ( Request ) message;
        if ( req.isHeartbeat() ) {
            heartBeatCounter.incrementAndGet();
            channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis());
            Response res = new Response( req.getId(), req.getVersion() );
            res.setEvent( req.getData() == null ? null : req.getData().toString() );
            channel.send( res );
        }
    } else {
        super.received( channel, message );
    }
}
 
Example #18
Source File: MulticastGroup.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void send(String msg) throws RemotingException {
    DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), mutilcastAddress, mutilcastSocket.getLocalPort());
    try {
        mutilcastSocket.send(hi);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #19
Source File: WrappedChannelHandlerTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public void disconnected(Channel channel) throws RemotingException {
    if (invokeWithBizError) {
        throw new RemotingException(channel, "test disconnect biz error");
    }
    sleep(20);
}
 
Example #20
Source File: ListTelnetHandlerTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testList() throws RemotingException {
    mockInvoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(mockInvoker.getInterface()).andReturn(DemoService.class).anyTimes();
    EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:20885/demo")).anyTimes();
    EasyMock.expect(mockInvoker.invoke((Invocation) EasyMock.anyObject())).andReturn(new RpcResult("ok")).anyTimes();
    mockChannel = EasyMock.createMock(Channel.class);
    EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
    EasyMock.replay(mockChannel, mockInvoker);
    DubboProtocol.getDubboProtocol().export(mockInvoker);
    String result = list.telnet(mockChannel, "");
    assertEquals("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService", result);
    EasyMock.reset(mockChannel);
}
 
Example #21
Source File: AbstractExchangeGroup.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public ExchangePeer join(URL url, ExchangeHandler handler) throws RemotingException {
    ExchangeServer server = servers.get(url);
    if (server == null) { // TODO 有并发间隙
        server = Exchangers.bind(url, handler);
        servers.put(url, server);
        dispatcher.addChannelHandler(handler);
    }
    return new ExchangeServerPeer(server, clients, this);
}
 
Example #22
Source File: AbstractServer.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
public void disconnected(Channel ch) throws RemotingException {
    Collection<Channel> channels = getChannels();
    if (channels.size() == 0){
        logger.warn("All clients has discontected from " + ch.getLocalAddress() + ". You can graceful shutdown now.");
    }
    super.disconnected(ch);
}
 
Example #23
Source File: LazyConnectExchangeClient.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
private void initClient() throws RemotingException {
    if (client != null)
        return;
    if (logger.isInfoEnabled()) {
        logger.info("Lazy connect to " + url);
    }
    connectLock.lock();
    try {
        if (client != null)
            return;
        this.client = Exchangers.connect(url, requestHandler);
    } finally {
        connectLock.unlock();
    }
}
 
Example #24
Source File: MultiMessageHandler.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
   public void received(Channel channel, Object message) throws RemotingException {
       if (message instanceof MultiMessage) {
           MultiMessage list = (MultiMessage)message;
           for(Object obj : list) {
               handler.received(channel, obj);
           }
       } else {
           handler.received(channel, message);
       }
   }
 
Example #25
Source File: WrappedChannelHandlerTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
public void received(Channel channel, Object message) throws RemotingException {
    if (invokeWithBizError){
        throw new RemotingException(channel, "test received biz error");
    }
    sleep(20);
}
 
Example #26
Source File: MulticastExchangeGroup.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private void send(String msg) throws RemotingException {
    DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), mutilcastAddress, mutilcastSocket.getLocalPort());
    try {
        mutilcastSocket.send(hi);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #27
Source File: CurrentTelnetHandlerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testService() throws RemotingException {
    mockChannel = EasyMock.createMock(Channel.class);
    EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService").anyTimes();
    EasyMock.replay(mockChannel);
    String result = count.telnet(mockChannel, "");
    assertEquals("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService", result);
}
 
Example #28
Source File: GrizzlyHandler.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
public NextAction handleWrite(FilterChainContext ctx) throws IOException {
    Connection<?> connection = ctx.getConnection();
    GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
        handler.sent(channel, ctx.getMessage());
    } catch (RemotingException e) {
        throw new IOException(StringUtils.toString(e));
    } finally {
        GrizzlyChannel.removeChannelIfDisconnectd(connection);
    }
    return ctx.getInvokeAction();
}
 
Example #29
Source File: ConnectionOrderedChannelHandler.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void caught(Channel channel, Throwable exception) throws RemotingException {
    ExecutorService cexecutor = executor;
    if (cexecutor == null || cexecutor.isShutdown()) { 
        cexecutor = SHARED_EXECUTOR;
    } 
    try{
        cexecutor.execute(new ChannelEventRunnable(channel, handler ,ChannelState.CAUGHT, exception));
    }catch (Throwable t) {
        throw new ExecutionException("caught event", channel, getClass()+" error when process caught event ." , t);
    }
}
 
Example #30
Source File: MulticastGroup.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private void send(String msg) throws RemotingException {
    DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), mutilcastAddress, mutilcastSocket.getLocalPort());
    try {
        mutilcastSocket.send(hi);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}