专栏首页码匠的流水账聊聊artemis的gracefulShutdownEnabled

聊聊artemis的gracefulShutdownEnabled

本文主要研究一下artemis的gracefulShutdownEnabled

gracefulShutdownEnabled

activemq-artemis-2.11.0/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java

public class ConfigurationImpl implements Configuration, Serializable {

   //......

   private boolean gracefulShutdownEnabled = ActiveMQDefaultConfiguration.isDefaultGracefulShutdownEnabled();

   private long gracefulShutdownTimeout = ActiveMQDefaultConfiguration.getDefaultGracefulShutdownTimeout();

   //......

   @Override
   public boolean isGracefulShutdownEnabled() {
      return gracefulShutdownEnabled;
   }

   @Override
   public ConfigurationImpl setGracefulShutdownEnabled(final boolean enabled) {
      gracefulShutdownEnabled = enabled;
      return this;
   }

   @Override
   public long getGracefulShutdownTimeout() {
      return gracefulShutdownTimeout;
   }

   @Override
   public ConfigurationImpl setGracefulShutdownTimeout(final long timeout) {
      gracefulShutdownTimeout = timeout;
      return this;
   }

   //......
}
  • ConfigurationImpl定义了两个关于gracefulShutdown的属性,分别是gracefulShutdownEnabled(默认为true)及gracefulShutdownTimeout(默认为-1)

ActiveMQServerImpl

activemq-artemis-2.11.0/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java

public class ActiveMQServerImpl implements ActiveMQServer {

   //......

   public final void stop(boolean failoverOnServerShutdown, boolean isExit) throws Exception {
      stop(failoverOnServerShutdown, false, false, isExit);
   }

   public void stop(boolean failoverOnServerShutdown, final boolean criticalIOError, boolean restarting) {
      this.stop(failoverOnServerShutdown, criticalIOError, restarting, false);
   }

   void stop(boolean failoverOnServerShutdown, final boolean criticalIOError, boolean restarting, boolean isShutdown) {

      logger.debug("Stopping server");

      synchronized (this) {
         if (state == SERVER_STATE.STOPPED || state == SERVER_STATE.STOPPING) {
            return;
         }
         state = SERVER_STATE.STOPPING;

         if (fileStoreMonitor != null) {
            fileStoreMonitor.stop();
            fileStoreMonitor = null;
         }

         if (failoverOnServerShutdown) {
            activation.sendLiveIsStopping();
         }

         stopComponent(connectorsService);

         // we stop the groupingHandler before we stop the cluster manager so binding mappings
         // aren't removed in case of failover
         if (groupingHandler != null) {
            managementService.removeNotificationListener(groupingHandler);
            stopComponent(groupingHandler);
         }
         stopComponent(federationManager);
         stopComponent(clusterManager);

         if (remotingService != null) {
            remotingService.pauseAcceptors();
         }

         // allows for graceful shutdown
         if (remotingService != null && configuration.isGracefulShutdownEnabled()) {
            long timeout = configuration.getGracefulShutdownTimeout();
            try {
               if (timeout == -1) {
                  remotingService.getConnectionCountLatch().await();
               } else {
                  remotingService.getConnectionCountLatch().await(timeout);
               }
            } catch (InterruptedException e) {
               ActiveMQServerLogger.LOGGER.interruptWhilstStoppingComponent(remotingService.getClass().getName());
            }
         }

         freezeConnections();
      }

      //......

    }

   static void stopComponent(ActiveMQComponent component) {
      try {
         if (component != null) {
            component.stop();
         }
      } catch (Throwable t) {
         ActiveMQServerLogger.LOGGER.errorStoppingComponent(t, component.getClass().getName());
      }
   }

   //......

}    
  • ActiveMQServerImpl在remotingService不为null且configuration.isGracefulShutdownEnabled()为true时会读取configuration.getGracefulShutdownTimeout();若timeout为-1,则执行remotingService.getConnectionCountLatch().await(),否则执行remotingService.getConnectionCountLatch().await(timeout)

connectionCountLatch

activemq-artemis-2.11.0/artemis-server/src/main/java/org/apache/activemq/artemis/core/remoting/server/impl/RemotingServiceImpl.java

public class RemotingServiceImpl implements RemotingService, ServerConnectionLifeCycleListener {

   //......

   private final ReusableLatch connectionCountLatch = new ReusableLatch(0);

   //......

   public synchronized ReusableLatch getConnectionCountLatch() {
      return connectionCountLatch;
   }

   public void connectionCreated(final ActiveMQComponent component,
                                 final Connection connection,
                                 final ProtocolManager protocol) {
      if (server == null) {
         throw new IllegalStateException("Unable to create connection, server hasn't finished starting up");
      }

      ConnectionEntry entry = protocol.createConnectionEntry((Acceptor) component, connection);
      try {
         if (server.hasBrokerConnectionPlugins()) {
            server.callBrokerConnectionPlugins(plugin -> plugin.afterCreateConnection(entry.connection));
         }
      } catch (ActiveMQException t) {
         logger.warn("Error executing afterCreateConnection plugin method: {}", t.getMessage(), t);
         throw new IllegalStateException(t.getMessage(), t.getCause());

      }
      if (logger.isTraceEnabled()) {
         logger.trace("Connection created " + connection);
      }

      connections.put(connection.getID(), entry);
      connectionCountLatch.countUp();
      totalConnectionCount.incrementAndGet();
   }

   public RemotingConnection removeConnection(final Object remotingConnectionID) {
      ConnectionEntry entry = connections.remove(remotingConnectionID);

      if (entry != null) {
         logger.debug("RemotingServiceImpl::removing connection ID " + remotingConnectionID);
         connectionCountLatch.countDown();
         return entry.connection;
      } else {
         logger.debug("The connectionID::" + remotingConnectionID + " was already removed by some other module");

         return null;
      }
   }

   //......

}        
  • getConnectionCountLatch返回的是connectionCountLatch;connectionCreated方法会执行connectionCountLatch.countUp();removeConnection方法会执行connectionCountLatch.countDown()

小结

ConfigurationImpl定义了两个关于gracefulShutdown的属性,分别是gracefulShutdownEnabled(默认为true)及gracefulShutdownTimeout(默认为-1);ActiveMQServerImpl在remotingService不为null且configuration.isGracefulShutdownEnabled()为true时会读取configuration.getGracefulShutdownTimeout();若timeout为-1,则执行remotingService.getConnectionCountLatch().await(),否则执行remotingService.getConnectionCountLatch().await(timeout)

doc

  • ConfigurationImpl

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-01-18

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊artemis的gracefulShutdownEnabled

    activemq-artemis-2.11.0/artemis-server/src/main/java/org/apache/activemq/artemis...

    codecraft
  • futureTask的超时原理解析

    java/util/concurrent/AbstractExecutorService.java

    codecraft
  • 聊聊eureka client的serviceUrl

    eureka-client-1.8.8-sources.jar!/com/netflix/discovery/DiscoveryClient.java

    codecraft
  • 聊聊artemis的gracefulShutdownEnabled

    activemq-artemis-2.11.0/artemis-server/src/main/java/org/apache/activemq/artemis...

    codecraft
  • futureTask的超时原理解析

    java/util/concurrent/AbstractExecutorService.java

    codecraft
  • 【python系统学习04】条件判断语句

    学过 js 的你,看到这个肯定小 case 吧!肯定第一时间得到答案,打印出“1”吧!

    xing.org1^
  • python基础(一)

    py3study
  • python基础1习题练习

    py3study
  • 带着问题去看源码——LayoutInflater

    LayoutInflater是通过Pull解析(XmlPullParser解析器)方式来解析XML布局文件,解析出节点名之后,然后会调用rInflate方法,这...

    用户2802329
  • 聊聊canal的BinLogFileQueue

    canal-1.1.4/parse/src/main/java/com/alibaba/otter/canal/parse/inbound/mysql/loca...

    codecraft

扫码关注云+社区

领取腾讯云代金券