org.springframework.context.Lifecycle类的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(179)

本文整理了Java中org.springframework.context.Lifecycle类的一些代码示例,展示了Lifecycle类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lifecycle类的具体详情如下:
包路径:org.springframework.context.Lifecycle
类名称:Lifecycle

Lifecycle介绍

[英]A common interface defining methods for start/stop lifecycle control. The typical use case for this is to control asynchronous processing. NOTE: This interface does not imply specific auto-startup semantics. Consider implementing SmartLifecycle for that purpose.

Can be implemented by both components (typically a Spring bean defined in a Spring context) and containers (typically a Spring ApplicationContextitself). Containers will propagate start/stop signals to all components that apply within each container, e.g. for a stop/restart scenario at runtime.

Can be used for direct invocations or for management operations via JMX. In the latter case, the org.springframework.jmx.export.MBeanExporterwill typically be defined with an org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler, restricting the visibility of activity-controlled components to the Lifecycle interface.

Note that the present Lifecycle interface is only supported on top-level singleton beans. On any other component, the Lifecycleinterface will remain undetected and hence ignored. Also, note that the extended SmartLifecycle interface provides sophisticated integration with the application context's startup and shutdown phases.
[中]定义启动/停止生命周期控制方法的通用接口。这方面的典型用例是控制异步处理。注意:此接口并不暗示特定的自动启动语义。考虑为此实现SmartLifecycle。
可以由组件(通常是在Spring上下文中定义的SpringBean)和容器(通常是SpringApplicationContext本身)实现。容器将向每个容器中应用的所有组件传播启动/停止信号,例如,对于运行时的停止/重新启动场景。
可用于直接调用或通过JMX进行管理操作。在后一种情况下,组织。springframework。jmx。出口MBeanExporter通常使用组织定义。springframework。jmx。出口汇编程序。InterfaceBasedMBeanInfoAssembler,将活动控制组件的可见性限制在生命周期接口上。
请注意,当前的生命周期接口仅在顶级单例bean上受支持。在任何其他组件上,Lifecycleinterface都不会被检测到,因此会被忽略。另外,请注意,扩展的SmartLifecycle界面提供了与应用程序上下文的启动和关闭阶段的复杂集成。

代码示例

代码示例来源:origin: spring-projects/spring-framework

protected void doStop() {
  if (this.requestUpgradeStrategy instanceof Lifecycle) {
    ((Lifecycle) this.requestUpgradeStrategy).stop();
  }
}

代码示例来源:origin: spring-projects/spring-framework

protected void doStart() {
  if (this.requestUpgradeStrategy instanceof Lifecycle) {
    ((Lifecycle) this.requestUpgradeStrategy).start();
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void startInternal() {
  if (this.client instanceof Lifecycle && !((Lifecycle) this.client).isRunning()) {
    ((Lifecycle) this.client).start();
  }
  super.startInternal();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void stopInternal() throws Exception {
  if (this.client instanceof Lifecycle && ((Lifecycle) this.client).isRunning()) {
    ((Lifecycle) this.client).stop();
  }
  super.stopInternal();
}

代码示例来源:origin: spring-projects/spring-batch

private void waitForResults(Lifecycle lifecycle, int count, int maxTries) throws InterruptedException {
  lifecycle.start();
  int timeout = 0;
  while (processed.size() < count && timeout++ < maxTries) {
    Thread.sleep(10);
  }
  lifecycle.stop();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean isRunning() {
  if (this.webSocketClient instanceof Lifecycle) {
    return ((Lifecycle) this.webSocketClient).isRunning();
  }
  else {
    return this.running;
  }
}

代码示例来源:origin: spring-projects/spring-batch

private void waitForResults(Lifecycle lifecycle, int count, int maxTries) throws InterruptedException {
  lifecycle.start();
  int timeout = 0;
  while (processed.size() < count && timeout++ < maxTries) {
    Thread.sleep(5);
  }
  lifecycle.stop();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void stop() {
  if (isRunning()) {
    this.running = false;
    for (Transport transport : this.transports) {
      if (transport instanceof Lifecycle) {
        Lifecycle lifecycle = (Lifecycle) transport;
        if (lifecycle.isRunning()) {
          lifecycle.stop();
        }
      }
    }
  }
}

代码示例来源:origin: apache/incubator-dubbo

if (((Lifecycle) context).isRunning()) {
  level = Status.Level.OK;
} else {

代码示例来源:origin: spring-projects/spring-framework

@Override
public void stop() {
  if (isRunning()) {
    this.running = false;
    if (this.handshakeHandler instanceof Lifecycle) {
      ((Lifecycle) this.handshakeHandler).stop();
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void start() {
  if (!isRunning()) {
    this.running = true;
    if (this.handshakeHandler instanceof Lifecycle) {
      ((Lifecycle) this.handshakeHandler).start();
    }
  }
}

代码示例来源:origin: spring-projects/spring-batch

private void waitForResults(Lifecycle lifecycle, int count, int maxTries) throws InterruptedException {
  lifecycle.start();
  int timeout = 0;
  while (service.getProcessed().size() < count && timeout++ < maxTries) {
    Thread.sleep(5);
  }
  lifecycle.stop();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void start() {
  if (!isRunning()) {
    this.running = true;
    for (Transport transport : this.transports) {
      if (transport instanceof Lifecycle) {
        Lifecycle lifecycle = (Lifecycle) transport;
        if (!lifecycle.isRunning()) {
          lifecycle.start();
        }
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

if (bean.isRunning()) {
  if (bean instanceof SmartLifecycle) {
    if (logger.isTraceEnabled()) {
          bean.getClass().getName() + "]");
    bean.stop();
    if (logger.isDebugEnabled()) {
      logger.debug("Successfully stopped bean '" + beanName + "'");

代码示例来源:origin: apache/incubator-dubbo

if (((Lifecycle) context).isRunning()) {
  level = Status.Level.OK;
} else {

代码示例来源:origin: spring-projects/spring-framework

@Override
public void stop() {
  if (isRunning()) {
    this.running = false;
    if (this.handshakeHandler instanceof Lifecycle) {
      ((Lifecycle) this.handshakeHandler).stop();
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void start() {
  if (!isRunning()) {
    this.running = true;
    if (this.sockJsService instanceof Lifecycle) {
      ((Lifecycle) this.sockJsService).start();
    }
  }
}

代码示例来源:origin: spring-projects/spring-batch

private void waitForResults(Lifecycle lifecycle, int count, int maxTries) throws InterruptedException {
  lifecycle.start();
  int timeout = 0;
  while (service.getProcessed().size() < count && timeout++ < maxTries) {
    Thread.sleep(10);
  }
  lifecycle.stop();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
  Lifecycle bean = lifecycleBeans.remove(beanName);
  if (bean != null && bean != this) {
    String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
    for (String dependency : dependenciesForBean) {
      doStart(lifecycleBeans, dependency, autoStartupOnly);
    }
    if (!bean.isRunning() &&
        (!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
      if (logger.isTraceEnabled()) {
        logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
      }
      try {
        bean.start();
      }
      catch (Throwable ex) {
        throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
      }
      if (logger.isDebugEnabled()) {
        logger.debug("Successfully started bean '" + beanName + "'");
      }
    }
  }
}

代码示例来源:origin: org.springframework/spring-context

if (bean.isRunning()) {
  if (bean instanceof SmartLifecycle) {
    if (logger.isTraceEnabled()) {
          bean.getClass().getName() + "]");
    bean.stop();
    if (logger.isDebugEnabled()) {
      logger.debug("Successfully stopped bean '" + beanName + "'");

相关文章

微信公众号

最新文章

更多