Search in sources :

Example 6 with DaemonThreadFactory

use of org.apache.openejb.util.DaemonThreadFactory in project tomee by apache.

the class SessionManager method initEviction.

public void initEviction() {
    if (!"true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.http.eviction", "true"))) {
        return;
    }
    final Duration duration = new Duration(SystemInstance.get().getProperty("openejb.http.eviction.duration", "1 minute"));
    es = Executors.newScheduledThreadPool(1, new DaemonThreadFactory(SessionManager.class));
    es.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            for (final SessionWrapper data : new ArrayList<>(sessions.values())) {
                final HttpSession session = data.session;
                if (session.getMaxInactiveInterval() > 0 && session.getLastAccessedTime() + TimeUnit.SECONDS.toMillis(session.getMaxInactiveInterval()) < System.currentTimeMillis()) {
                    doDestroy(data);
                    sessions.remove(data.session.getId());
                }
            }
        }
    }, duration.getTime(), duration.getTime(), duration.getUnit());
}
Also used : DaemonThreadFactory(org.apache.openejb.util.DaemonThreadFactory) HttpSession(org.apache.openejb.server.httpd.HttpSession) Duration(org.apache.openejb.util.Duration)

Example 7 with DaemonThreadFactory

use of org.apache.openejb.util.DaemonThreadFactory in project tomee by apache.

the class ThreadStackRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    if (System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows")) {
        return base;
    }
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final ScheduledExecutorService ses = Executors.newScheduledThreadPool(1, new DaemonThreadFactory(ThreadStackRule.class.getSimpleName() + "-"));
            final ScheduledFuture<?> task = ses.scheduleAtFixedRate(new Runnable() {

                @Override
                public void run() {
                    final RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
                    String pid = bean.getName();
                    if (pid.contains("@")) {
                        pid = pid.substring(0, pid.indexOf("@"));
                    }
                    try {
                        Pipe.pipe(Runtime.getRuntime().exec("kill -3 " + pid));
                    } catch (final Exception exception) {
                        exception.printStackTrace();
                    }
                }
            }, 2, 2, TimeUnit.MINUTES);
            try {
                base.evaluate();
            } finally {
                task.cancel(true);
                ses.shutdownNow();
            }
        }
    };
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Statement(org.junit.runners.model.Statement) DaemonThreadFactory(org.apache.openejb.util.DaemonThreadFactory) RuntimeMXBean(java.lang.management.RuntimeMXBean)

Aggregations

DaemonThreadFactory (org.apache.openejb.util.DaemonThreadFactory)7 ExecutorService (java.util.concurrent.ExecutorService)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 NamingException (javax.naming.NamingException)2 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)2 Duration (org.apache.openejb.util.Duration)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 PrintStream (java.io.PrintStream)1