Search in sources :

Example 6 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class ProfiledExecutionContextFactory method startThreadRoot.

@Override
public ExecutionContext startThreadRoot(final String name, final ExecutionContext parent, final long startTimeNanos, final long deadlineNanos, final Runnable onClose) {
    Thread currentThread = Thread.currentThread();
    ExecutionContext ctx = wrapped.startThreadRoot(name, parent, startTimeNanos, deadlineNanos, () -> {
        currentContexts.remove(currentThread);
        onClose.run();
    });
    currentContexts.put(currentThread, ctx);
    return ctx;
}
Also used : ExecutionContext(org.spf4j.base.ExecutionContext)

Example 7 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class TracingExecutionContextStackCollector method sample.

@Override
public void sample() {
    Iterable<Map.Entry<Thread, ExecutionContext>> currentThreads = execCtxSupplier.get();
    int i = 0;
    for (Map.Entry<Thread, ExecutionContext> entry : currentThreads) {
        requestFor[i] = entry.getKey();
        contexts[i++] = entry.getValue();
        if (i >= requestFor.length) {
            break;
        }
    }
    Arrays.fill(requestFor, i, requestFor.length, null);
    StackTraceElement[][] stackTraces = Threads.getStackTraces(requestFor);
    for (int j = 0; j < i; j++) {
        StackTraceElement[] stackTrace = stackTraces[j];
        ExecutionContext context = contexts[j];
        StackCollector c = collections.computeIfAbsent(context.getName(), (k) -> new StackCollectorImpl());
        if (stackTrace != null && stackTrace.length > 0) {
            c.collect(stackTrace);
            context.compute("TSS", (String k, SampleNode v) -> {
                if (v == null) {
                    return SampleNode.createSampleNode(stackTrace);
                } else {
                    SampleNode.addToSampleNode(v, stackTrace);
                    return v;
                }
            });
        } else {
            c.collect(new StackTraceElement[] { new StackTraceElement("Thread", requestFor[j].getName(), "", 0) });
        }
    }
}
Also used : ExecutionContext(org.spf4j.base.ExecutionContext) Map(java.util.Map) TMap(gnu.trove.map.TMap) THashMap(gnu.trove.map.hash.THashMap)

Example 8 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class RetryAspect method retriedMethod.

@Around(value = "execution(@org.spf4j.annotations.Retry * *(..)) && @annotation(annot)", argNames = "pjp,annot")
public Object retriedMethod(final ProceedingJoinPoint pjp, final Retry annot) throws Throwable {
    try (ExecutionContext ctx = ExecutionContexts.start(pjp.toShortString(), annot.timeout(), annot.units())) {
        Callable c = () -> {
            try {
                return pjp.proceed();
            } catch (Exception | Error e) {
                throw e;
            } catch (Throwable ex) {
                throw new UncheckedExecutionException(ex);
            }
        };
        String retryPolicyName = annot.retryPolicyName();
        if ("".equals(retryPolicyName)) {
            return RetryPolicy.defaultPolicy().call(c, Exception.class, ctx.getDeadlineNanos());
        } else {
            return POLICIES.get(retryPolicyName).call(c, Exception.class, ctx.getDeadlineNanos());
        }
    }
}
Also used : ExecutionContext(org.spf4j.base.ExecutionContext) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Callable(java.util.concurrent.Callable) Around(org.aspectj.lang.annotation.Around)

Example 9 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class ObjectPoolBuilderTest method testBuildDisposeTimeout.

@Test(expected = IllegalStateException.class)
@SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
public void testBuildDisposeTimeout() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException {
    RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(10, new ExpensiveTestObjectFactory()).build();
    LOG.debug("pool = {}", pool);
    pool.get();
    pool.get();
    LOG.debug("pool = {}", pool);
    try (ExecutionContext start = ExecutionContexts.start(1, TimeUnit.SECONDS)) {
        pool.dispose();
        pool.get();
        LOG.debug("pool = {}", pool);
    }
}
Also used : ExecutionContext(org.spf4j.base.ExecutionContext) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ExecutionContext (org.spf4j.base.ExecutionContext)9 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2 Test (org.junit.Test)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)1 TMap (gnu.trove.map.TMap)1 THashMap (gnu.trove.map.hash.THashMap)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 ArrayDeque (java.util.ArrayDeque)1 List (java.util.List)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1 Around (org.aspectj.lang.annotation.Around)1 AbstractRunnable (org.spf4j.base.AbstractRunnable)1 CollectTrobleshootingLogs (org.spf4j.test.log.CollectTrobleshootingLogs)1 Level (org.spf4j.test.log.Level)1