You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2011/09/09 20:18:40 UTC

svn commit: r1167321 - in /openejb/trunk/openejb3/examples: ./ schedule-expression/ schedule-expression/src/ schedule-expression/src/main/ schedule-expression/src/main/java/ schedule-expression/src/main/java/org/ schedule-expression/src/main/java/org/s...

Author: dblevins
Date: Fri Sep  9 18:18:40 2011
New Revision: 1167321

URL: http://svn.apache.org/viewvc?rev=1167321&view=rev
Log:
Example that shows ScheduleExpression

Added:
    openejb/trunk/openejb3/examples/schedule-expression/
    openejb/trunk/openejb3/examples/schedule-expression/build.xml
    openejb/trunk/openejb3/examples/schedule-expression/pom.xml
    openejb/trunk/openejb3/examples/schedule-expression/src/
    openejb/trunk/openejb3/examples/schedule-expression/src/main/
    openejb/trunk/openejb3/examples/schedule-expression/src/main/java/
    openejb/trunk/openejb3/examples/schedule-expression/src/main/java/org/
    openejb/trunk/openejb3/examples/schedule-expression/src/main/java/org/superbiz/
    openejb/trunk/openejb3/examples/schedule-expression/src/main/java/org/superbiz/corn/
    openejb/trunk/openejb3/examples/schedule-expression/src/main/java/org/superbiz/corn/FarmerBrown.java
    openejb/trunk/openejb3/examples/schedule-expression/src/test/
    openejb/trunk/openejb3/examples/schedule-expression/src/test/java/
    openejb/trunk/openejb3/examples/schedule-expression/src/test/java/org/
    openejb/trunk/openejb3/examples/schedule-expression/src/test/java/org/superbiz/
    openejb/trunk/openejb3/examples/schedule-expression/src/test/java/org/superbiz/corn/
    openejb/trunk/openejb3/examples/schedule-expression/src/test/java/org/superbiz/corn/FarmerBrownTest.java
Modified:
    openejb/trunk/openejb3/examples/pom.xml

Modified: openejb/trunk/openejb3/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/pom.xml?rev=1167321&r1=1167320&r2=1167321&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/pom.xml (original)
+++ openejb/trunk/openejb3/examples/pom.xml Fri Sep  9 18:18:40 2011
@@ -73,6 +73,7 @@
     <module>lookup-of-ejbs-with-descriptor</module>
     <module>lookup-of-ejbs</module>
     <module>schedule-methods</module>
+    <module>schedule-expression</module>
     <module>movies-complete-meta</module>
     <module>movies-complete</module>
     <module>schedule-methods-meta</module>

Added: openejb/trunk/openejb3/examples/schedule-expression/build.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/schedule-expression/build.xml?rev=1167321&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/schedule-expression/build.xml (added)
+++ openejb/trunk/openejb3/examples/schedule-expression/build.xml Fri Sep  9 18:18:40 2011
@@ -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: 1151005 $ $Date: 2011-07-26 00:27:21 -0700 (Tue, 26 Jul 2011) $ -->
+
+<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-SNAPSHOT"/>
+  </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-SNAPSHOT"/>
+    <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>

Added: openejb/trunk/openejb3/examples/schedule-expression/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/schedule-expression/pom.xml?rev=1167321&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/schedule-expression/pom.xml (added)
+++ openejb/trunk/openejb3/examples/schedule-expression/pom.xml Fri Sep  9 18:18:40 2011
@@ -0,0 +1,93 @@
+<?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: 1159537 $ $Date: 2011-08-19 01:23:36 -0700 (Fri, 19 Aug 2011) $ -->
+
+<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>schedule-expression</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: ScheduleExpression</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>2.3.2</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-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.1</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.0.0-SNAPSHOT</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>
\ No newline at end of file

Added: openejb/trunk/openejb3/examples/schedule-expression/src/main/java/org/superbiz/corn/FarmerBrown.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/schedule-expression/src/main/java/org/superbiz/corn/FarmerBrown.java?rev=1167321&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/schedule-expression/src/main/java/org/superbiz/corn/FarmerBrown.java (added)
+++ openejb/trunk/openejb3/examples/schedule-expression/src/main/java/org/superbiz/corn/FarmerBrown.java Fri Sep  9 18:18:40 2011
@@ -0,0 +1,87 @@
+/**
+ * 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.corn;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import javax.ejb.Lock;
+import javax.ejb.LockType;
+import javax.ejb.ScheduleExpression;
+import javax.ejb.Singleton;
+import javax.ejb.Startup;
+import javax.ejb.Timeout;
+import javax.ejb.Timer;
+import javax.ejb.TimerConfig;
+import javax.ejb.TimerService;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * This is where we schedule all of Farmer Brown's corn jobs
+ *
+ * @version $Revision$ $Date$
+ */
+@Singleton
+@Lock(LockType.READ) // allows timers to execute in parallel
+@Startup
+public class FarmerBrown {
+
+    private final AtomicInteger checks = new AtomicInteger();
+
+    @Resource
+    private TimerService timerService;
+
+    @PostConstruct
+    private void construct() {
+        final TimerConfig plantTheCorn = new TimerConfig("plantTheCorn", false);
+        timerService.createCalendarTimer(new ScheduleExpression().month(5).dayOfMonth("20-Last").minute(0).hour(8), plantTheCorn);
+        timerService.createCalendarTimer(new ScheduleExpression().month(6).dayOfMonth("1-10").minute(0).hour(8), plantTheCorn);
+
+        final TimerConfig harvestTheCorn = new TimerConfig("harvestTheCorn", false);
+        timerService.createCalendarTimer(new ScheduleExpression().month(9).dayOfMonth("20-Last").minute(0).hour(8), harvestTheCorn);
+        timerService.createCalendarTimer(new ScheduleExpression().month(10).dayOfMonth("1-10").minute(0).hour(8), harvestTheCorn);
+
+        final TimerConfig checkOnTheDaughters = new TimerConfig("checkOnTheDaughters", false);
+        timerService.createCalendarTimer(new ScheduleExpression().second("*").minute("*").hour("*"), checkOnTheDaughters);
+    }
+
+    @Timeout
+    public void timeout(Timer timer) {
+        if ("plantTheCorn".equals(timer.getInfo())) {
+            plantTheCorn();
+        } else if ("harvestTheCorn".equals(timer.getInfo())) {
+            harvestTheCorn();
+        } else if ("checkOnTheDaughters".equals(timer.getInfo())) {
+            checkOnTheDaughters();
+        }
+    }
+
+    private void plantTheCorn() {
+        // Dig out the planter!!!
+    }
+
+    private void harvestTheCorn() {
+        // Dig out the combine!!!
+    }
+
+    private void checkOnTheDaughters() {
+        checks.incrementAndGet();
+    }
+
+    public int getChecks() {
+        return checks.get();
+    }
+}

Added: openejb/trunk/openejb3/examples/schedule-expression/src/test/java/org/superbiz/corn/FarmerBrownTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/schedule-expression/src/test/java/org/superbiz/corn/FarmerBrownTest.java?rev=1167321&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/schedule-expression/src/test/java/org/superbiz/corn/FarmerBrownTest.java (added)
+++ openejb/trunk/openejb3/examples/schedule-expression/src/test/java/org/superbiz/corn/FarmerBrownTest.java Fri Sep  9 18:18:40 2011
@@ -0,0 +1,43 @@
+/**
+ * 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.corn;
+
+import junit.framework.TestCase;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.Context;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class FarmerBrownTest extends TestCase {
+
+    public void test() throws Exception {
+
+        final Context context = EJBContainer.createEJBContainer().getContext();
+
+        final FarmerBrown farmerBrown = (FarmerBrown) context.lookup("java:global/schedule-expression/FarmerBrown");
+
+        // Give Farmer brown a chance to do some work
+        Thread.sleep(SECONDS.toMillis(5));
+
+        final int checks = farmerBrown.getChecks();
+        assertTrue(checks+"", checks > 4);
+    }
+}