Search in sources :

Example 76 with ExecutionException

use of java.util.concurrent.ExecutionException in project crate by crate.

the class ListenableBatchConsumerTest method testErrorCaseHandling.

@Test
public void testErrorCaseHandling() throws Exception {
    TestingBatchConsumer consumer = new TestingBatchConsumer();
    ListenableBatchConsumer listenableBatchConsumer = new ListenableBatchConsumer(consumer);
    listenableBatchConsumer.accept(null, new IllegalStateException("dummy"));
    // both the delegate consumer must receive the error and also the listenableBatchConsumers future must trigger
    try {
        consumer.getResult();
        fail("should have raised an exception");
    } catch (IllegalStateException e) {
    // expected
    }
    try {
        listenableBatchConsumer.completionFuture().get(10, TimeUnit.SECONDS);
        fail("should have raised an exception");
    } catch (ExecutionException e) {
    // expected
    }
}
Also used : TestingBatchConsumer(io.crate.testing.TestingBatchConsumer) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 77 with ExecutionException

use of java.util.concurrent.ExecutionException 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 78 with ExecutionException

use of java.util.concurrent.ExecutionException in project cucumber-jvm by cucumber.

the class URLOutputStreamTest method can_http_put.

@Test
public void can_http_put() throws IOException, ExecutionException, InterruptedException {
    final BlockingQueue<String> data = new LinkedBlockingDeque<String>();
    Rest r = new Rest(webbit);
    r.PUT("/.cucumber/stepdefs.json", new HttpHandler() {

        @Override
        public void handleHttpRequest(HttpRequest req, HttpResponse res, HttpControl ctl) throws Exception {
            data.offer(req.body());
            res.end();
        }
    });
    Writer w = new UTF8OutputStreamWriter(new URLOutputStream(new URL(Utils.toURL("http://localhost:9873/.cucumber"), "stepdefs.json")));
    w.write("Hellesøy");
    w.flush();
    w.close();
    assertEquals("Hellesøy", data.poll(1000, TimeUnit.MILLISECONDS));
}
Also used : HttpRequest(org.webbitserver.HttpRequest) HttpHandler(org.webbitserver.HttpHandler) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HttpResponse(org.webbitserver.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) Rest(org.webbitserver.rest.Rest) HttpControl(org.webbitserver.HttpControl) Writer(java.io.Writer) Test(org.junit.Test)

Example 79 with ExecutionException

use of java.util.concurrent.ExecutionException in project weave by continuuity.

the class ZKServiceDecoratorTest method testStateTransition.

@Test
public void testStateTransition() throws InterruptedException, ExecutionException, TimeoutException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    try {
        final String namespace = Joiner.on('/').join("/weave", RunIds.generate(), "runnables", "Runner1");
        final ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClient.startAndWait();
        zkClient.create(namespace, null, CreateMode.PERSISTENT).get();
        try {
            JsonObject content = new JsonObject();
            content.addProperty("containerId", "container-123");
            content.addProperty("host", "localhost");
            RunId runId = RunIds.generate();
            final Semaphore semaphore = new Semaphore(0);
            ZKServiceDecorator service = new ZKServiceDecorator(ZKClients.namespace(zkClient, namespace), runId, Suppliers.ofInstance(content), new AbstractIdleService() {

                @Override
                protected void startUp() throws Exception {
                    Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to start");
                }

                @Override
                protected void shutDown() throws Exception {
                    Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to stop");
                }
            });
            final String runnablePath = namespace + "/" + runId.getId();
            final AtomicReference<String> stateMatch = new AtomicReference<String>("STARTING");
            watchDataChange(zkClient, runnablePath + "/state", semaphore, stateMatch);
            Assert.assertEquals(Service.State.RUNNING, service.start().get(5, TimeUnit.SECONDS));
            stateMatch.set("STOPPING");
            Assert.assertEquals(Service.State.TERMINATED, service.stop().get(5, TimeUnit.SECONDS));
        } finally {
            zkClient.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : ZKServiceDecorator(com.continuuity.weave.internal.ZKServiceDecorator) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) JsonObject(com.google.gson.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) RunId(com.continuuity.weave.api.RunId) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 80 with ExecutionException

use of java.util.concurrent.ExecutionException in project weave by continuuity.

the class ServicesTest method testCompletion.

@Test
public void testCompletion() throws ExecutionException, InterruptedException {
    Service service = new DummyService("s1", new AtomicBoolean());
    ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);
    service.start();
    service.stop();
    completion.get();
    AtomicBoolean transiting = new AtomicBoolean();
    service = new DummyService("s2", transiting);
    completion = Services.getCompletionFuture(service);
    service.startAndWait();
    transiting.set(true);
    service.stop();
    try {
        completion.get();
        Assert.assertTrue(false);
    } catch (ExecutionException e) {
    // Expected
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) Service(com.google.common.util.concurrent.Service) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)1341 IOException (java.io.IOException)367 Test (org.junit.Test)335 TimeoutException (java.util.concurrent.TimeoutException)258 ArrayList (java.util.ArrayList)237 Future (java.util.concurrent.Future)218 ExecutorService (java.util.concurrent.ExecutorService)152 CountDownLatch (java.util.concurrent.CountDownLatch)103 List (java.util.List)98 CancellationException (java.util.concurrent.CancellationException)98 Callable (java.util.concurrent.Callable)97 Test (org.testng.annotations.Test)78 HashMap (java.util.HashMap)69 Map (java.util.Map)65 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)64 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)63 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)56 ParallelTest (com.hazelcast.test.annotation.ParallelTest)47 QuickTest (com.hazelcast.test.annotation.QuickTest)47 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)46