文档章节

聊聊artemis的gracefulShutdownEnabled

go4it
 go4it
发布于 01/18 12:49
字数 547
阅读 144
收藏 0

本文主要研究一下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

© 著作权归作者所有

go4it
粉丝 93
博文 1295
码字总数 1210270
作品 0
深圳
私信 提问
加载中

评论(0)

Apache Artemis 1.5.4 发布,嵌入式消息服务

Apache Artemis 提供了一个非堵塞架构,实现了超高性能的 Java 对象消息服务器。其核心只依赖一个 netty.jar 文件。该项目的目的是为你的 Java 应用提供一个可嵌入的消息服务。 Apache Artem...

王练
2017/04/01
787
0
Apache Artemis 1.0.0 发布,嵌入式消息服务

Apache Artemis 1.0.0 发布,是首个发布版本,现已提供下载:apache-artemis-1.0.0-bin.zip 更多内容,请查看软件主页。 去年,HornetQ 代码库捐献给 Apache ActiveMQ 社区,它现在成为 Acti...

sikkx
2015/06/03
4.6K
13
使用apache-artemis搭建MQTT服务器

apache-artemis 搭建MQTT服务,windows下和Linux下操作方式基本一致 1、下载二进制包: 官网地址 https://activemq.apache.org/components/artemis/download/ linux 下载后缀为 .tar.gz 的包...

科陆李明
2019/04/08
411
0
springcloud服务一直输出debug信息,跪求大佬解惑

用了artemis-http-client-1.2-SNAPSHOT,artemis-http-client-1.2-SNAPSHOT-CL后,启动服务,注册中心显示注册成功 ,服务本身一直输出debug信息...

想好好写代码的伪程序
2019/08/30
96
0
已有12000多人申请成为NASA宇航员 希望执行月球和火星任务

据外媒报道,美国宇航局(NASA)最近开展的公开招募“阿尔忒弥斯一代”(Artemis Generation)宇航员活动已经吸引12000多人报名参加。被选为该计划的人员将参与Artemis飞往月球及其他星球的任...

稿源:
04/02
0
0

没有更多内容

加载失败,请刷新页面

加载更多

如何仅克隆Git存储库的子目录? - How do I clone a subdirectory only of a Git repository?

问题: I have my Git repository which, at the root, has two sub directories: 我有我的Git存储库,它的根目录有两个子目录: /finisht/static When this was in SVN , /finisht was ch......

技术盛宴
37分钟前
45
0
【 开发指南 】智能家居技能

本文作者:DuerOs 目录 一、教学视频 二、整体架构 三、开发步骤&资料整理 四、常见问题解答 五、智能家居文档 树状提纲 ————————————————————————————————...

百度开发者中心
2019/03/29
96
0
008. 并发容器类 Map

1. JDK 源码学习方法 1. 演绎推导法 示例:因果推理。 因为 JAVA 中只提供了 BIO 和 NIO 两种方式,所以一切框架中,涉及到网络处理的,都可以用这两个知识点去探究原理。 2. 归纳总结法 示例...

紫穹
今天
114
0
如何使用sed替换换行符(\\ n)? - How can I replace a newline (\n) using sed?

问题: How can I replace a newline (" \\n ") with a space (" 如何将换行符(“ \\n ”)替换为空格(“ ") using the sed command? “)使用sed命令? I unsuccessfully tried: 我尝试失......

javail
今天
147
0
OSChina 周一乱弹 —— 原来这叫分别焦虑

Osc乱弹歌单(2020)请戳(这里) 【今日歌曲】 @薛定谔的兄弟 :分享洛神有语创建的歌单「我喜欢的音乐」: 《A Careful Tearing》- Levi Patel 手机党少年们想听歌,请使劲儿戳(这里) 清明...

小小编辑
今天
236
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部