You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by ra...@apache.org on 2019/01/30 12:22:22 UTC

[tomee] 01/07: Example of how to use Fault Tolerance Timeout

This is an automated email from the ASF dual-hosted git repository.

radcortez pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit 36eed58098252a5c40733d0a13a882c0c1732ced
Author: josehenriqueventura <jo...@protonmail.com>
AuthorDate: Sat Jan 26 21:25:05 2019 +0000

    Example of how to use Fault Tolerance Timeout
---
 examples/mp-faulttolerance-timeout/README.adoc     | 98 ++++++++++++++++++++++
 examples/mp-faulttolerance-timeout/pom.xml         | 93 ++++++++++++++++++++
 .../java/org/superbiz/rest/WeatherGateway.java     | 49 +++++++++++
 .../java/org/superbiz/rest/WeatherService.java     | 39 +++++++++
 .../java/org/superbiz/rest/WeatherServiceTest.java | 66 +++++++++++++++
 .../src/test/resources/arquillian.xml              | 30 +++++++
 .../src/test/resources/beans.xml                   |  7 ++
 examples/pom.xml                                   |  1 +
 8 files changed, 383 insertions(+)

diff --git a/examples/mp-faulttolerance-timeout/README.adoc b/examples/mp-faulttolerance-timeout/README.adoc
new file mode 100644
index 0000000..f4bfc36
--- /dev/null
+++ b/examples/mp-faulttolerance-timeout/README.adoc
@@ -0,0 +1,98 @@
+= MicroProfile Fault Tolerance - Timeout
+:index-group: MicroProfile
+:jbake-type: page
+:jbake-status: published
+
+This is an example of how to use Microprofile @Timeout in TomEE.
+
+== Timeout Feature
+
+Fault Tolerance Timeout allow to specify how long a task can take to complete its execution and aborting it
+in case of timeout. Timeout feature can be used along with another annotations to guide the execution and result of a task.
+Check the
+https://download.eclipse.org/microprofile/microprofile-fault-tolerance-1.1/microprofile-fault-tolerance-spec.html#_timeout_usage[specification]
+for more details.
+
+Timeout annotation allows to configure :
+
+* *value:* the timeout value
+* *unit:* the timeout unit
+
+== Examples
+
+=== Run the application
+
+....
+mvn clean install tomee:run
+....
+
+=== Example 1
+
+The method `statusOfDayByAccuWeather` fails when threshold of `@Timeout` annotation is reached due to a long execution of
+`longProcessingTask` method. To respond the request nicely, a fallback method will be activated to complete the request
+successfully by the annotation `@Fallback`.
+
+[source,java]
+----
+@RequestScoped
+public class WeatherGateway {
+
+    private static final Logger LOGGER = Logger.getLogger(WeatherGateway.class.getName());
+
+    @Timeout(1000)
+    @Fallback(fallbackMethod = "statusOfWeekByMetEireann")
+    public String statusOfDayByAccuWeather(){
+        return longProcessingTask();
+    }
+
+    public String statusOfWeekByMetEireann(){
+        LOGGER.log(Level.WARNING, "MetEireann backup service has been requested due to AccuWeather timeout");
+        return "Beautiful day";
+    }
+
+    private String longProcessingTask(){
+        try {
+            Thread.sleep(1100);
+        } catch (InterruptedException e) {
+            LOGGER.log(Level.WARNING,"AccuWeather task has been interrupted.");
+        }
+        return null;
+    }
+    ...
+}
+----
+
+Day status call
+
+....
+GET http://localhost:8080/mp-faulttolerance-retry/weather/day/status
+....
+
+Server log
+
+....
+WARNING AccuWeather task has been interrupted.
+WARNING MetEireann backup service has been requested due to AccuWeather timeout
+....
+
+Response
+
+....
+Beautiful day!
+....
+
+=== Run the tests
+
+You can also try it out using the
+link:src/test/java/org/superbiz/rest/WeatherServiceTest.java[WeatherServiceTest.java]
+available in the project.
+
+....
+mvn clean test
+....
+
+....
+[INFO] Results:
+[INFO] 
+[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
+....
diff --git a/examples/mp-faulttolerance-timeout/pom.xml b/examples/mp-faulttolerance-timeout/pom.xml
new file mode 100644
index 0000000..88e2c02
--- /dev/null
+++ b/examples/mp-faulttolerance-timeout/pom.xml
@@ -0,0 +1,93 @@
+<?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>mp-faulttolerance-timeout</artifactId>
+    <version>8.0.0-SNAPSHOT</version>
+    <packaging>war</packaging>
+    <name>OpenEJB :: Examples :: Microprofile Fault Tolerance :: Timeout</name>
+
+    <properties>
+        <microprofile-fault-tolerance-api.version>1.0</microprofile-fault-tolerance-api.version>
+        <arquillian-junit-container.version>1.4.0.Final</arquillian-junit-container.version>
+        <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
+        <maven-war-plugin.version>3.1.0</maven-war-plugin.version>
+        <tomee.version>${project.version}</tomee.version>
+        <javaee-api.version>8.0</javaee-api.version>
+        <junit.version>4.12</junit.version>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.tomee</groupId>
+            <artifactId>javaee-api</artifactId>
+            <version>${javaee-api.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.eclipse.microprofile.fault-tolerance</groupId>
+            <artifactId>microprofile-fault-tolerance-api</artifactId>
+            <version>${microprofile-fault-tolerance-api.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomee</groupId>
+            <artifactId>openejb-cxf-rs</artifactId>
+            <version>${tomee.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.jboss.arquillian.junit</groupId>
+            <artifactId>arquillian-junit-container</artifactId>
+            <version>${arquillian-junit-container.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomee</groupId>
+            <artifactId>arquillian-tomee-remote</artifactId>
+            <version>${tomee.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomee</groupId>
+            <artifactId>apache-tomee</artifactId>
+            <version>${tomee.version}</version>
+            <type>zip</type>
+            <classifier>microprofile</classifier>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.tomee.maven</groupId>
+                <artifactId>tomee-maven-plugin</artifactId>
+                <version>${project.version}</version>
+                <configuration>
+                    <tomeeClassifier>microprofile</tomeeClassifier>
+                    <context>${artifactId}</context>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>${maven-war-plugin.version}</version>
+                <configuration>
+                    <failOnMissingWebXml>false</failOnMissingWebXml>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/examples/mp-faulttolerance-timeout/src/main/java/org/superbiz/rest/WeatherGateway.java b/examples/mp-faulttolerance-timeout/src/main/java/org/superbiz/rest/WeatherGateway.java
new file mode 100644
index 0000000..c9dd379
--- /dev/null
+++ b/examples/mp-faulttolerance-timeout/src/main/java/org/superbiz/rest/WeatherGateway.java
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+package org.superbiz.rest;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.enterprise.context.RequestScoped;
+import org.eclipse.microprofile.faulttolerance.Fallback;
+import org.eclipse.microprofile.faulttolerance.Timeout;
+
+@RequestScoped
+public class WeatherGateway {
+
+    private static final Logger LOGGER = Logger.getLogger(WeatherGateway.class.getName());
+
+    @Timeout(1000)
+    @Fallback(fallbackMethod = "statusOfWeekByMetEireann")
+    public String statusOfDayByAccuWeather(){
+        return longProcessingTask();
+    }
+
+    public String statusOfWeekByMetEireann(){
+        LOGGER.log(Level.WARNING, "MetEireann backup service has been requested due to AccuWeather timeout");
+        return "Beautiful day";
+    }
+
+    private String longProcessingTask(){
+        try {
+            Thread.sleep(1100);
+        } catch (InterruptedException e) {
+            LOGGER.log(Level.WARNING,"AccuWeather task has been interrupted.");
+        }
+        return null;
+    }
+}
diff --git a/examples/mp-faulttolerance-timeout/src/main/java/org/superbiz/rest/WeatherService.java b/examples/mp-faulttolerance-timeout/src/main/java/org/superbiz/rest/WeatherService.java
new file mode 100644
index 0000000..2b96049
--- /dev/null
+++ b/examples/mp-faulttolerance-timeout/src/main/java/org/superbiz/rest/WeatherService.java
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+package org.superbiz.rest;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Path("/weather")
+@RequestScoped
+public class WeatherService {
+
+    @Inject
+    private WeatherGateway weatherService;
+
+    @GET
+    @Path("/day/status")
+    @Produces(MediaType.TEXT_PLAIN)
+    public String dayStatus() {
+        return weatherService.statusOfDayByAccuWeather();
+    }
+}
diff --git a/examples/mp-faulttolerance-timeout/src/test/java/org/superbiz/rest/WeatherServiceTest.java b/examples/mp-faulttolerance-timeout/src/test/java/org/superbiz/rest/WeatherServiceTest.java
new file mode 100644
index 0000000..b24890f
--- /dev/null
+++ b/examples/mp-faulttolerance-timeout/src/test/java/org/superbiz/rest/WeatherServiceTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+package org.superbiz.rest;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Arquillian.class)
+public class WeatherServiceTest {
+
+    @ArquillianResource
+    private URL base;
+
+    private Client client;
+
+    @Deployment(testable = false)
+    public static WebArchive createDeployment() {
+        return ShrinkWrap.create(WebArchive.class, "test.war").addPackage(WeatherService.class.getPackage());
+    }
+
+    @Before
+    public void before() {
+        this.client = ClientBuilder.newClient();
+    }
+
+    @Test
+    public void testStatusOfDay() {
+        WebTarget webTarget = this.client.target(this.base.toExternalForm());
+        Response response = webTarget.path("/weather/day/status").request().get();
+        assertEquals("Beautiful day", response.readEntity(String.class));
+    }
+
+    @After
+    public void after() {
+        this.client.close();
+    }
+}
diff --git a/examples/mp-faulttolerance-timeout/src/test/resources/arquillian.xml b/examples/mp-faulttolerance-timeout/src/test/resources/arquillian.xml
new file mode 100644
index 0000000..3029d48
--- /dev/null
+++ b/examples/mp-faulttolerance-timeout/src/test/resources/arquillian.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+
+    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
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    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.
+-->
+<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+
+  <container qualifier="tomee" default="true">
+    <configuration>
+      <property name="httpPort">-1</property>
+      <property name="stopPort">-1</property>
+      <property name="classifier">microprofile</property>
+      <property name="dir">target/apache-tomee-remote</property>
+      <property name="appWorkingDir">target/arquillian-test-working-dir</property>
+    </configuration>
+  </container>
+</arquillian>
\ No newline at end of file
diff --git a/examples/mp-faulttolerance-timeout/src/test/resources/beans.xml b/examples/mp-faulttolerance-timeout/src/test/resources/beans.xml
new file mode 100644
index 0000000..d942d7a
--- /dev/null
+++ b/examples/mp-faulttolerance-timeout/src/test/resources/beans.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://java.sun.com/xml/ns/javaee"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+      http://java.sun.com/xml/ns/javaee
+      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
+</beans>
\ No newline at end of file
diff --git a/examples/pom.xml b/examples/pom.xml
index 804d124..d195ad5 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -188,6 +188,7 @@
     <module>vaadin-lts-v08-simple</module>
     <module>vaadin-lts-v10-simple</module>
     <module>vaadin-vxx-simple</module>
+    <module>mp-faulttolerance-timeout</module>
   </modules>
 
   <dependencies>