@Override protected void doStart() throws Exception { if (_executor.isShutdown()) throw new IllegalStateException("This thread pool is not restartable"); for (int i = 0; i < _minThreads; ++i) _executor.prestartCoreThread(); _tryExecutor = new ReservedThreadExecutor(this, _reservedThreads); addBean(_tryExecutor); super.doStart(); }
@Override protected void doStop() throws Exception { super.doStop(); removeBean(_tryExecutor); _tryExecutor = TryExecutor.NO_TRY; _executor.shutdownNow(); _budget.reset(); }
/** * Dump this object to an Appendable with no indent. * @param out The appendable to dump to. * @throws IOException May be thrown by the Appendable */ public void dump(Appendable out) throws IOException { dump(out, ""); }
public void attach(ContainerLifeCycle container) { for (LifeCycle object : managedObjects) { container.addBean(object); } container.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() { @Override public void lifeCycleStarting(LifeCycle event) { LOGGER.debug("managed objects = {}", managedObjects); } }); for (LifeCycle.Listener listener : lifecycleListeners) { container.addLifeCycleListener(listener); } }
public void updateBean(Object oldBean, final Object newBean) { if (newBean!=oldBean) { if (oldBean!=null) removeBean(oldBean); if (newBean!=null) addBean(newBean); } }
/** * Adds a managed lifecycle. * <p>This is a convenience method that uses addBean(lifecycle,true) * and then ensures that the added bean is started iff this container * is running. Exception from nested calls to start are caught and * wrapped as RuntimeExceptions * @param lifecycle the managed lifecycle to add */ public void addManaged(LifeCycle lifecycle) { addBean(lifecycle,true); try { if (isRunning() && !lifecycle.isRunning()) start(lifecycle); } catch (RuntimeException | Error e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void dump(Appendable out, String indent) throws IOException { ContainerLifeCycle.dumpObject(out,this); ContainerLifeCycle.dump(out,indent,Arrays.asList(new String[]{"connections="+_connectionStats,"duration="+_connectionDurationStats,"in="+_messagesIn,"out="+_messagesOut})); }
public boolean addBean(Object o, Managed managed) if (o==null || contains(o)) return false; addEventListener((Container.Listener)o); unmanage(new_bean); break; manage(new_bean); if (isStarting() && _doStarted) start(l); if (isStarting()) unmanage(new_bean); else if (_doStarted) manage(new_bean); start(l); else if (isStarted()) unmanage(new_bean); else new_bean._managed=Managed.AUTO;
unmanage(bean); removeEventListener((Container.Listener)bean._bean); stop((LifeCycle)bean._bean);
@Override public Collection<Object> getBeans() { return getBeans(Object.class); }
private List<Connector> buildAdminConnectors(MetricRegistry metricRegistry, Server server) { // threadpool is shared between all the connectors, so it should be managed by the server instead of the // individual connectors final QueuedThreadPool threadPool = new InstrumentedQueuedThreadPool(metricRegistry, adminMaxThreads, adminMinThreads); threadPool.setName("dw-admin"); server.addBean(threadPool); final List<Connector> connectors = new ArrayList<>(); for (ConnectorFactory factory : adminConnectors) { final Connector connector = factory.build(server, metricRegistry, "admin", threadPool); if (connector instanceof ContainerLifeCycle) { ((ContainerLifeCycle) connector).unmanage(threadPool); } connectors.add(connector); } return connectors; }
ContainerLifeCycle containerLifeCycle = new ContainerLifeCycle(); environment.lifecycle().attach(containerLifeCycle); containerLifeCycle.start(); try { AllTablesReportGenerator generator = injector.getInstance(AllTablesReportGenerator.class); containerLifeCycle.stop();
/** * @param clazz the class of the beans * @param <T> the Bean type * @param beans the collection to add beans of the given class from the entire managed hierarchy */ protected <T> void getContainedBeans(Class<T> clazz, Collection<T> beans) { beans.addAll(getBeans(clazz)); for (Container c : getBeans(Container.class)) { Bean bean = getBean(c); if (bean!=null && bean.isManageable()) { if (c instanceof ContainerLifeCycle) ((ContainerLifeCycle)c).getContainedBeans(clazz, beans); else beans.addAll(c.getContainedBeans(clazz)); } } } }
@Override protected void run(final Environment environment, final Namespace namespace, final C configuration) throws Exception { // simulating managed objects lifecycle support container = new ContainerLifeCycle(); environment.lifecycle().attach(container); container.start(); }
public void setBeans(Collection<Object> beans) { for (Object bean : beans) addBean(bean); }
@Override public void destroy() { if (!isStopped()) throw new IllegalStateException("!STOPPED"); super.destroy(); }
/** * Stops the managed lifecycle beans in the reverse order they were added. */ @Override protected void doStop() throws Exception { _doStarted = false; super.doStop(); List<Bean> reverse = new ArrayList<>(_beans); Collections.reverse(reverse); MultiException mex = new MultiException(); for (Bean b : reverse) { if (b._managed==Managed.MANAGED && b._bean instanceof LifeCycle) { LifeCycle l = (LifeCycle)b._bean; try { stop(l); } catch (Throwable th) { mex.add(th); } } } mex.ifExceptionThrow(); }
private void manage(Bean bean) { if (bean._managed!=Managed.MANAGED) { bean._managed=Managed.MANAGED; if (bean._bean instanceof Container) { for (Container.Listener l:_listeners) { if (l instanceof InheritedListener) { if (bean._bean instanceof ContainerLifeCycle) ((ContainerLifeCycle)bean._bean).addBean(l,false); else ((Container)bean._bean).addBean(l); } } } if (bean._bean instanceof AbstractLifeCycle) { ((AbstractLifeCycle)bean._bean).setStopTimeout(getStopTimeout()); } } }