KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > asynchronous > Tester


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.transaction.UserTransaction JavaDoc;
32 import javax.transaction.RollbackException JavaDoc;
33 import javax.transaction.TransactionManager JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
39  * @version $Revision: 39547 $
40  */

41 public class Tester implements TesterMBean
42 {
43    public void testSLLocalAsynchronous() throws Exception JavaDoc
44    {
45       Context JavaDoc ctx = new InitialContext JavaDoc();
46       StatelessLocal tester =
47             (StatelessLocal) ctx.lookup("StatelessBean/local");
48       int ret = tester.method(111);
49       if (ret != 111) throw new RuntimeException JavaDoc("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 JavaDoc("Wrong return value for stateless local "+ ret);
54       AsynchProvider ap = (AsynchProvider) asynchTester;
55       Future future = ap.getFuture();
56       ret = (Integer JavaDoc) future.get();
57       if (ret != 112) throw new RuntimeException JavaDoc("Wrong async return value for stateless local "+ ret);
58    }
59
60
61    public void testSFLocalAsynchronous() throws Exception JavaDoc
62    {
63       Context JavaDoc ctx = new InitialContext JavaDoc();
64       StatefulLocal tester =
65             (StatefulLocal) ctx.lookup("StatefulBean/local");
66       int ret = tester.method(121);
67       if (ret != 121) throw new RuntimeException JavaDoc("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 JavaDoc("Wrong return value for stateful local "+ ret);
72       AsynchProvider ap = (AsynchProvider) asynchTester;
73       Future future = ap.getFuture();
74       ret = (Integer JavaDoc) future.get();
75       if (ret != 122) throw new RuntimeException JavaDoc("Wrong async return value for stateful local "+ ret);
76    }
77
78    public void testServiceLocalAsynchronous() throws Exception JavaDoc
79    {
80       Context JavaDoc ctx = new InitialContext JavaDoc();
81       ServiceLocal tester =
82             (ServiceLocal) ctx.lookup("ServiceBean/local");
83       int ret = tester.method(131);
84       if (ret != 131) throw new RuntimeException JavaDoc("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 JavaDoc("Wrong return value for service local "+ ret);
89       AsynchProvider ap = (AsynchProvider) asynchTester;
90       Future future = ap.getFuture();
91       ret = (Integer JavaDoc) future.get();
92       if (ret != 132) throw new RuntimeException JavaDoc("Wrong async return value for service local "+ ret);
93    }
94
95    public void testLocalAsynchTransaction() throws Exception JavaDoc
96    {
97       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
98       TxSessionLocal tester = (TxSessionLocal) ctx.lookup("TxSessionBean/local");
99       TxSessionLocal asynchTester = (TxSessionLocal)((JBossProxy)tester).getAsynchronousProxy();
100       AsynchProvider ap = (AsynchProvider) asynchTester;
101       TransactionManager JavaDoc tx = (TransactionManager JavaDoc) ctx.lookup(TransactionManagerService.JNDI_NAME);
102
103       //Add some entries in different threads and commit
104
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 JavaDoc entries = tester.getEntries();
119       tx.commit();
120       if (entries.size() != 6) throw new RuntimeException JavaDoc("Wrong number of entries, should have been 6, have: " + entries.size());
121
122       //Cleanup synchronously
123
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 JavaDoc("Wrong number of entries, should have been 0, have: " + entries.size());
131
132       //Add some entries in different threads and rollback
133
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 JavaDoc("Wrong number of entries, should have been 0, have: " + entries.size());
151
152       //Add some entries in different threads and rollback from within Tx
153
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 JavaDoc e)
171       {
172          rollbackException = true;
173       }
174
175       if (!rollbackException) throw new RuntimeException JavaDoc("RollbackException not picked up");
176
177       tx.begin();
178       entries = tester.getEntries();
179       tx.commit();
180       if (entries.size() != 0) throw new RuntimeException JavaDoc("Wrong number of entries, should have been 0, have: " + entries.size());
181    }
182
183    private void waitForProvider(AsynchProvider provider) throws InterruptedException JavaDoc
184    {
185       Future future = provider.getFuture();
186       waitForFuture(future);
187    }
188
189    private void waitForFuture(Future future) throws InterruptedException JavaDoc
190    {
191       while (!future.isDone())
192       {
193          Thread.sleep(100);
194       }
195    }
196
197 }
198
Popular Tags