org.apache.catalina.Lifecycle类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(14.6k)|赞(0)|评价(0)|浏览(197)

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

Lifecycle介绍

[英]Common interface for component life cycle methods. Catalina components may implement this interface (as well as the appropriate interface(s) for the functionality they support) in order to provide a consistent mechanism to start and stop the component.
The valid state transitions for components that support Lifecycleare:

start() 
----------------------------- 
|                           | 
| init()                    | 
NEW ->-- INITIALIZING        | 
| |           |              |     -------------------- STARTING_PREP -->- STARTING -->- STARTED -->---  | 
| |         |                                                  |         |  | 
| |         |                                                  |         |  | 
| |         |                                                  |         |  | 
| |destroy()|                                                  |         |  | 
| -->---------- STOPPING ------>----- STOPPED ---->------ 
|     |                                ^                  |  |  ^ 
|     |               stop()           |                  |  |  | 
|     |       --------------------------                  |  |  | 
|     |       |                                  auto     |  |  | 
|     |       |                  MUST_DESTROY------------ DESTROYING --------------------    \|/                         | 
|                                 DESTROYED                     | 
|                                                               | 
|                            stop()                             | 
--->------------------------------>------------------------------ 
Any state can transition to FAILED. 
Calling start() while a component is in states STARTING_PREP, STARTING or 
STARTED has no effect. 
Calling start() while a component is in state NEW will cause init() to be 
called immediately after the start() method is entered. 
Calling stop() while a component is in states STOPPING_PREP, STOPPING or 
STOPPED has no effect. 
Calling stop() while a component is in state NEW transitions the component 
to STOPPED. This is typically encountered when a component fails to start and 
does not start all its sub-components. When the component is stopped, it will 
try to stop all sub-components - even those it didn't start. 
MUST_STOP is used to indicate that the  
#stop() should be called on 
the component as soon as  
#start() exits. It is typically used when a 
component has failed to start. 
MUST_DESTROY is used to indicate that the  
#destroy() should be called on 
the component as soon as  
#stop() exits. It is typically used when a 
component is not designed to be restarted. 
Attempting any other transition will throw  
LifecycleException.

The LifecycleEvents fired during state changes are defined in the methods that trigger the changed. No LifecycleEvents are fired if the attempted transition is not valid. TODO: Not all components may transition from STOPPED to STARTING_PREP. These components should use MUST_DESTROY to signal this.
[中]组件生命周期方法的通用接口。Catalina组件可以实现该接口(以及它们支持的功能的适当接口),以便提供启动和停止组件的一致机制。
支持Lifecycleare的组件的有效状态转换:

start() 
----------------------------- 
|                           | 
| init()                    | 
NEW ->-- INITIALIZING        | 
| |           |              |     -------------------- STARTING_PREP -->- STARTING -->- STARTED -->---  | 
| |         |                                                  |         |  | 
| |         |                                                  |         |  | 
| |         |                                                  |         |  | 
| |destroy()|                                                  |         |  | 
| -->---------- STOPPING ------>----- STOPPED ---->------ 
|     |                                ^                  |  |  ^ 
|     |               stop()           |                  |  |  | 
|     |       --------------------------                  |  |  | 
|     |       |                                  auto     |  |  | 
|     |       |                  MUST_DESTROY------------ DESTROYING --------------------    \|/                         | 
|                                 DESTROYED                     | 
|                                                               | 
|                            stop()                             | 
--->------------------------------>------------------------------ 
Any state can transition to FAILED. 
Calling start() while a component is in states STARTING_PREP, STARTING or 
STARTED has no effect. 
Calling start() while a component is in state NEW will cause init() to be 
called immediately after the start() method is entered. 
Calling stop() while a component is in states STOPPING_PREP, STOPPING or 
STOPPED has no effect. 
Calling stop() while a component is in state NEW transitions the component 
to STOPPED. This is typically encountered when a component fails to start and 
does not start all its sub-components. When the component is stopped, it will 
try to stop all sub-components - even those it didn't start. 
MUST_STOP is used to indicate that the  
#stop() should be called on 
the component as soon as  
#start() exits. It is typically used when a 
component has failed to start. 
MUST_DESTROY is used to indicate that the  
#destroy() should be called on 
the component as soon as  
#stop() exits. It is typically used when a 
component is not designed to be restarted. 
Attempting any other transition will throw  
LifecycleException.

在状态更改期间激发的LifecycleEvents在触发更改的组件的方法中定义。如果尝试的转换无效,则不会激发LifecycleEvents。TODO:并非所有组件都可以从停止转换为启动准备。这些组件应使用必须销毁来发出此信号。

代码示例

代码示例来源:origin: org.jboss.web/jbossweb

/**
 * Prepare for the beginning of active use of the public methods of this
 * component.  This method should be called before any of the public
 * methods of this component are utilized.  It should also send a
 * LifecycleEvent of type START_EVENT to any registered listeners.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
public void start() throws LifecycleException {
  // Validate and update our current component state
  if (started) {
    return;
  }
  // Notify our interested LifecycleListeners
  lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
  lifecycle.fireLifecycleEvent(START_EVENT, null);
  started = true;
  // Start our defined Services
  synchronized (services) {
    for (int i = 0; i < services.length; i++) {
      if (services[i] instanceof Lifecycle)
        ((Lifecycle) services[i]).start();
    }
  }
  // Notify our interested LifecycleListeners
  lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
}

代码示例来源:origin: org.jboss.web/jbossweb

/**
 * Gracefully terminate the active use of the public methods of this
 * component.  This method should be the last one called on a given
 * instance of this component.  It should also send a LifecycleEvent
 * of type STOP_EVENT to any registered listeners.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that needs to be reported
 */
public void stop() throws LifecycleException {
  // Validate and update our current component state
  if (!started)
    return;
  // Notify our interested LifecycleListeners
  lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);
  lifecycle.fireLifecycleEvent(STOP_EVENT, null);
  started = false;
  // Stop our defined Services
  for (int i = 0; i < services.length; i++) {
    if (services[i] instanceof Lifecycle)
      ((Lifecycle) services[i]).stop();
  }
  // Notify our interested LifecycleListeners
  lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
}

代码示例来源:origin: org.jboss.mod_cluster/mod_cluster

private void addListeners(Server server)
{
 // Register ourself as a listener for child services
 for (Service service: server.findServices())
 {
   Container engine = service.getContainer();
   engine.addContainerListener(this);
   ((Lifecycle) engine).addLifecycleListener(this);
   for (Container host: engine.findChildren())
   {
    host.addContainerListener(this);
    
    for (Container context: host.findChildren())
    {
      ((Lifecycle) context).addLifecycleListener(this);
    }
   }
 }
}

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

((Lifecycle) oldManager).stop();
      ((Lifecycle) oldManager).destroy();
    } catch (LifecycleException e) {
      log.error(sm.getString("standardContext.setManager.stop"), e);
    manager.setContext(this);
  if (getState().isAvailable() && manager instanceof Lifecycle) {
    try {
      ((Lifecycle) manager).start();
    } catch (LifecycleException e) {
      log.error(sm.getString("standardContext.setManager.start"), e);
support.firePropertyChange("manager", oldManager, manager);

代码示例来源:origin: org.mobicents.arquillian.container/mss-tomcat-embedded-6

@Override
public synchronized void addEngine(Engine engine) {
  if( log.isDebugEnabled() )
    log.debug("Adding engine (" + engine.getInfo() + ")");
  // Add this Engine to our set of defined Engines
  Engine results[] = new Engine[engines.length + 1];
  for (int i = 0; i < engines.length; i++)
    results[i] = engines[i];
  results[engines.length] = engine;
  engines = results;
  // Start this Engine if necessary
  if (started && (engine instanceof Lifecycle)) {
    try {
      ((Lifecycle) engine).start();
    } catch (LifecycleException e) {
      log.error("Engine.start", e);
    }
  }
  service.setContainer(engine);
}

代码示例来源:origin: org.apache.geronimo.ext.tomcat/catalina

((Lifecycle) pipeline).getState().isAvailable()) {
((Lifecycle) pipeline).stop();
  result.get();
} catch (Exception e) {
  log.error(sm.getString("containerBase.threadedStopFailed"), e);
  fail = true;
((Lifecycle) resources).stop();
((Lifecycle) realm).stop();
((Lifecycle) cluster).stop();
  ((Lifecycle) manager).getState().isAvailable() ) {
((Lifecycle) manager).stop();
((Lifecycle) loader).stop();

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

/**
 * Start this component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void startInternal() throws LifecycleException {
  super.startInternal();
  if (store == null)
    log.error("No Store configured, persistence disabled");
  else if (store instanceof Lifecycle)
    ((Lifecycle)store).start();
  setState(LifecycleState.STARTING);
}

代码示例来源:origin: org.apache.geronimo.ext.tomcat/catalina

/**
 * Stop nested components ({@link Connector}s and {@link Engine}s) and
 * implement the requirements of
 * {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that needs to be reported
 */
@Override
protected void stopInternal() throws LifecycleException {
  if( log.isDebugEnabled() )
    log.debug("Stopping embedded server");
  setState(LifecycleState.STOPPING);
  // Stop our defined Connectors first
  for (int i = 0; i < connectors.length; i++) {
    ((Lifecycle) connectors[i]).stop();
  }
  // Stop our defined Engines second
  for (int i = 0; i < engines.length; i++) {
    engines[i].stop();
  }
}

代码示例来源:origin: jboss.web/jbossweb

Engine engine = (Engine) service.getContainer();
engine.addContainerListener(this);
((Lifecycle) engine).addLifecycleListener(this);
if (engine.getDefaultHost() != null) {
  mapper.setDefaultHostName(engine.getDefaultHost());
  host.addContainerListener(this);
  mapper.addHost(host.getName(), ((Host) host).findAliases(), host);
  for (Container context : host.findChildren()) {
    ((Lifecycle) context).addLifecycleListener(this);
Engine engine = (Engine) service.getContainer();
engine.removeContainerListener(this);
((Lifecycle) engine).removeLifecycleListener(this);
for (Container host : engine.findChildren()) {
  host.removeContainerListener(this);
  mapper.removeHost(host.getName());
  for (Container context : host.findChildren()) {
    ((Lifecycle) context).removeLifecycleListener(this);
    mapper.removeContext(host.getName(), context.getName());

代码示例来源:origin: org.mobicents.arquillian.container/mss-tomcat-embedded-6

@Override
public Context createContext(String path, String docBase) {
  if( log.isDebugEnabled() )
    log.debug("Creating context '" + path + "' with docBase '" +
          docBase + "'");
  context = new StandardContext();
  context.setDocBase(docBase);
  context.setPath(path);
  ContextConfig config = new ContextConfig();
  config.setCustomAuthenticators(authenticators);
  ((Lifecycle) context).addLifecycleListener(config);
  return (context);
}

代码示例来源:origin: org.mobicents.arquillian.container/mss-tomcat-embedded-6

@Override
public void start() throws LifecycleException {
  if( log.isInfoEnabled() )
    log.info("Starting tomcat server");
  // Validate the setup of our required system properties
  initDirs();
  // Initialize some naming specific properties
  initNaming();
  // Validate and update our current component state
  if (started)
    throw new LifecycleException
      (sm.getString("embedded.alreadyStarted"));
  lifecycle.fireLifecycleEvent(START_EVENT, null);
  started = true;
  service.initialize();
  // Start our defined Engines first
  for (int i = 0; i < engines.length; i++) {
    if (engines[i] instanceof Lifecycle)
      ((Lifecycle) engines[i]).start();
  }
  // Start our defined Connectors second
  Connector[] connectors = service.findConnectors();
  for (int i = 0; i < connectors.length; i++) {
    connectors[i].initialize();
    if (connectors[i] instanceof Lifecycle)
      ((Lifecycle) connectors[i]).start();
  }
}

代码示例来源:origin: org.jboss.web/jbossweb

private synchronized void addChildInternal(Container child) {
  if (child.getName() == null)
    throw MESSAGES.containerChildWithNullName();
  if (children.get(child.getName()) != null)
    throw MESSAGES.containerChildNameNotUnique(child.getName());
  child.setParent(this);  // May throw IAE
  children.put(child.getName(), child);
  // Start child
  if (started && startChildren && (child instanceof Lifecycle)) {
    boolean success = false;
    try {
      ((Lifecycle) child).start();
      success = true;
    } catch (LifecycleException e) {
      throw MESSAGES.containerChildStartFailed(child.getName(), e);
    } finally {
      if (!success) {
        children.remove(child.getName());
      }
    }
  }
  fireContainerEvent(ADD_CHILD_EVENT, child);
}

代码示例来源:origin: org.jboss.jbossas/jboss-as-tomcat

getContainer().getName().replaceAll("/", ""));
LifecycleListener[] listeners = lifecycle.findLifecycleListeners();
 lifecycle.removeLifecycleListener(listener);
lifecycle.addLifecycleListener(this);
 lifecycle.addLifecycleListener(listener);

代码示例来源:origin: org.apache.tomee/tomee-catalina

private void addTomEERealm(final Engine engine) {
  final Realm realm = engine.getRealm();
  if (realm != null && !(realm instanceof TomEERealm) && (engine.getParent() == null || (!realm.equals(engine.getParent().getRealm())))) {
    final Realm tomeeRealm = tomeeRealm(realm);
    engine.setRealm(tomeeRealm);
    if (LifecycleState.STARTING_PREP.equals(engine.getState())) {
      try {
        Lifecycle.class.cast(tomeeRealm).start();
      } catch (final LifecycleException e) {
        throw new IllegalStateException(e);
      }
    }
  }
}

代码示例来源:origin: org.jboss.web/jbossweb

((Lifecycle) context).addLifecycleListener(this);
  if (context.isStarted()) {
    addContext(context);
  ((Lifecycle) context).removeLifecycleListener(this);
  mapper.removeContext(container.getName(), context.getName());
} else if (container instanceof Engine) {

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

@Override
protected void destroyInternal() throws LifecycleException {
  Realm realm = getRealmInternal();
  if (realm instanceof Lifecycle) {
    ((Lifecycle) realm).destroy();
  }
  Cluster cluster = getClusterInternal();
  if (cluster instanceof Lifecycle) {
    ((Lifecycle) cluster).destroy();
  }
  // Stop the Valves in our pipeline (including the basic), if any
  if (pipeline instanceof Lifecycle) {
    ((Lifecycle) pipeline).destroy();
  }
  // Remove children now this container is being destroyed
  for (Container child : findChildren()) {
    removeChild(child);
  }
  // Required if the child is destroyed directly.
  if (parent != null) {
    parent.removeChild(this);
  }
  // If init fails, this may be null
  if (startStopExecutor != null) {
    startStopExecutor.shutdownNow();
  }
  super.destroyInternal();
}

代码示例来源:origin: org.jboss.mod_cluster/mod_cluster

private void removeListeners(Server server)
{
 // Unregister ourself as a listener to child components
 for (Service service: server.findServices())
 {
   Container engine = service.getContainer();
   engine.removeContainerListener(this);
   ((Lifecycle) engine).removeLifecycleListener(this);
      for (Container host: engine.findChildren())
   {
    host.removeContainerListener(this);
    
    for (Container context: host.findChildren())
    {
      ((Lifecycle) context).removeLifecycleListener(this);
    }
   }
 }      
}

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

@Override
protected void stopInternal() throws LifecycleException {
  if (sessionIdGenerator instanceof Lifecycle) {
    ((Lifecycle) sessionIdGenerator).stop();
  }
}

代码示例来源:origin: org.jboss.web/jbossweb

/**
 * Prepare for the beginning of active use of the public methods of this
 * component.  This method should be called after <code>configure()</code>,
 * and before any of the public methods of the component are utilized.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
public void start() throws LifecycleException {
  // Validate and update our current component state
  if (started)
    throw new LifecycleException
      (MESSAGES.valveAlreadyStarted());
  lifecycle.fireLifecycleEvent(START_EVENT, null);
  started = true;
  if (container instanceof Context) {
    ((Lifecycle) container).addLifecycleListener(this);
  }
  
}

代码示例来源:origin: org.apache.tomee/tomee-catalina

@Override
public void start() throws LifecycleException {
  if (delegate instanceof Lifecycle) {
    ((Lifecycle) delegate).start();
  }
}

相关文章