Author: dblevins
Date: Sat Apr  9 02:05:24 2011
New Revision: 1090509

URL: http://svn.apache.org/viewvc?rev=1090509&view=rev
Log:
OPENEJB-1513: Example: @AccessTimeout

Added:
    openejb/trunk/openejb3/examples/access-timeout/
      - copied from r1090482, openejb/trunk/openejb3/examples/async-methods/
    
openejb/trunk/openejb3/examples/access-timeout/src/main/java/org/superbiz/accesstimeout/
    
openejb/trunk/openejb3/examples/access-timeout/src/main/java/org/superbiz/accesstimeout/BusyBee.java
   (with props)
    
openejb/trunk/openejb3/examples/access-timeout/src/test/java/org/superbiz/accesstimeout/
    
openejb/trunk/openejb3/examples/access-timeout/src/test/java/org/superbiz/accesstimeout/BusyBeeTest.java
   (with props)
Removed:
    openejb/trunk/openejb3/examples/access-timeout/README.txt
Modified:
    openejb/trunk/openejb3/examples/access-timeout/pom.xml

Modified: openejb/trunk/openejb3/examples/access-timeout/pom.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/access-timeout/pom.xml?rev=1090509&r1=1090482&r2=1090509&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/access-timeout/pom.xml (original)
+++ openejb/trunk/openejb3/examples/access-timeout/pom.xml Sat Apr  9 02:05:24 
2011
@@ -22,10 +22,10 @@
 <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-methods</artifactId>
+  <artifactId>access-timeout</artifactId>
   <packaging>jar</packaging>
   <version>1.0-SNAPSHOT</version>
-  <name>OpenEJB :: Examples :: @Asynchronous Methods</name>
+  <name>OpenEJB :: Examples :: @AccessTimeout</name>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

Added: 
openejb/trunk/openejb3/examples/access-timeout/src/main/java/org/superbiz/accesstimeout/BusyBee.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/access-timeout/src/main/java/org/superbiz/accesstimeout/BusyBee.java?rev=1090509&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/access-timeout/src/main/java/org/superbiz/accesstimeout/BusyBee.java
 (added)
+++ 
openejb/trunk/openejb3/examples/access-timeout/src/main/java/org/superbiz/accesstimeout/BusyBee.java
 Sat Apr  9 02:05:24 2011
@@ -0,0 +1,64 @@
+/**
+ * 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.accesstimeout;
+
+import javax.ejb.AccessTimeout;
+import javax.ejb.Asynchronous;
+import javax.ejb.Lock;
+import javax.ejb.Singleton;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import static javax.ejb.LockType.WRITE;
+
+/**
+ * @version $Revision$ $Date$
+ */
+@Singleton
+@Lock(WRITE) 
+public class BusyBee {
+
+    @Asynchronous
+    public Future stayBusy(CountDownLatch ready) {
+        ready.countDown();
+        
+        try {
+            new CountDownLatch(1).await();
+        } catch (InterruptedException e) {
+            Thread.interrupted();
+        }
+
+        return null;
+    }
+
+    @AccessTimeout(0)
+    public void doItNow() {
+        // do something
+    }
+
+    @AccessTimeout(value = 5, unit = TimeUnit.SECONDS)
+    public void doItSoon() {
+        // do something
+    }
+
+    @AccessTimeout(-1)
+    public void justDoIt() {
+        // do something
+    }
+
+}

Propchange: 
openejb/trunk/openejb3/examples/access-timeout/src/main/java/org/superbiz/accesstimeout/BusyBee.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openejb/trunk/openejb3/examples/access-timeout/src/test/java/org/superbiz/accesstimeout/BusyBeeTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/access-timeout/src/test/java/org/superbiz/accesstimeout/BusyBeeTest.java?rev=1090509&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/access-timeout/src/test/java/org/superbiz/accesstimeout/BusyBeeTest.java
 (added)
+++ 
openejb/trunk/openejb3/examples/access-timeout/src/test/java/org/superbiz/accesstimeout/BusyBeeTest.java
 Sat Apr  9 02:05:24 2011
@@ -0,0 +1,84 @@
+/**
+ * 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.accesstimeout;
+
+import junit.framework.TestCase;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.Context;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class BusyBeeTest extends TestCase {
+
+    public void test() throws Exception {
+
+        final Context context = EJBContainer.createEJBContainer().getContext();
+
+        final CountDownLatch ready = new CountDownLatch(1);
+
+        final BusyBee busyBee = (BusyBee) 
context.lookup("java:global/access-timeout/BusyBee");
+
+        // This asynchronous method will never exit 
+        busyBee.stayBusy(ready);
+
+        // Are you working yet little bee?
+        ready.await();
+
+        
+        // OK, Bee is busy
+
+
+        { // Timeout Immediately
+            final long start = System.nanoTime();
+
+            try {
+                busyBee.doItNow();
+
+                fail("The bee should be busy");
+            } catch (Exception e) {
+                // the bee is still too busy as expected
+            }
+
+            assertEquals(0, seconds(start));
+        }
+
+        { // Timeout in 5 seconds
+            final long start = System.nanoTime();
+
+            try {
+                busyBee.doItSoon();
+
+                fail("The bee should be busy");
+            } catch (Exception e) {
+                // the bee is still too busy as expected
+            }
+
+            assertEquals(5, seconds(start));
+        }
+
+        // This will wait forever, give it a try if you have that long
+        //busyBee.justDoIt();
+    }
+
+    private long seconds(long start) {
+        return TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start);
+    }
+}

Propchange: 
openejb/trunk/openejb3/examples/access-timeout/src/test/java/org/superbiz/accesstimeout/BusyBeeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to