001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): ____________________________________.
022: * Contributor(s): Fran?ois Exertier
023: *
024: * --------------------------------------------------------------------------
025: * $Id: EjbCompClient.java 4618 2004-04-19 06:39:30Z benoitf $
026: * --------------------------------------------------------------------------
027: */package jms;
028:
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.rmi.PortableRemoteObject;
033: import javax.transaction.UserTransaction;
034:
035: import eb.Account;
036: import eb.AccountHome;
037:
038: /**
039: *
040: */
041: public class EjbCompClient {
042:
043: private static Context initialContext = null;
044:
045: private static EjbCompHome home1 = null;
046:
047: private static AccountHome home2 = null;
048:
049: private static UserTransaction utx = null;
050:
051: public static void main(String[] arg) {
052:
053: // Get InitialContext
054: try {
055: initialContext = new InitialContext();
056: } catch (NamingException e) {
057: e.printStackTrace();
058: System.exit(2);
059: }
060:
061: // For starting transactions from client: get UserTransaction
062: System.out
063: .println("Getting a UserTransaction object from JNDI");
064: try {
065: utx = (UserTransaction) initialContext
066: .lookup("javax.transaction.UserTransaction");
067: } catch (Exception e) {
068: System.err.println("Cannot lookup UserTransaction: " + e);
069: System.exit(2);
070: }
071:
072: // Lookup EjbComp bean home
073: try {
074: home1 = (EjbCompHome) PortableRemoteObject.narrow(
075: initialContext.lookup("EjbCompHome"),
076: EjbCompHome.class);
077: } catch (Exception e) {
078: System.err.println("Cannot lookup EjbCompHome: " + e);
079: System.exit(2);
080: }
081:
082: // Lookup Account bean home
083: try {
084: home2 = (AccountHome) PortableRemoteObject.narrow(
085: initialContext.lookup("AccountImplHome"),
086: AccountHome.class);
087: } catch (Exception e) {
088: System.err.println("Cannot lookup AccountImplHome: " + e);
089: // no entity bean to use for running the jms sample
090: System.out
091: .println("Sample will work without entity bean Account");
092: home2 = null;
093: }
094:
095: // Delete account that may have been created by a previous run
096: try {
097: if (home2 != null) { // use entity bean account in the sample
098: Account b = home2.findByNumber(222);
099: 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
124: .println("Creating an Account bean (will be commited)");
125: aDataBean = home2.create(222, "JMS Sample OK", 0);
126: }
127: utx.commit();
128: } catch (Exception e) {
129: System.err.println("Exception .... : " + e);
130: e.printStackTrace();
131: System.exit(2);
132: }
133:
134: // Calling EjbComp method that sends a message and eventually creating
135: // the corresponding account (i.e. in the database), in a
136: // transaction that fails !
137: try {
138: utx.begin();
139: System.out
140: .println("Sending a message (will be rolled back)");
141: aJmsBean.sendMsg("Hello rollback");
142: if (home2 != null) {
143: System.out
144: .println("Creating a Account bean (will be rolled back)");
145: aDataBean = home2.create(223, "JMS Sample KO", 0);
146: }
147: utx.rollback();
148: } catch (Exception e) {
149: System.err.println("Exception .... : " + e);
150: System.exit(2);
151: }
152:
153: System.out.println("If your JMS MsgReceptor windows prints");
154: System.out
155: .println(" received message ======> Hello commit");
156: System.out.println("this test is successful.");
157: System.out
158: .println("You may also check that the record 222 has been");
159: System.out
160: .println("created in the database in the table accountsample");
161: System.out
162: .println("(if the sample was working with the entity bean Account).");
163: }
164: }
|