use of java.util.concurrent.Future in project weixin-java-tools by chanjarster.
the class WxCpMessageRouter method route.
/**
* 处理微信消息
* @param wxMessage
*/
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
if (isDuplicateMessage(wxMessage)) {
// 如果是重复消息,那么就不做处理
return null;
}
final List<WxCpMessageRouterRule> matchRules = new ArrayList<WxCpMessageRouterRule>();
// 收集匹配的规则
for (final WxCpMessageRouterRule rule : rules) {
if (rule.test(wxMessage)) {
matchRules.add(rule);
if (!rule.isReEnter()) {
break;
}
}
}
if (matchRules.size() == 0) {
return null;
}
WxCpXmlOutMessage res = null;
final List<Future> futures = new ArrayList<Future>();
for (final WxCpMessageRouterRule rule : matchRules) {
// 返回最后一个非异步的rule的执行结果
if (rule.isAsync()) {
futures.add(executorService.submit(new Runnable() {
public void run() {
rule.service(wxMessage, wxCpService, sessionManager, exceptionHandler);
}
}));
} else {
res = rule.service(wxMessage, wxCpService, sessionManager, exceptionHandler);
// 在同步操作结束,session访问结束
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
sessionEndAccess(wxMessage);
}
}
if (futures.size() > 0) {
executorService.submit(new Runnable() {
@Override
public void run() {
for (Future future : futures) {
try {
future.get();
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
// 异步操作结束,session访问结束
sessionEndAccess(wxMessage);
} catch (InterruptedException e) {
log.error("Error happened when wait task finish", e);
} catch (ExecutionException e) {
log.error("Error happened when wait task finish", e);
}
}
}
});
}
return res;
}
use of java.util.concurrent.Future in project druid by druid-io.
the class MockMemcachedClient method asyncGet.
@Override
public <T> Future<T> asyncGet(String key, final Transcoder<T> tc) {
CachedData data = theMap.get(key);
final T theValue = data != null ? tc.decode(data) : null;
return new Future<T>() {
@Override
public boolean cancel(boolean b) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return true;
}
@Override
public T get() throws InterruptedException, ExecutionException {
return theValue;
}
@Override
public T get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
return theValue;
}
};
}
use of java.util.concurrent.Future in project druid by druid-io.
the class BytesBoundedLinkedQueueTest method testConcurrentOperations.
// @Test
public void testConcurrentOperations() throws Exception {
final BlockingQueue<TestObject> q = getQueue(Integer.MAX_VALUE);
long duration = TimeUnit.SECONDS.toMillis(10);
ExecutorService executor = Executors.newCachedThreadPool();
final AtomicBoolean stopTest = new AtomicBoolean(false);
List<Future> futures = new ArrayList<>();
for (int i = 0; i < 5; i++) {
futures.add(executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() {
while (!stopTest.get()) {
q.add(new TestObject(1));
q.add(new TestObject(2));
}
return true;
}
}));
}
for (int i = 0; i < 10; i++) {
futures.add(executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws InterruptedException {
while (!stopTest.get()) {
q.poll(100, TimeUnit.MILLISECONDS);
q.offer(new TestObject(2));
}
return true;
}
}));
}
for (int i = 0; i < 5; i++) {
futures.add(executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() {
while (!stopTest.get()) {
System.out.println("drained elements : " + q.drainTo(new ArrayList<TestObject>(), Integer.MAX_VALUE));
}
return true;
}
}));
}
Thread.sleep(duration);
stopTest.set(true);
for (Future<Boolean> future : futures) {
Assert.assertTrue(future.get());
}
}
use of java.util.concurrent.Future in project druid by druid-io.
the class ServerManagerTest method testDelete1.
@Test
public void testDelete1() throws Exception {
final String dataSouce = "test";
final Interval interval = new Interval("2011-04-01/2011-04-02");
Future future = assertQueryable(Granularities.DAY, dataSouce, interval, ImmutableList.<Pair<String, Interval>>of(new Pair<String, Interval>("2", interval)));
waitForTestVerificationAndCleanup(future);
dropQueryable(dataSouce, "2", interval);
future = assertQueryable(Granularities.DAY, dataSouce, interval, ImmutableList.<Pair<String, Interval>>of(new Pair<String, Interval>("1", interval)));
waitForTestVerificationAndCleanup(future);
}
use of java.util.concurrent.Future in project druid by druid-io.
the class ServerManagerTest method testSimpleGet.
@Test
public void testSimpleGet() {
Future future = assertQueryable(Granularities.DAY, "test", new Interval("P1d/2011-04-01"), ImmutableList.<Pair<String, Interval>>of(new Pair<String, Interval>("1", new Interval("P1d/2011-04-01"))));
waitForTestVerificationAndCleanup(future);
future = assertQueryable(Granularities.DAY, "test", new Interval("P2d/2011-04-02"), ImmutableList.<Pair<String, Interval>>of(new Pair<String, Interval>("1", new Interval("P1d/2011-04-01")), new Pair<String, Interval>("2", new Interval("P1d/2011-04-02"))));
waitForTestVerificationAndCleanup(future);
}
Aggregations