Java Code Examples for org.apache.openejb.loader.SystemInstance#removeComponent()

The following examples show how to use org.apache.openejb.loader.SystemInstance#removeComponent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LightweightWebAppBuilder.java    From tomee with Apache License 2.0 6 votes vote down vote up
private void switchServletContextIfNeeded(final ServletContext sc, final Runnable runnable) {
    if (sc == null) {
        runnable.run();
        return;
    }
    final SystemInstance systemInstance = SystemInstance.get();
    final ServletContext old = systemInstance.getComponent(ServletContext.class);
    systemInstance.setComponent(ServletContext.class, sc);
    try {
        runnable.run();
    } finally {
        if (old == null) {
            systemInstance.removeComponent(ServletContext.class);
        } else {
            systemInstance.setComponent(ServletContext.class, old);
        }
    }
}
 
Example 2
Source File: DefaultTimerThreadPoolAdapter.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void shutdown(final boolean waitForJobsToComplete) {
    if (threadPoolExecutorUsed) {
        final SystemInstance systemInstance = SystemInstance.get();
        final TimerExecutor te = systemInstance.getComponent(TimerExecutor.class);
        if (te != null) {
            if (te.executor == executor) {
                if (te.decr()) {
                    doShutdownExecutor(waitForJobsToComplete);
                    systemInstance.removeComponent(TimerExecutor.class);
                } else { // flush jobs, maybe not all dedicated to this threadpool if shared but shouldn't be an issue
                    final ThreadPoolExecutor tpe = ThreadPoolExecutor.class.cast(executor);
                    if (waitForJobsToComplete) {
                        final Collection<Runnable> jobs = new ArrayList<>();
                        tpe.getQueue().drainTo(jobs);
                        for (final Runnable r : jobs) {
                            try {
                                r.run();
                            } catch (final Exception e) {
                                logger.warning(e.getMessage(), e);
                            }
                        }
                    }
                }
            } else {
                doShutdownExecutor(waitForJobsToComplete);
            }
        } else {
            doShutdownExecutor(waitForJobsToComplete);
        }
    }
}