| /** |
| * 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.apache.openejb.core.asynch; |
| |
| import java.util.concurrent.Future; |
| |
| import javax.annotation.Resource; |
| import javax.ejb.AsyncResult; |
| import javax.ejb.Asynchronous; |
| import javax.ejb.SessionContext; |
| import javax.ejb.Singleton; |
| import javax.ejb.Stateless; |
| import javax.naming.InitialContext; |
| |
| import org.apache.openejb.assembler.classic.AppInfo; |
| import org.apache.openejb.assembler.classic.Assembler; |
| import org.apache.openejb.assembler.classic.SecurityServiceInfo; |
| import org.apache.openejb.assembler.classic.TransactionServiceInfo; |
| import org.apache.openejb.config.AppModule; |
| import org.apache.openejb.config.ConfigurationFactory; |
| import org.apache.openejb.config.EjbModule; |
| import org.apache.openejb.core.ivm.naming.InitContextFactory; |
| import org.apache.openejb.jee.EjbJar; |
| import org.apache.openejb.jee.SingletonBean; |
| import org.apache.openejb.jee.StatelessBean; |
| import org.junit.After; |
| import org.junit.Assert; |
| import org.junit.Before; |
| import org.junit.Test; |
| |
| /** |
| * Testing of the @Asynchronous annotation on beans. |
| * |
| */ |
| public class AsynchTest{ |
| |
| private Assembler assembler; |
| |
| private ConfigurationFactory config; |
| |
| @Before |
| public void beforeTest() throws Exception { |
| System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName()); |
| config = new ConfigurationFactory(); |
| assembler = new Assembler(); |
| assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class)); |
| assembler.createSecurityService(config.configureService(SecurityServiceInfo.class)); |
| } |
| |
| @After |
| public void afterTest() throws Exception{ |
| assembler.destroy(); |
| } |
| |
| @Test |
| public void testMethodScopeAsynch() throws Exception{ |
| System.out.println(long.class.getName()); |
| System.out.println(String[].class.getCanonicalName()); |
| //Build the application |
| AppModule app = new AppModule(this.getClass().getClassLoader(), "testasynch"); |
| EjbJar ejbJar = new EjbJar(); |
| ejbJar.addEnterpriseBean(new StatelessBean(TestBeanC.class)); |
| ejbJar.addEnterpriseBean(new SingletonBean(TestBeanD.class)); |
| app.getEjbModules().add(new EjbModule(ejbJar)); |
| |
| AppInfo appInfo = config.configureApplication(app); |
| assembler.createApplication(appInfo); |
| |
| InitialContext context = new InitialContext(); |
| |
| String[] beans = new String[]{"TestBeanCLocal", "TestBeanDLocal"}; |
| for(String beanName : beans){ |
| TestBean testBean = (TestBean)context.lookup(beanName); |
| |
| testBean.testA(Thread.currentThread().getId()); |
| Thread.sleep(1000L); |
| Assert.assertEquals("testA was never executed", "testA" , testBean.getLastInvokeMethod()); |
| Future<String> future = testBean.testB(Thread.currentThread().getId()); |
| Thread.sleep(1000L); |
| Assert.assertTrue("The task should be done", future.isDone()); |
| Assert.assertEquals("testB was never executed", "testB" , testBean.getLastInvokeMethod()); |
| testBean.testC(Thread.currentThread().getId()); |
| Assert.assertEquals("testC was never executed", "testC" , testBean.getLastInvokeMethod()); |
| testBean.testD(Thread.currentThread().getId()); |
| Assert.assertEquals("testD was never executed", "testD" , testBean.getLastInvokeMethod()); |
| } |
| } |
| |
| @Test |
| public void testClassScopeAsynch() throws Exception { |
| //Build the application |
| AppModule app = new AppModule(this.getClass().getClassLoader(), "testclassasynch"); |
| EjbJar ejbJar = new EjbJar(); |
| ejbJar.addEnterpriseBean(new SingletonBean(TestBeanA.class)); |
| app.getEjbModules().add(new EjbModule(ejbJar)); |
| |
| AppInfo appInfo = config.configureApplication(app); |
| assembler.createApplication(appInfo); |
| |
| InitialContext context = new InitialContext(); |
| TestBean test = (TestBean)context.lookup("TestBeanALocal"); |
| |
| test.testA(Thread.currentThread().getId()); |
| Thread.sleep(1000L); |
| Assert.assertEquals("testA was never executed", "testA" , test.getLastInvokeMethod()); |
| |
| Future<String> future = test.testB(Thread.currentThread().getId()); |
| Thread.sleep(1000L); |
| Assert.assertTrue("The task should be done", future.isDone()); |
| Assert.assertEquals("testB was never executed", "testB" , test.getLastInvokeMethod()); |
| |
| test.testC(Thread.currentThread().getId()); |
| Assert.assertEquals("testC was never executed", "testC" , test.getLastInvokeMethod()); |
| |
| test.testD(Thread.currentThread().getId()); |
| Assert.assertEquals("testD was never executed", "testD" , test.getLastInvokeMethod()); |
| } |
| |
| @Test |
| public void testSessionContext() throws Exception { |
| //Build the application |
| AppModule app = new AppModule(this.getClass().getClassLoader(), "testcanceltask"); |
| EjbJar ejbJar = new EjbJar(); |
| ejbJar.addEnterpriseBean(new StatelessBean(TestBeanB.class)); |
| app.getEjbModules().add(new EjbModule(ejbJar)); |
| |
| AppInfo appInfo = config.configureApplication(app); |
| assembler.createApplication(appInfo); |
| |
| InitialContext context = new InitialContext(); |
| TestBean test = (TestBean) context.lookup("TestBeanBLocal"); |
| |
| test.testA(Thread.currentThread().getId()); |
| Assert.assertEquals("testA was never executed", "testA", test.getLastInvokeMethod()); |
| |
| Future<String> future = test.testB(Thread.currentThread().getId()); |
| Thread.sleep(1000L); |
| Assert.assertFalse(future.cancel(true)); |
| Assert.assertFalse(future.isCancelled()); |
| Assert.assertFalse(future.isDone()); |
| Thread.sleep(3000L); |
| Assert.assertTrue(future.isDone()); |
| Assert.assertEquals("testB was never executed", "testB", test.getLastInvokeMethod()); |
| |
| test.testC(Thread.currentThread().getId()); |
| Assert.assertEquals("testC was never executed", "testC", test.getLastInvokeMethod()); |
| |
| test.testD(Thread.currentThread().getId()); |
| Thread.sleep(3000L); |
| Assert.assertEquals("testD was never executed", "testD", test.getLastInvokeMethod()); |
| } |
| |
| public interface TestBean { |
| |
| public void testA(long callerThreadId); |
| |
| public Future<String> testB(long callerThreadId); |
| |
| public Future<String> testC(long callerThreadId); |
| |
| public void testD(long callerThreadId); |
| |
| public String getLastInvokeMethod(); |
| } |
| |
| @Stateless |
| public static class TestBeanC implements TestBean { |
| |
| private String lastInvokeMethod; |
| |
| @Asynchronous |
| public void testA(long callerThreadId) { |
| Assert.assertFalse("testA should be executed in asynchronous mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testA"; |
| } |
| |
| @Asynchronous |
| public Future<String> testB(long callerThreadId) { |
| Assert.assertFalse("testB should be executed in asynchronous mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testB"; |
| return new AsyncResult<String>("testB"); |
| } |
| |
| public Future<String> testC(long callerThreadId) { |
| Assert.assertTrue("testC should be executed in blocing mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testC"; |
| return new AsyncResult<String>("testC"); |
| } |
| |
| public void testD(long callerThreadId) { |
| Assert.assertTrue("testD should be executed in blocing mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testD"; |
| } |
| |
| public String getLastInvokeMethod() { |
| return lastInvokeMethod; |
| } |
| } |
| |
| @Singleton |
| public static class TestBeanD implements TestBean { |
| |
| private String lastInvokeMethod; |
| |
| @Asynchronous |
| public void testA(long callerThreadId) { |
| Assert.assertFalse("testA should be executed in asynchronous mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testA"; |
| } |
| |
| @Asynchronous |
| public Future<String> testB(long callerThreadId) { |
| Assert.assertFalse("testB should be executed in asynchronous mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testB"; |
| return new AsyncResult<String>("testB"); |
| } |
| |
| public Future<String> testC(long callerThreadId) { |
| Assert.assertTrue("testC should be executed in blocing mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testC"; |
| return new AsyncResult<String>("testC"); |
| } |
| |
| public void testD(long callerThreadId) { |
| Assert.assertTrue("testD should be executed in blocing mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testD"; |
| } |
| |
| public String getLastInvokeMethod() { |
| return lastInvokeMethod; |
| } |
| } |
| |
| @Asynchronous |
| public static abstract class AbstractBean implements TestBean { |
| |
| protected String lastInvokeMethod; |
| |
| public void testA(long callerThreadId) { |
| Assert.assertFalse("testA should be executed in asynchronous mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testA"; |
| } |
| |
| public Future<String> testB(long callerThreadId) { |
| Assert.assertFalse("testB should be executed in asynchronous mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testB"; |
| return new AsyncResult<String>("testB" + callerThreadId); |
| } |
| |
| } |
| |
| @Singleton |
| public static class TestBeanA extends AbstractBean implements TestBean { |
| |
| public String getLastInvokeMethod() { |
| return lastInvokeMethod; |
| } |
| |
| public Future<String> testC(long callerThreadId) { |
| Assert.assertTrue("testC should be executed in blocing mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testC"; |
| return new AsyncResult<String>("testC"); |
| } |
| |
| public void testD(long callerThreadId) { |
| Assert.assertTrue("testD should be executed in blocing mode", Thread.currentThread().getId() == callerThreadId); |
| lastInvokeMethod = "testD"; |
| } |
| } |
| |
| @Stateless |
| public static class TestBeanB implements TestBean { |
| |
| private String lastInvokeMethod; |
| |
| @Resource |
| private SessionContext sessionContext; |
| |
| public void testA(long callerThreadId) { |
| Assert.assertTrue("testA should be executed in blocing mode", Thread.currentThread().getId() == callerThreadId); |
| Exception expectedException = null; |
| try { |
| sessionContext.wasCancelCalled(); |
| } catch (IllegalStateException e) { |
| expectedException = e; |
| } |
| lastInvokeMethod = "testA"; |
| Assert.assertNotNull("IllegalStateException should be thrown", expectedException); |
| } |
| |
| @Asynchronous |
| public Future<String> testB(long callerThreadId) { |
| Assert.assertFalse("testB should be executed in asynchronous mode", Thread.currentThread().getId() == callerThreadId); |
| Assert.assertFalse(sessionContext.wasCancelCalled()); |
| try { |
| Thread.sleep(3000L); |
| } catch (InterruptedException e) { |
| } |
| Assert.assertTrue(sessionContext.wasCancelCalled()); |
| lastInvokeMethod = "testB"; |
| return new AsyncResult<String>("echoB"); |
| } |
| |
| public Future<String> testC(long callerThreadId) { |
| Assert.assertTrue("testC should be executed in blocing mode", Thread.currentThread().getId() == callerThreadId); |
| Exception expectedException = null; |
| try { |
| sessionContext.wasCancelCalled(); |
| } catch (IllegalStateException e) { |
| expectedException = e; |
| } |
| Assert.assertNotNull("IllegalStateException should be thrown", expectedException); |
| lastInvokeMethod = "testC"; |
| return null; |
| } |
| |
| @Asynchronous |
| public void testD(long callerThreadId) { |
| Assert.assertFalse("testD should be executed in asynchronous mode", Thread.currentThread().getId() == callerThreadId); |
| Exception expectedException = null; |
| try { |
| sessionContext.wasCancelCalled(); |
| } catch (IllegalStateException e) { |
| expectedException = e; |
| } |
| Assert.assertNotNull("IllegalStateException should be thrown", expectedException); |
| lastInvokeMethod = "testD"; |
| } |
| |
| public String getLastInvokeMethod() { |
| return lastInvokeMethod; |
| } |
| } |
| } |