1 28 29 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 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 try { 61 initialContext = new InitialContext(); 62 } catch (NamingException e) { 63 e.printStackTrace(); 64 System.exit(2); 65 } 66 67 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 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 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 System.out.println("Sample will work without entity bean Account"); 92 home2 = null; 93 } 94 95 try { 97 if (home2 != null) { Account b = home2.findByNumber(222); 99 b.remove(); 100 } 101 } catch (Exception e) { 102 } 103 104 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 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 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 |