1 43 44 45 import javax.jms.Queue ; 46 import javax.naming.Context ; 47 import javax.naming.InitialContext ; 48 import javax.naming.NamingException ; 49 import javax.transaction.TransactionManager ; 50 51 import org.objectweb.jonas_jms.api.JmsManager; 52 import org.objectweb.jonas_jms.JmsManagerImpl; 53 import org.objectweb.jonas_jms.TraceJms; 54 55 import java.io.PrintWriter ; 56 57 61 public class SimpleJmsXa { 62 63 public static void main(String [] args) { 64 65 String jmsAdminClassName = "org.objectweb.jonas_jms.JmsAdminForJoram"; 67 if (args.length>0) { 68 jmsAdminClassName = args[0]; 69 } 70 71 Context ctx = null; 73 try { 74 ctx = new InitialContext (); 75 } catch (NamingException e) { 76 System.err.println("No Initial Context"); 77 e.printStackTrace(); 78 System.exit(1); 79 } 80 81 System.out.println("[SimpleJmsXa] lookup the TransactionManager."); 82 TransactionManager tm = null; 84 try { 85 tm = (TransactionManager ) ctx.lookup("TransactionManager"); 86 } catch (NamingException e) { 87 System.err.println("TransactionManager not found"); 88 e.printStackTrace(); 89 System.exit(1); 90 } 91 92 System.out.println("[SimpleJmsXa] start the JMS server."); 93 Class cAdmin = null; 96 try { 97 cAdmin = Class.forName(jmsAdminClassName); 98 } catch (ClassNotFoundException e) { 99 System.err.println("Cannot load class "+jmsAdminClassName); 100 e.printStackTrace(); 101 System.exit(1); 102 } 103 104 boolean collocated = true; 106 String url = null; 107 108 TraceJms.setVerbose(false); 111 TraceJms.setDebug(false); 112 TraceJms.setLogWriter(new PrintWriter (System.out,true)); 113 JmsManager jmsMg = JmsManagerImpl.getJmsManager(); 114 try { 115 jmsMg.init(cAdmin, collocated, url, tm); 116 } catch (Exception e) { 117 System.err.println("Cannot initialize JmsManager"); 118 e.printStackTrace(); 119 System.exit(1); 120 } 121 System.out.println("[SimpleJmsXa] JMS server started."); 122 123 System.out.println("[SimpleJmsXa] create JMS objects, register them in JOTM and bind them."); 124 try { 125 ctx.rebind("theQueueConnectionFactory", jmsMg.getQueueConnectionFactory()); 128 129 Queue queue = jmsMg.createQueue("theQueue"); 133 ctx.rebind("theQueue", queue); 134 135 } catch (NamingException e) { 136 System.err.println("Cannot rebind JMS objects"); 137 e.printStackTrace(); 138 System.exit(1); 139 } catch (Exception e) { 140 System.err.println("Cannot create Queue"); 141 e.printStackTrace(); 142 System.exit(1); 143 } 144 System.out.println("[SimpleJmsXa] JMS objects available."); 145 146 System.out.println("[SimpleJmsXa] start simple sender."); 147 new SimpleSender().run(); 149 150 System.out.println("[SimpleJmsXa] start simple receiver."); 151 new SimpleReceiver().run(); 153 154 try { 156 ctx.unbind("theQueueConnectionFactory"); 157 ctx.unbind("theQueue"); 158 jmsMg.stop(); 159 } catch (Exception e) { 160 System.err.println("Error while shutting down"); 161 e.printStackTrace(); 162 System.exit(1); 163 } 164 165 System.out.println("[SimpleJmsXa] JMS server stopped"); 166 System.exit(0); 169 } 170 } 171 | Popular Tags |