KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleJmsXa


1 /*
2  * @(#) SimpleJmsXa.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  *
7  * This module was orginally developed by
8  *
9  * - INRIA (www.inria.fr)inside the ObjectWeb Consortium
10  * (http://www.objectweb.org)
11  *
12  * --------------------------------------------------------------------------
13  * The original code and portions created by INRIA are
14  * Copyright (c) 2002 INRIA
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * -Redistributions of source code must retain the above copyright notice, this
21  * list of conditions and the following disclaimer.
22  *
23  * -Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  *-----------------------------------------------------------------------------
40  * $Id: SimpleJmsXa.java,v 1.3 2003/12/05 20:07:06 trentshue Exp $
41  *-----------------------------------------------------------------------------
42  */

43
44
45 import javax.jms.Queue JavaDoc;
46 import javax.naming.Context JavaDoc;
47 import javax.naming.InitialContext JavaDoc;
48 import javax.naming.NamingException JavaDoc;
49 import javax.transaction.TransactionManager JavaDoc;
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 JavaDoc;
56
57 /**
58  * Created on Mar 4, 2002
59  * @author Christophe Ney - cney@batisseurs.com
60  */

61 public class SimpleJmsXa {
62
63     public static void main(String JavaDoc[] args) {
64
65         // parse args
66
String JavaDoc jmsAdminClassName = "org.objectweb.jonas_jms.JmsAdminForJoram";
67         if (args.length>0) {
68             jmsAdminClassName = args[0];
69         }
70
71         // get initial context
72
Context JavaDoc ctx = null;
73         try {
74             ctx = new InitialContext JavaDoc();
75         } catch (NamingException JavaDoc 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         // get transaction manager
83
TransactionManager JavaDoc tm = null;
84         try {
85             tm = (TransactionManager JavaDoc) ctx.lookup("TransactionManager");
86         } catch (NamingException JavaDoc 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         // load the JMS Resource Adaptor using the JOnAS JmsAdministration SPI
94
// The SPI is made of one interface org.objectweb.jonas_jms.api.JmsAdministration
95
Class JavaDoc cAdmin = null;
96         try {
97             cAdmin = Class.forName(jmsAdminClassName);
98         } catch (ClassNotFoundException JavaDoc e) {
99             System.err.println("Cannot load class "+jmsAdminClassName);
100             e.printStackTrace();
101             System.exit(1);
102         }
103
104         // the MOM is collocated in the VM (could be remote)
105
boolean collocated = true;
106         String JavaDoc url = null;
107
108         // init the JMS Resource Manager using the JOnAS JmsManager API
109
// The API is made of only one interface org.objectweb.jonas_jms.api.JmsManager
110
TraceJms.setVerbose(false);
111         TraceJms.setDebug(false);
112         TraceJms.setLogWriter(new PrintWriter JavaDoc(System.out,true));
113         JmsManager jmsMg = JmsManagerImpl.getJmsManager();
114         try {
115             jmsMg.init(cAdmin, collocated, url, tm);
116         } catch (Exception JavaDoc 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             // Rebind theQuueConnectionFactory for JNDI access
126
// It is similar to binding a datasource for a given driver
127
ctx.rebind("theQueueConnectionFactory", jmsMg.getQueueConnectionFactory());
128
129             // Create and rebind theQueue
130
// It is similar to creating a TABLE in a database, but the name
131
// space is used to store the name
132
Queue JavaDoc queue = jmsMg.createQueue("theQueue");
133             ctx.rebind("theQueue", queue);
134
135         } catch (NamingException JavaDoc e) {
136             System.err.println("Cannot rebind JMS objects");
137             e.printStackTrace();
138             System.exit(1);
139         } catch (Exception JavaDoc 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         // send a few messages on theQueue (current thread for now)
148
new SimpleSender().run();
149
150         System.out.println("[SimpleJmsXa] start simple receiver.");
151         // receive messages found in theQueue (SimpleReceiver is a MessageListener)
152
new SimpleReceiver().run();
153
154         // stop the JMS Manager
155
try {
156             ctx.unbind("theQueueConnectionFactory");
157             ctx.unbind("theQueue");
158             jmsMg.stop();
159         } catch (Exception JavaDoc 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         // stop the VM since ReferenceWarpper used for binding Reference in JNDI
167
// are not unexported : jdk bug
168
System.exit(0);
169     }
170 }
171
Popular Tags