Author: dblevins
Date: Tue Jul 2 01:26:38 2013
New Revision: 1498751
URL: http://svn.apache.org/r1498751
Log:
TOMEE-987 - Example: Asynchronous @PostConstruct pattern
Added:
tomee/tomee/trunk/examples/async-postconstruct/
tomee/tomee/trunk/examples/async-postconstruct/README.md
tomee/tomee/trunk/examples/async-postconstruct/build.xml (with props)
tomee/tomee/trunk/examples/async-postconstruct/pom.xml (with props)
tomee/tomee/trunk/examples/async-postconstruct/src/
tomee/tomee/trunk/examples/async-postconstruct/src/main/
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/Executor.java
(with props)
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/SlowStarter.java
(with props)
tomee/tomee/trunk/examples/async-postconstruct/src/test/
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/superbiz/
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/superbiz/asyncpost/
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/superbiz/asyncpost/SlowStarterTest.java
(with props)
Modified:
tomee/tomee/trunk/examples/index.md
tomee/tomee/trunk/examples/pom.xml
Added: tomee/tomee/trunk/examples/async-postconstruct/README.md
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/examples/async-postconstruct/README.md?rev=1498751&view=auto
==============================================================================
--- tomee/tomee/trunk/examples/async-postconstruct/README.md (added)
+++ tomee/tomee/trunk/examples/async-postconstruct/README.md Tue Jul 2
01:26:38 2013
@@ -0,0 +1,111 @@
+Title: @Asynchronous @PostConstruct
+
+Placing `@Asynchronous` on the `@PostConstruct` of an EJB is not a supported
part of Java EE, but this example shows a patter which works just as well with
little effort.
+
+ package org.superbiz.asyncpost;
+
+ import javax.annotation.PostConstruct;
+ import javax.ejb.EJB;
+ import javax.ejb.Lock;
+ import javax.ejb.LockType;
+ import javax.ejb.Singleton;
+ import javax.interceptor.AroundInvoke;
+ import javax.interceptor.InvocationContext;
+ import java.util.concurrent.Callable;
+ import java.util.concurrent.Future;
+
+ import static java.util.concurrent.TimeUnit.SECONDS;
+
+ @Singleton
+ @Lock(LockType.READ)
+ public class SlowStarter {
+
+ @EJB
+ private Executor executor;
+
+ private Future construct;
+
+ private String color;
+ private String shape;
+
+ @PostConstruct
+ private void construct() throws Exception {
+ construct = executor.submit(new Callable() {
+ @Override
+ public Object call() throws Exception {
+ Thread.sleep(SECONDS.toMillis(10));
+ SlowStarter.this.color = "orange";
+ SlowStarter.this.shape = "circle";
+ return null;
+ }
+ });
+ }
+
+ @AroundInvoke
+ private Object guaranteeConstructionComplete(InvocationContext
context) throws Exception {
+ construct.get();
+ return context.proceed();
+ }
+
+ public String getColor() {
+ return color;
+ }
+
+ public String getShape() {
+ return shape;
+ }
+ }
+
+
+The `Executor` is a simple pattern, useful for many things, which exposes an
interface functionaly equivalent to `java.util.concurrent.ExecutorService`, but
+with the underlying thread pool controlled by the container.
+
+ package org.superbiz.asyncpost;
+
+ import javax.ejb.AsyncResult;
+ import javax.ejb.Asynchronous;
+ import javax.ejb.Lock;
+ import javax.ejb.LockType;
+ import javax.ejb.Singleton;
+ import java.util.concurrent.Callable;
+ import java.util.concurrent.Future;
+
+ @Singleton
+ @Lock(LockType.READ)
+ public class Executor {
+
+ @Asynchronous
+ public <T> Future<T> submit(Callable<T> task) throws Exception {
+ return new AsyncResult<T>(task.call());
+ }
+
+ }
+
+
+Finally a test case shows the usefulness of `@AroundInvoke` call in our bean
that calls `construct.get()`
+
+ package org.superbiz.asyncpost;
+
+ import junit.framework.Assert;
+ import org.junit.Test;
+
+ import javax.ejb.EJB;
+ import javax.ejb.embeddable.EJBContainer;
+
+ public class SlowStarterTest {
+
+ @EJB
+ private SlowStarter slowStarter;
+
+ @Test
+ public void test() throws Exception {
+
+ // Start the Container
+ EJBContainer.createEJBContainer().getContext().bind("inject",
this);
+
+ // Immediately access the fields initialized in the PostConstruct
+ // This will fail without the @AroundInvoke call to construct.get()
+ Assert.assertEquals("orange", slowStarter.getColor());
+ Assert.assertEquals("circle", slowStarter.getShape());
+ }
+ }
Added: tomee/tomee/trunk/examples/async-postconstruct/build.xml
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/examples/async-postconstruct/build.xml?rev=1498751&view=auto
==============================================================================
--- tomee/tomee/trunk/examples/async-postconstruct/build.xml (added)
+++ tomee/tomee/trunk/examples/async-postconstruct/build.xml Tue Jul 2
01:26:38 2013
@@ -0,0 +1,119 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+-->
+
+<!-- $Rev: 1237516 $ $Date: 2012-01-29 16:48:17 -0800 (Sun, 29 Jan 2012) $ -->
+
+<project name="MyProject" default="dist" basedir="."
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
+
+ <!-- ===============================================================
+
+ HOW TO RUN
+
+ Download
http://archive.apache.org/dist/maven/binaries/maven-ant-tasks-2.0.9.jar
+ Then execute ant as follows:
+
+ ant -lib maven-ant-tasks-2.0.9.jar
+
+ NOTE
+
+ You do NOT need maven-ant-tasks-2.0.9.jar to use OpenEJB for embedded EJB
+ testing with Ant. It is simply used in this example to make the build.xml
+ a bit simpler. As long as OpenEJB and it's required libraries are in the
+ <junit> classpath, the tests will run with OpenEJB embedded.
+
+ ================================================================= -->
+
+ <artifact:remoteRepository id="apache.snapshot.repository"
url="http://repository.apache.org/snapshots/"/>
+ <artifact:remoteRepository id="m2.repository"
url="http://repo1.maven.org/maven2/"/>
+
+ <!-- Build Classpath -->
+ <artifact:dependencies pathId="classpath.main">
+ <dependency groupId="org.apache.openejb" artifactId="javaee-api-embedded"
version="6.0-3"/>
+ </artifact:dependencies>
+
+ <!-- Test Build Classpath -->
+ <artifact:dependencies pathId="classpath.test.build">
+ <dependency groupId="junit" artifactId="junit" version="4.3.1"/>
+ </artifact:dependencies>
+
+ <!-- Test Run Classpath -->
+ <artifact:dependencies pathId="classpath.test.run">
+ <remoteRepository refid="apache.snapshot.repository"/>
+ <remoteRepository refid="m2.repository"/>
+
+ <dependency groupId="org.apache.openejb" artifactId="openejb-core"
version="4.0.0-beta-1"/>
+ <dependency groupId="junit" artifactId="junit" version="4.3.1"/>
+ </artifact:dependencies>
+
+ <!-- Properties -->
+
+ <property name="src.main.java" location="src/main/java"/>
+ <property name="src.main.resources" location="src/main/resources"/>
+ <property name="src.test.java" location="src/test/java"/>
+ <property name="build.main" location="target/classes"/>
+ <property name="build.test" location="target/test-classes"/>
+ <property name="test.reports" location="target/test-reports"/>
+ <property name="dist" location="target"/>
+
+
+ <target name="init">
+ <mkdir dir="${build.main}"/>
+ <mkdir dir="${build.test}"/>
+ <mkdir dir="${test.reports}"/>
+ </target>
+
+ <target name="compile" depends="init">
+
+ <javac srcdir="${src.main.java}" destdir="${build.main}">
+ <classpath refid="classpath.main"/>
+ </javac>
+ <copy todir="${build.main}">
+ <fileset dir="${src.main.resources}"/>
+ </copy>
+
+ <javac srcdir="${src.test.java}" destdir="${build.test}">
+ <classpath location="${build.main}"/>
+ <classpath refid="classpath.main"/>
+ <classpath refid="classpath.test.build"/>
+ </javac>
+ </target>
+
+ <target name="test" depends="compile">
+ <junit fork="yes" printsummary="yes">
+ <classpath location="${build.main}"/>
+ <classpath location="${build.test}"/>
+ <classpath refid="classpath.main"/>
+ <classpath refid="classpath.test.build"/>
+ <classpath refid="classpath.test.run"/>
+
+ <formatter type="plain"/>
+
+ <batchtest fork="yes" todir="${test.reports}">
+ <fileset dir="${src.test.java}">
+ <include name="**/*Test.java"/>
+ </fileset>
+ </batchtest>
+ </junit>
+ </target>
+
+ <target name="dist" depends="test">
+ <jar jarfile="${dist}/myproject-1.0.jar" basedir="${build.main}"/>
+ </target>
+
+</project>
Propchange: tomee/tomee/trunk/examples/async-postconstruct/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: tomee/tomee/trunk/examples/async-postconstruct/pom.xml
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/examples/async-postconstruct/pom.xml?rev=1498751&view=auto
==============================================================================
--- tomee/tomee/trunk/examples/async-postconstruct/pom.xml (added)
+++ tomee/tomee/trunk/examples/async-postconstruct/pom.xml Tue Jul 2 01:26:38
2013
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+-->
+
+<!-- $Rev: 1461000 $ $Date: 2013-03-26 00:13:43 -0700 (Tue, 26 Mar 2013) $ -->
+
+<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/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.superbiz</groupId>
+ <artifactId>async-postconstruct</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>OpenEJB :: Examples :: @Asynchronous @PostConstrct</name>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <build>
+ <defaultGoal>install</defaultGoal>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.0</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>apache-m2-snapshot</id>
+ <name>Apache Snapshot Repository</name>
+ <url>http://repository.apache.org/snapshots</url>
+ </repository>
+ </repositories>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>javaee-api</artifactId>
+ <version>6.0-4</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+ <!--
+ The <scope>test</scope> guarantees that non of your runtime
+ code is dependent on any OpenEJB classes.
+ -->
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>openejb-core</artifactId>
+ <version>4.5.2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <!--
+ This section allows you to configure where to publish libraries for sharing.
+ It is not required and may be deleted. For more information see:
+ http://maven.apache.org/plugins/maven-deploy-plugin/
+ -->
+ <distributionManagement>
+ <repository>
+ <id>localhost</id>
+ <url>file://${basedir}/target/repo/</url>
+ </repository>
+ <snapshotRepository>
+ <id>localhost</id>
+ <url>file://${basedir}/target/snapshot-repo/</url>
+ </snapshotRepository>
+ </distributionManagement>
+</project>
Propchange: tomee/tomee/trunk/examples/async-postconstruct/pom.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added:
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/Executor.java
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/Executor.java?rev=1498751&view=auto
==============================================================================
---
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/Executor.java
(added)
+++
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/Executor.java
Tue Jul 2 01:26:38 2013
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+package org.superbiz.asyncpost;
+
+import javax.ejb.AsyncResult;
+import javax.ejb.Asynchronous;
+import javax.ejb.Lock;
+import javax.ejb.LockType;
+import javax.ejb.Singleton;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+
+@Singleton
+@Lock(LockType.READ)
+public class Executor {
+
+ @Asynchronous
+ public <T> Future<T> submit(Callable<T> task) throws Exception {
+ return new AsyncResult<T>(task.call());
+ }
+
+}
Propchange:
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/Executor.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/SlowStarter.java
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/SlowStarter.java?rev=1498751&view=auto
==============================================================================
---
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/SlowStarter.java
(added)
+++
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/SlowStarter.java
Tue Jul 2 01:26:38 2013
@@ -0,0 +1,71 @@
+/*
+ * 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.
+ */
+package org.superbiz.asyncpost;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.EJB;
+import javax.ejb.Lock;
+import javax.ejb.LockType;
+import javax.ejb.Singleton;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.AroundTimeout;
+import javax.interceptor.InvocationContext;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+@Singleton
+@Lock(LockType.READ)
+public class SlowStarter {
+
+ @EJB
+ private Executor executor;
+
+ private Future construct;
+
+ private String color;
+ private String shape;
+
+ @PostConstruct
+ private void construct() throws Exception {
+ construct = executor.submit(new Callable() {
+ @Override
+ public Object call() throws Exception {
+ Thread.sleep(SECONDS.toMillis(10));
+ SlowStarter.this.color = "orange";
+ SlowStarter.this.shape = "circle";
+ return null;
+ }
+ });
+ }
+
+ @AroundTimeout
+ @AroundInvoke
+ private Object guaranteeConstructionComplete(InvocationContext context)
throws Exception {
+ construct.get();
+ return context.proceed();
+ }
+
+ public String getColor() {
+ return color;
+ }
+
+ public String getShape() {
+ return shape;
+ }
+}
Propchange:
tomee/tomee/trunk/examples/async-postconstruct/src/main/java/org/superbiz/asyncpost/SlowStarter.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/superbiz/asyncpost/SlowStarterTest.java
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/superbiz/asyncpost/SlowStarterTest.java?rev=1498751&view=auto
==============================================================================
---
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/superbiz/asyncpost/SlowStarterTest.java
(added)
+++
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/superbiz/asyncpost/SlowStarterTest.java
Tue Jul 2 01:26:38 2013
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+package org.superbiz.asyncpost;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+import javax.ejb.EJB;
+import javax.ejb.embeddable.EJBContainer;
+
+public class SlowStarterTest {
+
+ @EJB
+ private SlowStarter slowStarter;
+
+ @Test
+ public void test() throws Exception {
+
+ // Start the Container
+ EJBContainer.createEJBContainer().getContext().bind("inject", this);
+
+ // Immediately access the fields initialized in the PostConstruct
+ // This will fail without the @AroundInvoke call to construct.get()
+ Assert.assertEquals("orange", slowStarter.getColor());
+ Assert.assertEquals("circle", slowStarter.getShape());
+ }
+}
Propchange:
tomee/tomee/trunk/examples/async-postconstruct/src/test/java/org/superbiz/asyncpost/SlowStarterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: tomee/tomee/trunk/examples/index.md
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/examples/index.md?rev=1498751&r1=1498750&r2=1498751&view=diff
==============================================================================
--- tomee/tomee/trunk/examples/index.md (original)
+++ tomee/tomee/trunk/examples/index.md Tue Jul 2 01:26:38 2013
@@ -64,6 +64,7 @@ All the examples show code, but some are
- [schedule-expression](schedule-expression/README.html) *
- [schedule-methods](schedule-methods/README.html)
- [interceptors](interceptors/README.html)
+ - [async-postconstruct](async-postconstruct/README.html)
}
{span-one-third
Modified: tomee/tomee/trunk/examples/pom.xml
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/examples/pom.xml?rev=1498751&r1=1498750&r2=1498751&view=diff
==============================================================================
--- tomee/tomee/trunk/examples/pom.xml (original)
+++ tomee/tomee/trunk/examples/pom.xml Tue Jul 2 01:26:38 2013
@@ -36,6 +36,7 @@
<module>application-composer</module>
<module>applicationexception</module>
<module>async-methods</module>
+ <module>async-postconstruct</module>
<module>bean-validation-design-by-contract</module>
<module>cdi-alternative-and-stereotypes</module>
<module>cdi-application-scope</module>