Package org.springframework.context

Examples of org.springframework.context.Lifecycle


   * 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 void start() {
    Iterator it = getLifecycleBeans().iterator();
    while (it.hasNext()) {
      Lifecycle lifecycle = (Lifecycle) it.next();
      if (!lifecycle.isRunning()) {
        lifecycle.start();
      }
    }
  }
View Full Code Here

  }

  public void stop() {
    Iterator it = getLifecycleBeans().iterator();
    while (it.hasNext()) {
      Lifecycle lifecycle = (Lifecycle) it.next();
      if (lifecycle.isRunning()) {
        lifecycle.stop();
      }
    }
  }
View Full Code Here

  }

  public boolean isRunning() {
    Iterator it = getLifecycleBeans().iterator();
    while (it.hasNext()) {
      Lifecycle lifecycle = (Lifecycle) it.next();
      if (!lifecycle.isRunning()) {
        return false;
      }
    }
    return true;
  }
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

  }

  @Test
  public void singleLifecycleShutdown() throws Exception {
    CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<Lifecycle>();
    Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
    StaticApplicationContext context = new StaticApplicationContext();
    context.getBeanFactory().registerSingleton("bean", bean);
    context.refresh();
    assertFalse(bean.isRunning());
    bean.start();
    assertTrue(bean.isRunning());
    context.stop();
    assertEquals(1, stoppedBeans.size());
    assertFalse(bean.isRunning());
    assertEquals(bean, stoppedBeans.get(0));
  }
View Full Code Here

  }

  @Test
  public void mixedShutdown() throws Exception {
    CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<Lifecycle>();
    Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
    Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
    Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
    Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
    Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
    Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
    Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
    StaticApplicationContext context = new StaticApplicationContext();
    context.getBeanFactory().registerSingleton("bean1", bean1);
    context.getBeanFactory().registerSingleton("bean2", bean2);
    context.getBeanFactory().registerSingleton("bean3", bean3);
    context.getBeanFactory().registerSingleton("bean4", bean4);
    context.getBeanFactory().registerSingleton("bean5", bean5);
    context.getBeanFactory().registerSingleton("bean6", bean6);
    context.getBeanFactory().registerSingleton("bean7", bean7);
    context.refresh();
    assertTrue(bean2.isRunning());
    assertTrue(bean3.isRunning());
    assertTrue(bean5.isRunning());
    assertTrue(bean6.isRunning());
    assertTrue(bean7.isRunning());
    assertFalse(bean1.isRunning());
    assertFalse(bean4.isRunning());
    bean1.start();
    bean4.start();
    assertTrue(bean1.isRunning());
    assertTrue(bean4.isRunning());
    context.stop();
    assertFalse(bean1.isRunning());
    assertFalse(bean2.isRunning());
    assertFalse(bean3.isRunning());
    assertFalse(bean4.isRunning());
    assertFalse(bean5.isRunning());
    assertFalse(bean6.isRunning());
    assertFalse(bean7.isRunning());
    assertEquals(7, stoppedBeans.size());
    assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
    assertEquals(500, getPhase(stoppedBeans.get(1)));
    assertEquals(1, getPhase(stoppedBeans.get(2)));
    assertEquals(0, getPhase(stoppedBeans.get(3)));
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.