package org.camunda.bpm.unittest;
import org.camunda.bpm.engine.impl.interceptor.Command;
import org.camunda.bpm.engine.impl.interceptor.CommandInterceptor;
public class TimeoutInterceptor extends CommandInterceptor {
protected static final long TIMEOUT_MILLIS = 5 * 60 * 1000;
protected static ThreadLocal<Long> commandBeginTime = new ThreadLocal<Long>();
public <T> T execute(Command<T> cmd) {
boolean recordTime = commandBeginTime.get() == null;
if (recordTime) {
commandBeginTime.set(System.currentTimeMillis());
}
try {
return next.execute(cmd);
} finally {
if (recordTime) {
commandBeginTime.set(null);
}
}
}
public static void ensureThreadNotTimedOut() {
long currentTimeMillis = System.currentTimeMillis();
long startTime = commandBeginTime.get();
if (currentTimeMillis - startTime > TIMEOUT_MILLIS) {
throw new RuntimeException("timeout");
}
}
}