Biblioteca Java - Blame information for rev 6

Subversion Repositories:
Rev:
Rev Author Line No. Line
6 mihai 1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5  
6 package mqdemo;
7  
8 /*
9  * @(#)HelloWorldMessage.java   1.7 04/09/15
10  *
11  * Copyright (c) 2000-2002 Sun Microsystems, Inc. All Rights Reserved.
12  *
13  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
14  * modify and redistribute this software in source and binary code form,
15  * provided that i) this copyright notice and license appear on all copies of
16  * the software; and ii) Licensee does not utilize the software in a manner
17  * which is disparaging to Sun.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
21  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
22  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
23  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
24  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
25  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
26  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
27  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
28  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGES.
30  *
31  * This software is not designed or intended for use in on-line control of
32  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
33  * the design, construction, operation or maintenance of any nuclear
34  * facility. Licensee represents and warrants that it will not use or
35  * redistribute the Software for such purposes.
36  */
37  
38  
39 /**
40  * The HelloWorldMessage class consists only of a main method, which sends
41  * a message to a queue and then receives the message from the queue.
42  * <p>
43  * This example is used in the "Quick Start Tutorial" of the Sun Java(tm)
44  * System Message Queue Developer's Guide to illustrate a very simple JMS
45  * client.
46  * The line comments associate the lines of code with the steps in the tutorial.
47  *
48  * @version 1.7 04/09/15
49  */
50  
51 //Step 1:
52 //Import the JMS API classes.
53 import javax.jms.QueueConnectionFactory;
54 import javax.jms.QueueConnection;
55 import javax.jms.QueueSession;
56 import javax.jms.QueueSender;
57 import javax.jms.QueueReceiver;
58 import javax.jms.Queue;
59 import javax.jms.Session;
60 import javax.jms.Message;
61 import javax.jms.TextMessage;
62 //Import the classes to use JNDI.
63 import javax.naming.*;
64 import java.util.*;
65  
66 public class HelloWorldMessage {
67  
68     /**
69      * Main method.
70      *
71      * @param args      not used
72      *
73      */
74     public static void main(String[] args) {
75  
76         try {
77  
78             QueueConnectionFactory myQConnFactory;
79             Queue myQueue;
80  
81             /*
82              * The following code uses the JNDI File System Service Provider
83              * to lookup() Administered Objects that were stored in the
84              * Administration Console Tutorial in the Administrator's Guide
85              *
86              * The following code (in this comment block replaces the
87              * statements in Steps 2 and 5 of this example.
88              *
89              ****
90                 String MYQCF_LOOKUP_NAME = "MyQueueConnectionFactory";
91                 String MYQUEUE_LOOKUP_NAME = "MyQueue";
92  
93                 Hashtable env;
94                 Context ctx = null;
95  
96                 env = new Hashtable();
97  
98                 // Store the environment variable that tell JNDI which initial context
99                 // to use and where to find the provider.
100  
101                 // For use with the File System JNDI Service Provider
102                 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
103                 // On Unix, use file:///tmp instead of file:///C:/Temp
104                 env.put(Context.PROVIDER_URL, "file:///C:/Temp");
105  
106                 // Create the initial context.
107                 ctx = new InitialContext(env);
108  
109                 // Lookup my connection factory from the admin object store.
110                 // The name used here here must match the lookup name
111                 // used when the admin object was stored.
112                 myQConnFactory = (javax.jms.QueueConnectionFactory) ctx.lookup(MYQCF_LOOKUP_NAME);
113  
114                 // Lookup my queue from the admin object store.
115                 // The name I search for here must match the lookup name used when
116                 // the admin object was stored.
117                 myQueue = (javax.jms.Queue)ctx.lookup(MYQUEUE_LOOKUP_NAME);
118             ****
119             *
120             */
121  
122             //Step 2:
123             //Instantiate a Sun Java(tm) System Message Queue QueueConnectionFactory
124             //administered object.
125             //This statement can be eliminated if the JNDI code above is used.
126             myQConnFactory = new  com.sun.messaging.QueueConnectionFactory();
127  
128             //Step 3:
129             //Create a connection to the Sun Java(tm) System Message Queue Message
130             //Service.
131             QueueConnection myQConn = myQConnFactory.createQueueConnection();
132  
133  
134             //Step 4:
135             //Create a session within the connection.
136             QueueSession myQSess = myQConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
137  
138  
139             //Step 5:
140             //Instantiate a Sun Java(tm) System Message Queue Destination
141             //administered object.
142             //This statement can be eliminated if the JNDI code above is used.
143             myQueue = new com.sun.messaging.Queue("world");
144  
145  
146             //Step 6:
147             //Create a QueueSender message producer.
148             QueueSender myQueueSender = myQSess.createSender(myQueue);
149  
150  
151             //Step 7:
152             //Create and send a message to the queue.
153             TextMessage myTextMsg = myQSess.createTextMessage();
154             myTextMsg.setText("Hello World");
155             System.out.println("Sending Message: " + myTextMsg.getText());
156             for(int i=0;i<4;i++)
157              myQueueSender.send(myTextMsg);
158  
159  
160             //Step 8:
161             //Create a QueueReceiver message consumer.
162             QueueReceiver myQueueReceiver = myQSess.createReceiver(myQueue);
163  
164  
165             //Step 9:
166             //Start the QueueConnection created in step 3.
167             myQConn.start();
168  
169  
170             //Step 10:
171             //Receive a message from the queue.
172             Message msg = myQueueReceiver.receive();
173  
174  
175             //Step 11:
176             //Retreive the contents of the message.
177             if (msg instanceof TextMessage) {
178                 TextMessage txtMsg = (TextMessage) msg;
179                 System.out.println("Read Message: " + txtMsg.getText());
180             }
181  
182  
183             //Step 12:
184             //Close the session and connection resources.
185             myQSess.close();
186             myQConn.close();
187  
188         } catch (Exception jmse) {
189             System.out.println("Exception occurred : " + jmse.toString());
190             jmse.printStackTrace();
191         }
192     }
193 }