1 22 package org.jboss.ejb3.test.asynchronous; 23 24 import org.jboss.aspects.asynch.AsynchProvider; 25 import org.jboss.aspects.asynch.Future; 26 import org.jboss.ejb3.JBossProxy; 27 import org.jboss.tm.TransactionManagerService; 28 29 import javax.naming.InitialContext ; 30 import javax.naming.Context ; 31 import javax.transaction.UserTransaction ; 32 import javax.transaction.RollbackException ; 33 import javax.transaction.TransactionManager ; 34 import java.util.Collection ; 35 import java.util.Iterator ; 36 37 41 public class Tester implements TesterMBean 42 { 43 public void testSLLocalAsynchronous() throws Exception 44 { 45 Context ctx = new InitialContext (); 46 StatelessLocal tester = 47 (StatelessLocal) ctx.lookup("StatelessBean/local"); 48 int ret = tester.method(111); 49 if (ret != 111) throw new RuntimeException ("Wrong return for stateless local "+ ret); 50 51 StatelessLocal asynchTester = (StatelessLocal)((JBossProxy)tester).getAsynchronousProxy(); 52 ret = asynchTester.method(112); 53 if (ret != 0) throw new RuntimeException ("Wrong return value for stateless local "+ ret); 54 AsynchProvider ap = (AsynchProvider) asynchTester; 55 Future future = ap.getFuture(); 56 ret = (Integer ) future.get(); 57 if (ret != 112) throw new RuntimeException ("Wrong async return value for stateless local "+ ret); 58 } 59 60 61 public void testSFLocalAsynchronous() throws Exception 62 { 63 Context ctx = new InitialContext (); 64 StatefulLocal tester = 65 (StatefulLocal) ctx.lookup("StatefulBean/local"); 66 int ret = tester.method(121); 67 if (ret != 121) throw new RuntimeException ("Wrong return for stateful local "+ ret); 68 69 StatefulLocal asynchTester = (StatefulLocal)((JBossProxy)tester).getAsynchronousProxy(); 70 ret = asynchTester.method(122); 71 if (ret != 0) throw new RuntimeException ("Wrong return value for stateful local "+ ret); 72 AsynchProvider ap = (AsynchProvider) asynchTester; 73 Future future = ap.getFuture(); 74 ret = (Integer ) future.get(); 75 if (ret != 122) throw new RuntimeException ("Wrong async return value for stateful local "+ ret); 76 } 77 78 public void testServiceLocalAsynchronous() throws Exception 79 { 80 Context ctx = new InitialContext (); 81 ServiceLocal tester = 82 (ServiceLocal) ctx.lookup("ServiceBean/local"); 83 int ret = tester.method(131); 84 if (ret != 131) throw new RuntimeException ("Wrong return for service local "+ ret); 85 86 ServiceLocal asynchTester = (ServiceLocal)((JBossProxy)tester).getAsynchronousProxy(); 87 ret = asynchTester.method(132); 88 if (ret != 0) throw new RuntimeException ("Wrong return value for service local "+ ret); 89 AsynchProvider ap = (AsynchProvider) asynchTester; 90 Future future = ap.getFuture(); 91 ret = (Integer ) future.get(); 92 if (ret != 132) throw new RuntimeException ("Wrong async return value for service local "+ ret); 93 } 94 95 public void testLocalAsynchTransaction() throws Exception 96 { 97 InitialContext ctx = new InitialContext (); 98 TxSessionLocal tester = (TxSessionLocal) ctx.lookup("TxSessionBean/local"); 99 TxSessionLocal asynchTester = (TxSessionLocal)((JBossProxy)tester).getAsynchronousProxy(); 100 AsynchProvider ap = (AsynchProvider) asynchTester; 101 TransactionManager tx = (TransactionManager ) ctx.lookup(TransactionManagerService.JNDI_NAME); 102 103 tx.begin(); 105 tester.createFruit("apple", false); 106 tester.createFruit("pear", false); 107 tester.createFruit("tomato", false); 108 109 asynchTester.createVeg("Potato", false); 110 waitForProvider(ap); 111 asynchTester.createVeg("Turnip", false); 112 waitForProvider(ap); 113 asynchTester.createVeg("Carrot", false); 114 waitForProvider(ap); 115 tx.commit(); 116 117 tx.begin(); 118 Collection entries = tester.getEntries(); 119 tx.commit(); 120 if (entries.size() != 6) throw new RuntimeException ("Wrong number of entries, should have been 6, have: " + entries.size()); 121 122 tx.begin(); 124 tester.cleanAll(); 125 tx.commit(); 126 127 tx.begin(); 128 entries = tester.getEntries(); 129 tx.commit(); 130 if (entries.size() != 0) throw new RuntimeException ("Wrong number of entries, should have been 0, have: " + entries.size()); 131 132 tx.begin(); 134 tester.createFruit("apple", false); 135 tester.createFruit("pear", false); 136 tester.createFruit("tomato", false); 137 138 asynchTester.createVeg("Potato", false); 139 waitForProvider(ap); 140 asynchTester.createVeg("Turnip", false); 141 waitForProvider(ap); 142 asynchTester.createVeg("Carrot", false); 143 waitForProvider(ap); 144 tx.rollback(); 145 146 tx.begin(); 147 entries = tester.getEntries(); 148 tx.commit(); 149 150 if (entries.size() != 0) throw new RuntimeException ("Wrong number of entries, should have been 0, have: " + entries.size()); 151 152 tx.begin(); 154 tester.createFruit("apple", false); 155 tester.createFruit("pear", false); 156 tester.createFruit("tomato", true); 157 158 asynchTester.createVeg("Potato", false); 159 waitForProvider(ap); 160 asynchTester.createVeg("Turnip", false); 161 waitForProvider(ap); 162 asynchTester.createVeg("Carrot", true); 163 waitForProvider(ap); 164 165 boolean rollbackException = false; 166 try 167 { 168 tx.commit(); 169 } 170 catch(RollbackException e) 171 { 172 rollbackException = true; 173 } 174 175 if (!rollbackException) throw new RuntimeException ("RollbackException not picked up"); 176 177 tx.begin(); 178 entries = tester.getEntries(); 179 tx.commit(); 180 if (entries.size() != 0) throw new RuntimeException ("Wrong number of entries, should have been 0, have: " + entries.size()); 181 } 182 183 private void waitForProvider(AsynchProvider provider) throws InterruptedException 184 { 185 Future future = provider.getFuture(); 186 waitForFuture(future); 187 } 188 189 private void waitForFuture(Future future) throws InterruptedException 190 { 191 while (!future.isDone()) 192 { 193 Thread.sleep(100); 194 } 195 } 196 197 } 198 | Popular Tags |