Package org.springframework.context

Examples of org.springframework.context.Lifecycle


        }
    }

    public static void main(String[] args) throws IOException {
        SpringSupport support = new SpringSupport();
        Lifecycle lifecycle = null;

        support.getBean("ConfigurationContext", ConfigurationFactory.class).configure(args);
        lifecycle = support.getBean("LifeCycle", Lifecycle.class);

        lifecycle.start();
        lifecycle.stop();
    }
View Full Code Here


  private void startBeans(boolean autoStartupOnly) {
    Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
    Map<Integer, LifecycleGroup> phases = new HashMap<Integer, LifecycleGroup>();
    for (Map.Entry<String, ? extends Lifecycle> entry : lifecycleBeans.entrySet()) {
      Lifecycle bean = entry.getValue();
      if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
        int phase = getPhase(bean);
        LifecycleGroup group = phases.get(phase);
        if (group == null) {
          group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
View Full Code Here

   * making sure that any beans that it depends on are started first.
   * @param lifecycleBeans 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 && !this.equals(bean)) {
      String[] dependenciesForBean = this.beanFactory.getDependenciesForBean(beanName);
      for (String dependency : dependenciesForBean) {
        doStart(lifecycleBeans, dependency, autoStartupOnly);
      }
      if (!bean.isRunning() &&
          (!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
        if (logger.isDebugEnabled()) {
          logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass() + "]");
        }
        try {
          bean.start();
        }
        catch (Throwable ex) {
          throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
        }
        if (logger.isDebugEnabled()) {
View Full Code Here

  private void stopBeans() {
    Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
    Map<Integer, LifecycleGroup> phases = new HashMap<Integer, LifecycleGroup>();
    for (Map.Entry<String, Lifecycle> entry : lifecycleBeans.entrySet()) {
      Lifecycle bean = entry.getValue();
      int shutdownOrder = getPhase(bean);
      LifecycleGroup group = phases.get(shutdownOrder);
      if (group == null) {
        group = new LifecycleGroup(shutdownOrder, this.timeoutPerShutdownPhase, lifecycleBeans, false);
        phases.put(shutdownOrder, group);
View Full Code Here

   * @param beanName the name of the bean to stop
   */
  private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName,
      final CountDownLatch latch, final Set<String> countDownBeanNames) {

    Lifecycle bean = lifecycleBeans.remove(beanName);
    if (bean != null) {
      String[] dependentBeans = this.beanFactory.getDependentBeans(beanName);
      for (String dependentBean : dependentBeans) {
        doStop(lifecycleBeans, dependentBean, latch, countDownBeanNames);
      }
      try {
        if (bean.isRunning()) {
          if (bean instanceof SmartLifecycle) {
            if (logger.isDebugEnabled()) {
              logger.debug("Asking bean '" + beanName + "' of type [" + bean.getClass() + "] to stop");
            }
            countDownBeanNames.add(beanName);
            ((SmartLifecycle) bean).stop(new Runnable() {
              public void run() {
                latch.countDown();
                countDownBeanNames.remove(beanName);
                if (logger.isDebugEnabled()) {
                  logger.debug("Bean '" + beanName + "' completed its stop procedure");
                }
              }
            });
          }
          else {
            if (logger.isDebugEnabled()) {
              logger.debug("Stopping bean '" + beanName + "' of type [" + bean.getClass() + "]");
            }
            bean.stop();
            if (logger.isDebugEnabled()) {
              logger.debug("Successfully stopped bean '" + beanName + "'");
            }
          }
        }
View Full Code Here

      boolean isFactoryBean = this.beanFactory.isFactoryBean(beanNameToRegister);
      String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
      if ((this.beanFactory.containsSingleton(beanNameToRegister) &&
          (!isFactoryBean || Lifecycle.class.isAssignableFrom(this.beanFactory.getType(beanNameToCheck)))) ||
          SmartLifecycle.class.isAssignableFrom(this.beanFactory.getType(beanNameToCheck))) {
        Lifecycle bean = this.beanFactory.getBean(beanNameToCheck, Lifecycle.class);
        if (bean != this) {
          beans.put(beanNameToRegister, bean);
        }
      }
    }
View Full Code Here

  }

  public boolean isRunning() {
    Iterator it = getLifecycleBeans().values().iterator();
    while (it.hasNext()) {
      Lifecycle lifecycle = (Lifecycle) it.next();
      if (!lifecycle.isRunning()) {
        return false;
      }
    }
    return true;
  }
View Full Code Here

   * making sure that any beans that it depends on are started first.
   * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
   * @param beanName the name of the bean to start
   */
  private void doStart(Map lifecycleBeans, String beanName) {
    Lifecycle bean = (Lifecycle) lifecycleBeans.get(beanName);
    if (bean != null) {
      String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
      for (int i = 0; i < dependenciesForBean.length; i++) {
        doStart(lifecycleBeans, dependenciesForBean[i]);
      }
      if (!bean.isRunning()) {
        bean.start();
      }
      lifecycleBeans.remove(beanName);
    }
  }
View Full Code Here

   * making sure that any beans that depends on it are stopped first.
   * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
   * @param beanName the name of the bean to stop
   */
  private void doStop(Map lifecycleBeans, String beanName) {
    Lifecycle bean = (Lifecycle) lifecycleBeans.get(beanName);
    if (bean != null) {
      String[] dependentBeans = getBeanFactory().getDependentBeans(beanName);
      for (int i = 0; i < dependentBeans.length; i++) {
        doStop(lifecycleBeans, dependentBeans[i]);
      }
      if (bean.isRunning()) {
        bean.stop();
      }
      lifecycleBeans.remove(beanName);
    }
  }
View Full Code Here

  }

  public boolean isRunning() {
    Iterator it = getLifecycleBeans().values().iterator();
    while (it.hasNext()) {
      Lifecycle lifecycle = (Lifecycle) it.next();
      if (!lifecycle.isRunning()) {
        return false;
      }
    }
    return true;
  }
View Full Code Here

TOP

Related Classes of org.springframework.context.Lifecycle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.