doOpen(); } catch (Throwable t) { close(); throw new RemotingException(url.toInetSocketAddress(), null, "Failed to start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress() + " connect to the server " + getRemoteAddress() + ", cause: " + t.getMessage(), t); connect(); if (logger.isInfoEnabled()) { logger.info("Start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress() + " connect to the server " + getRemoteAddress()); close(); throw t; } else { logger.warn("Failed to start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress() + " connect to the server " + getRemoteAddress() + " (check == false, ignore and retry later!), cause: " + t.getMessage(), t); close(); throw new RemotingException(url.toInetSocketAddress(), null, "Failed to start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress() + " connect to the server " + getRemoteAddress() + ", cause: " + t.getMessage(), t);
@Override public void reconnect() throws RemotingException { if (!isConnected()) { connectLock.lock(); try { if (!isConnected()) { disconnect(); connect(); } } finally { connectLock.unlock(); } } }
@Override public void close() { try { super.close(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { if (executor != null) { ExecutorUtil.shutdownNow(executor, 100); } } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { disconnect(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { doClose(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } }
@Override public void send(Object message, boolean sent) throws RemotingException { if (needReconnect && !isConnected()) { connect(); } Channel channel = getChannel(); //TODO Can the value returned by getChannel() be null? need improvement. if (channel == null || !channel.isConnected()) { throw new RemotingException(this, "message can not send, because channel is closed . url:" + getUrl()); } channel.send(message, sent); }
@Override public InetSocketAddress getRemoteAddress() { Channel channel = getChannel(); if (channel == null) { return getUrl().toInetSocketAddress(); } return channel.getRemoteAddress(); }
protected void connect() throws RemotingException { connectLock.lock(); try { if (isConnected()) { return; } doConnect(); if (!isConnected()) { throw new RemotingException(this, "Failed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: Connect wait timeout: " + getConnectTimeout() + "ms."); } else { if (logger.isInfoEnabled()) { logger.info("Successed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", channel is " + this.getChannel()); } } } catch (RemotingException e) { throw e; } catch (Throwable e) { throw new RemotingException(this, "Failed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: " + e.getMessage(), e); } finally { connectLock.unlock(); } }
@Override public void setAttribute(String key, Object value) { Channel channel = getChannel(); if (channel == null) { return; } channel.setAttribute(key, value); }
public void disconnect() { connectLock.lock(); try { destroyConnectStatusCheckCommand(); try { Channel channel = getChannel(); if (channel != null) { channel.close(); } } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { doDisConnect(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } finally { connectLock.unlock(); } }
@Override public void run() { try { if (!isConnected()) { connect(); } else { lastConnectedTime = System.currentTimeMillis(); } } catch (Throwable t) { String errorMsg = "client reconnect to " + getUrl().getAddress() + " find error . url: " + getUrl(); // wait registry sync provider list if (System.currentTimeMillis() - lastConnectedTime > shutdown_timeout) { if (!reconnect_error_log_flag.get()) { reconnect_error_log_flag.set(true); logger.error(errorMsg, t); return; } } if (reconnect_count.getAndIncrement() % reconnect_warning_period == 0) { logger.warn(errorMsg, t); } } } };
public void disconnect() { connectLock.lock(); try { try { Channel channel = getChannel(); if (channel != null) { channel.close(); } } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { doDisConnect(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } finally { connectLock.unlock(); } }
@Override public void close(int timeout) { ExecutorUtil.gracefulShutdown(executor, timeout); close(); }
protected void connect() throws RemotingException { connectLock.lock(); try { if (isConnected()) { return; } doConnect(); if (!isConnected()) { throw new RemotingException(this, "Failed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: Connect wait timeout: " + getConnectTimeout() + "ms."); } else { if (logger.isInfoEnabled()) { logger.info("Successed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", channel is " + this.getChannel()); } } } catch (RemotingException e) { throw e; } catch (Throwable e) { throw new RemotingException(this, "Failed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: " + e.getMessage(), e); } finally { connectLock.unlock(); } }
@Override public void send(Object message, boolean sent) throws RemotingException { if (needReconnect && !isConnected()) { connect(); } Channel channel = getChannel(); //TODO Can the value returned by getChannel() be null? need improvement. if (channel == null || !channel.isConnected()) { throw new RemotingException(this, "message can not send, because channel is closed . url:" + getUrl()); } channel.send(message, sent); }
@Override public boolean hasAttribute(String key) { Channel channel = getChannel(); if (channel == null) { return false; } return channel.hasAttribute(key); }
public void disconnect() { connectLock.lock(); try { destroyConnectStatusCheckCommand(); try { Channel channel = getChannel(); if (channel != null) { channel.close(); } } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { doDisConnect(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } finally { connectLock.unlock(); } }
@Override public void run() { try { if (!isConnected()) { connect(); } else { lastConnectedTime = System.currentTimeMillis(); } } catch (Throwable t) { String errorMsg = "client reconnect to " + getUrl().getAddress() + " find error . url: " + getUrl(); // wait registry sync provider list if (System.currentTimeMillis() - lastConnectedTime > shutdown_timeout) { if (!reconnect_error_log_flag.get()) { reconnect_error_log_flag.set(true); logger.error(errorMsg, t); return; } } if (reconnect_count.getAndIncrement() % reconnect_warning_period == 0) { logger.warn(errorMsg, t); } } } };
@Override public InetSocketAddress getRemoteAddress() { Channel channel = getChannel(); if (channel == null) { return getUrl().toInetSocketAddress(); } return channel.getRemoteAddress(); }
public void disconnect() { connectLock.lock(); try { try { Channel channel = getChannel(); if (channel != null) { channel.close(); } } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { doDisConnect(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } finally { connectLock.unlock(); } }
@Override public void close(int timeout) { ExecutorUtil.gracefulShutdown(executor, timeout); close(); }
protected void connect() throws RemotingException { connectLock.lock(); try { if (isConnected()) { return; initConnectStatusCheckCommand(); doConnect(); if (!isConnected()) { throw new RemotingException(this, "Failed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: Connect wait timeout: " + getConnectTimeout() + "ms."); } else { if (logger.isInfoEnabled()) { logger.info("Successed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", channel is " + this.getChannel()); throw e; } catch (Throwable e) { throw new RemotingException(this, "Failed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: " + e.getMessage(), e);