KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jms > EjbCompClient


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): ____________________________________.
22  * Contributor(s): Fran?ois Exertier
23  *
24  * --------------------------------------------------------------------------
25  * $Id: EjbCompClient.java,v 1.6 2004/04/19 06:39:29 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // EjbCompClient.java
30
// Client for accessing session bean EjbComp and entity bean eb.Account,
31
// call sendMsg (send a JMS message) on EjbComp and create an instance
32
// of Account (database record creation).
33
package jms;
34
35 import javax.naming.Context;
36 import javax.naming.InitialContext;
37 import javax.naming.NamingException;
38 import javax.rmi.PortableRemoteObject;
39 import javax.transaction.UserTransaction;
40
41 import eb.Account;
42 import eb.AccountHome;
43
44 /**
45  *
46  */

47 public class EjbCompClient {
48
49     private static Context initialContext = null;
50
51     private static EjbCompHome home1 = null;
52
53     private static AccountHome home2 = null;
54
55     private static UserTransaction utx = null;
56
57     public static void main(String[] arg) {
58
59         // Get InitialContext
60
try {
61             initialContext = new InitialContext();
62         } catch (NamingException e) {
63             e.printStackTrace();
64             System.exit(2);
65         }
66
67         // For starting transactions from client: get UserTransaction
68
System.out.println("Getting a UserTransaction object from JNDI");
69         try {
70             utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
71         } catch (Exception e) {
72             System.err.println("Cannot lookup UserTransaction: " + e);
73             System.exit(2);
74         }
75
76         // Lookup EjbComp bean home
77
try {
78             home1 = (EjbCompHome) PortableRemoteObject.narrow(initialContext.lookup("EjbCompHome"), EjbCompHome.class);
79         } catch (Exception e) {
80             System.err.println("Cannot lookup EjbCompHome: " + e);
81             System.exit(2);
82         }
83
84         // Lookup Account bean home
85
try {
86             home2 = (AccountHome) PortableRemoteObject.narrow(initialContext.lookup("AccountImplHome"),
87                     AccountHome.class);
88         } catch (Exception e) {
89             System.err.println("Cannot lookup AccountImplHome: " + e);
90             // no entity bean to use for running the jms sample
91
System.out.println("Sample will work without entity bean Account");
92             home2 = null;
93         }
94
95         // Delete account that may have been created by a previous run
96
try {
97             if (home2 != null) { // use entity bean account in the sample
98
Account b = home2.findByNumber(222);
99                 b.remove();
100             }
101         } catch (Exception e) {
102         }
103
104         // EjbComp creation
105
EjbComp aJmsBean = null;
106         try {
107             System.out.println("Creating an EjbComp bean");
108             aJmsBean = home1.create();
109         } catch (Exception e) {
110             System.err.println("Cannot create EjbComp: " + e);
111             System.exit(2);
112         }
113
114         // Calling EjbComp method that sends a message and eventually creating
115
// the corresponding account (i.e. in the database), in a
116
// transaction that succeeds !
117
Account aDataBean = null;
118         try {
119             utx.begin();
120             System.out.println("Sending a message (will be commited)");
121             aJmsBean.sendMsg("Hello commit");
122             if (home2 != null) {
123                 System.out.println("Creating an Account bean (will be commited)");
124                 aDataBean = home2.create(222, "JMS Sample OK", 0);
125             }
126             utx.commit();
127         } catch (Exception e) {
128             System.err.println("Exception .... : " + e);
129             e.printStackTrace();
130             System.exit(2);
131         }
132
133         // Calling EjbComp method that sends a message and eventually creating
134
// the corresponding account (i.e. in the database), in a
135
// transaction that fails !
136
try {
137             utx.begin();
138             System.out.println("Sending a message (will be rolled back)");
139             aJmsBean.sendMsg("Hello rollback");
140             if (home2 != null) {
141                 System.out.println("Creating a Account bean (will be rolled back)");
142                 aDataBean = home2.create(223, "JMS Sample KO", 0);
143             }
144             utx.rollback();
145         } catch (Exception e) {
146             System.err.println("Exception .... : " + e);
147             System.exit(2);
148         }
149
150         System.out.println("If your JMS MsgReceptor windows prints");
151         System.out.println(" received message ======> Hello commit");
152         System.out.println("this test is successful.");
153         System.out.println("You may also check that the record 222 has been");
154         System.out.println("created in the database in the table accountsample");
155         System.out.println("(if the sample was working with the entity bean Account).");
156     }
157 }
Popular Tags