TOMEE-2301 - project creation Signed-off-by: brunobat <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/b3d8edcc Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/b3d8edcc Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/b3d8edcc Branch: refs/heads/master Commit: b3d8edcc845b4f025d7ba32c14770c28ff60c389 Parents: 4b760d3 Author: brunobat <[email protected]> Authored: Thu Nov 29 12:21:07 2018 +0000 Committer: brunobat <[email protected]> Committed: Wed Dec 5 15:27:04 2018 +0000 ---------------------------------------------------------------------- examples/executor/pom.xml | 68 +++++++++++++++++ .../org/superbiz/executor/AsyncBookService.java | 77 ++++++++++++++++++++ .../superbiz/executor/AsyncBookServiceTest.java | 73 +++++++++++++++++++ 3 files changed, 218 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/b3d8edcc/examples/executor/pom.xml ---------------------------------------------------------------------- diff --git a/examples/executor/pom.xml b/examples/executor/pom.xml new file mode 100644 index 0000000..08a6dd3 --- /dev/null +++ b/examples/executor/pom.xml @@ -0,0 +1,68 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <groupId>org.superbiz</groupId> + <artifactId>executor</artifactId> + <version>8.0.0-SNAPSHOT</version> + <packaging>jar</packaging> + <name>OpenEJB :: Examples :: Executor for concurrency utilities</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <javaee-api.version>8.0</javaee-api.version> + <junit.version>4.12</junit.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.tomee</groupId> + <artifactId>javaee-api</artifactId> + <version>${javaee-api.version}</version> + <scope>provided</scope> + </dependency> + <!--Tests--> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>${junit.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.tomee</groupId> + <artifactId>arquillian-tomee-remote</artifactId> + <version>${pom.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jboss.arquillian.junit</groupId> + <artifactId>arquillian-junit-container</artifactId> + <version>1.4.0.Final</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jboss.arquillian.container</groupId> + <artifactId>arquillian-container-test-api</artifactId> + <version>1.4.0.Final</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.7.1</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + </plugins> + </build> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tomee/blob/b3d8edcc/examples/executor/src/main/java/org/superbiz/executor/AsyncBookService.java ---------------------------------------------------------------------- diff --git a/examples/executor/src/main/java/org/superbiz/executor/AsyncBookService.java b/examples/executor/src/main/java/org/superbiz/executor/AsyncBookService.java new file mode 100644 index 0000000..1f17189 --- /dev/null +++ b/examples/executor/src/main/java/org/superbiz/executor/AsyncBookService.java @@ -0,0 +1,77 @@ +package org.superbiz.executor; + +import javax.annotation.Resource; +import javax.ejb.Asynchronous; +import javax.enterprise.concurrent.ManagedExecutorService; +import javax.enterprise.context.RequestScoped; +import java.io.IOException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; +import java.util.function.Supplier; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@RequestScoped +public class AsyncBookService { + + @Resource + private ManagedExecutorService executor; + + @Asynchronous + public Future<String> serviceA() { + CompletableFuture<String> future = new CompletableFuture<>(); + future.completeExceptionally(new IOException("Simulated error")); + return future; + } + + @Asynchronous + public CompletableFuture<Integer> serviceB() { + return CompletableFuture.supplyAsync(delayedSupplier(1, 100), executor) + .thenApply(i -> i + 1); + } + + @Asynchronous + public CompletableFuture<Integer> serviceB() { + return CompletableFuture.supplyAsync(delayedWithExceptionSupplier(100, new RuntimeException("test")), executor); + } + + private Supplier<Integer> delayedSupplier(final int value, + final int delayMs) { + return () -> { + try { + Thread.sleep(delayMs); + } catch (InterruptedException e) { + throw new RuntimeException("Problem while waiting"); + } + return value; + }; + } + + private CompletableFuture<Integer> delayedWithExceptionSupplier(final int delayMs, + final Throwable t) { + final CompletableFuture<Integer> future = new CompletableFuture<>(); + try { + Thread.sleep(delayMs); + future.completeExceptionally(t); + } catch (InterruptedException e) { + throw new RuntimeException("Problem while waiting"); + } + return future; + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/b3d8edcc/examples/executor/src/test/java/org/superbiz/executor/AsyncBookServiceTest.java ---------------------------------------------------------------------- diff --git a/examples/executor/src/test/java/org/superbiz/executor/AsyncBookServiceTest.java b/examples/executor/src/test/java/org/superbiz/executor/AsyncBookServiceTest.java new file mode 100644 index 0000000..e567da4 --- /dev/null +++ b/examples/executor/src/test/java/org/superbiz/executor/AsyncBookServiceTest.java @@ -0,0 +1,73 @@ +package org.superbiz.executor; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@RunWith(Arquillian.class) +public class AsyncBookServiceTest { + + @Inject + private AsyncBookService service; + + @Deployment() + public static final WebArchive app() { + return ShrinkWrap.create(WebArchive.class, "example.war") + .addClasses(AsyncBookService.class) + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void testServiceA() { + final Future<String> future = service.serviceA(); + try { + future.get(200, TimeUnit.MILLISECONDS); + } catch (InterruptedException | TimeoutException e) { + fail("Unexpected exception" + e); + } catch (ExecutionException ioe) { + assertEquals("Simulated error", ioe.getCause().getMessage()); + } + } + + @Test + public void testServiceB() { + final CompletableFuture<Integer> future = service.serviceB(); + try { + assertEquals(2, future.get(200, TimeUnit.MILLISECONDS).intValue()); + } catch (Exception e) { + fail("Unexpected exception" + e); + } + } + +} \ No newline at end of file
