use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.
the class Spf4jTestLogRunListenerSingleton method testFinished.
@Override
public synchronized void testFinished(final Description description) {
LogCollection<ArrayDeque<LogRecord>> handler = collections.remove(description);
try (LogCollection<ArrayDeque<LogRecord>> h = handler) {
handleUncaughtExceptions(description, h.get());
}
ExecutionContext ctx = ctxts.remove(description);
ExecutionContext currentThreadContext = ExecutionContexts.current();
if (ctx == currentThreadContext) {
ctx.close();
List<AutoCloseable> closeables = (List<AutoCloseable>) ctx.get(AutoCloseable.class);
if (closeables != null) {
Exception ex = Closeables.closeAll(closeables);
if (ex != null) {
throw new IllegalStateException("cannot close " + closeables, ex);
}
}
} else {
throw new IllegalStateException("JUnit Threading model not as expected " + ctx + " != " + currentThreadContext);
}
}
use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.
the class TestLoggers method addConfig.
private void addConfig(final String category, final LogHandler handler, final HandlerRegistration reg) {
synchronized (sync) {
config = config.add(category, handler);
resetJulConfig();
ExecutionContext ctx = ExecutionContexts.current();
if (ctx != null) {
ctx.compute(AutoCloseable.class, (Class<AutoCloseable> k, ArrayList<AutoCloseable> v) -> {
if (v == null) {
ArrayList<AutoCloseable> res = new ArrayList();
res.add(reg);
return res;
} else {
v.add(reg);
return v;
}
});
}
}
}
use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.
the class JdbcTemplate method transactOnConnection.
@SuppressFBWarnings("BED_BOGUS_EXCEPTION_DECLARATION")
public <R> R transactOnConnection(final HandlerNano<Connection, R, SQLException> handler, final long timeout, final TimeUnit tu) throws SQLException, InterruptedException {
try (ExecutionContext ctx = ExecutionContexts.start(handler.toString(), timeout, tu)) {
return (R) retryPolicy.call(new Callable() {
@Override
public R call() throws SQLException {
try (Connection conn = dataSource.getConnection()) {
boolean autocomit = conn.getAutoCommit();
if (autocomit) {
conn.setAutoCommit(false);
}
try {
R result = handler.handle(conn, ctx.getDeadlineNanos());
conn.commit();
return result;
} catch (SQLException | RuntimeException ex) {
conn.rollback();
throw ex;
} finally {
if (autocomit) {
conn.setAutoCommit(true);
}
}
}
}
}, SQLException.class, ctx.getDeadlineNanos());
} catch (TimeoutException ex) {
throw new SQLTimeoutException(ex);
}
}
use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.
the class MonitorTest method main.
@SuppressFBWarnings("MDM_THREAD_YIELD")
public static void main(final String[] args) throws InterruptedException {
stopped = false;
try (ExecutionContext ctx = ExecutionContexts.start("main", 10, TimeUnit.MINUTES)) {
List<Thread> threads = new ArrayList<Thread>(20);
for (int i = 0; i < 20; i++) {
Thread t;
t = new Thread(new AbstractRunnable() {
@Override
public void doRun() throws InterruptedException {
try (ExecutionContext tctx = ExecutionContexts.start("testThread", ctx, 10, TimeUnit.MINUTES)) {
while (!stopped) {
double rnd = Math.random();
if (rnd < 0.33) {
doStuff1(rnd, 50);
} else if (rnd < 0.66) {
doStuff2(rnd);
} else {
doStuff3(rnd);
}
}
}
}
@SuppressFBWarnings("MDM_THREAD_YIELD")
private double doStuff3(final double rnd) throws InterruptedException {
Thread.sleep(1);
if (rnd > 0.8) {
doStuff2(rnd);
}
return rnd * Math.pow(2, 10000);
}
@SuppressFBWarnings("MDM_THREAD_YIELD")
private double doStuff2(final double prnd) throws InterruptedException {
double rnd = prnd;
Thread.sleep(1);
for (int j = 0; j < 10000; j++) {
rnd = rnd + rnd;
}
return rnd;
}
@SuppressFBWarnings("MDM_THREAD_YIELD")
private void doStuff1(final double rnd, final int depth) throws InterruptedException {
if (depth <= 0) {
Thread.sleep(10);
} else {
doStuff1(rnd, depth - 1);
}
}
}, "Thread" + i);
t.start();
threads.add(t);
}
Thread.sleep(5000);
stopped = true;
for (Thread t : threads) {
t.join(3000);
}
}
}
use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.
the class Spf4jTestLogRunListenerSingleton method testStarted.
@Override
public void testStarted(final Description description) throws Exception {
Test ta = description.getAnnotation(Test.class);
ExecutionContext ctx;
if (ta != null && ta.timeout() > 0) {
ctx = ExecutionContexts.start(description.getDisplayName(), ta.timeout(), TimeUnit.MILLISECONDS);
} else {
ctx = ExecutionContexts.start(description.getDisplayName());
}
CollectTrobleshootingLogs ca = description.getAnnotation(CollectTrobleshootingLogs.class);
Level mll = ca == null ? minLogLevel : ca.minLevel();
boolean clp = ca == null ? collectPrinted : ca.collectPrinted();
collections.put(description, TestLoggers.sys().collect(mll, maxDebugLogsCollected, clp));
ctxts.put(description, ctx);
super.testStarted(description);
}
Aggregations