1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 /***
41 * The SimpleQueueSender class consists only of a main method,
42 * which sends several messages to a queue.
43 *
44 * Run this program in conjunction with SimpleQueueReceiver.
45 * Specify a queue name on the command line when you run the
46 * program. By default, the program sends one message. Specify
47 * a number after the queue name to send that number of messages.
48 */
49 package org.codehaus.activemq.demo;
50
51
52
53
54 import javax.jms.JMSException;
55 import javax.jms.Queue;
56 import javax.jms.QueueConnection;
57 import javax.jms.QueueConnectionFactory;
58 import javax.jms.QueueSender;
59 import javax.jms.QueueSession;
60 import javax.jms.Session;
61 import javax.jms.TextMessage;
62 import javax.naming.Context;
63 import javax.naming.InitialContext;
64 import javax.naming.NamingException;
65
66 public class SimpleQueueSender {
67
68 /***
69 * Main method.
70 *
71 * @param args the queue used by the example and,
72 * optionally, the number of messages to send
73 */
74 public static void main(String[] args) {
75 String queueName = null;
76 Context jndiContext = null;
77 QueueConnectionFactory queueConnectionFactory = null;
78 QueueConnection queueConnection = null;
79 QueueSession queueSession = null;
80 Queue queue = null;
81 QueueSender queueSender = null;
82 TextMessage message = null;
83 final int NUM_MSGS;
84
85 if ((args.length < 1) || (args.length > 2)) {
86 System.out.println("Usage: java SimpleQueueSender " +
87 "<queue-name> [<number-of-messages>]");
88 System.exit(1);
89 }
90 queueName = new String(args[0]);
91 System.out.println("Queue name is " + queueName);
92 if (args.length == 2) {
93 NUM_MSGS = (new Integer(args[1])).intValue();
94 }
95 else {
96 NUM_MSGS = 1;
97 }
98
99
100
101
102
103 try {
104 jndiContext = new InitialContext();
105 }
106 catch (NamingException e) {
107 System.out.println("Could not create JNDI API context: " + e.toString());
108 System.exit(1);
109 }
110
111
112
113
114
115 try {
116 queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
117 queue = (Queue) jndiContext.lookup(queueName);
118 }
119 catch (NamingException e) {
120 System.out.println("JNDI API lookup failed: " + e);
121 System.exit(1);
122 }
123
124
125
126
127
128
129
130
131
132
133 try {
134 queueConnection = queueConnectionFactory.createQueueConnection();
135 queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
136 queueSender = queueSession.createSender(queue);
137 message = queueSession.createTextMessage();
138 for (int i = 0; i < NUM_MSGS; i++) {
139 message.setText("This is message " + (i + 1));
140 System.out.println("Sending message: " +
141 message.getText());
142 queueSender.send(message);
143 }
144
145
146
147
148
149 queueSender.send(queueSession.createMessage());
150 }
151 catch (JMSException e) {
152 System.out.println("Exception occurred: " +
153 e.toString());
154 }
155 finally {
156 if (queueConnection != null) {
157 try {
158 queueConnection.close();
159 }
160 catch (JMSException e) {
161 }
162 }
163 }
164 }
165 }
166
167