Search in sources :

Example 56 with Future

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;
}
Also used : WxCpXmlOutMessage(me.chanjar.weixin.cp.bean.WxCpXmlOutMessage) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 57 with Future

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;
        }
    };
}
Also used : CachedData(net.spy.memcached.CachedData) Future(java.util.concurrent.Future) OperationFuture(net.spy.memcached.internal.OperationFuture) BulkFuture(net.spy.memcached.internal.BulkFuture) TimeUnit(java.util.concurrent.TimeUnit)

Example 58 with Future

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());
    }
}
Also used : ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 59 with Future

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);
}
Also used : Future(java.util.concurrent.Future) Interval(org.joda.time.Interval) Pair(io.druid.java.util.common.Pair) Test(org.junit.Test)

Example 60 with 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);
}
Also used : Future(java.util.concurrent.Future) Interval(org.joda.time.Interval) Pair(io.druid.java.util.common.Pair) Test(org.junit.Test)

Aggregations

Future (java.util.concurrent.Future)1138 ArrayList (java.util.ArrayList)479 ExecutorService (java.util.concurrent.ExecutorService)445 Test (org.junit.Test)413 ExecutionException (java.util.concurrent.ExecutionException)264 Callable (java.util.concurrent.Callable)206 IOException (java.io.IOException)177 ParallelTest (com.hazelcast.test.annotation.ParallelTest)148 QuickTest (com.hazelcast.test.annotation.QuickTest)148 HashMap (java.util.HashMap)92 List (java.util.List)84 CountDownLatch (java.util.concurrent.CountDownLatch)71 LinkedList (java.util.LinkedList)67 TimeoutException (java.util.concurrent.TimeoutException)63 HashSet (java.util.HashSet)62 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)59 Map (java.util.Map)58 ICompletableFuture (com.hazelcast.core.ICompletableFuture)57 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 File (java.io.File)46