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 import javax.jms.Connection;
54 import javax.jms.ConnectionFactory;
55 import javax.jms.Destination;
56 import javax.jms.JMSException;
57 import javax.jms.MessageProducer;
58 import javax.jms.Session;
59 import javax.jms.TextMessage;
60 import javax.naming.Context;
61 import javax.naming.InitialContext;
62 import javax.naming.NamingException;
63
64 /***
65 * A simple polymorphic JMS producer which can work with Queues or Topics
66 * which uses JNDI to lookup the JMS connection factory and destination
67 *
68 * @version $Revision: 1.2 $
69 */
70 public class SimpleProducer {
71
72 /***
73 * @param args the destination name to send to and optionally, the number of messages to send
74 */
75 public static void main(String[] args) {
76 Context jndiContext = null;
77 ConnectionFactory connectionFactory = null;
78 Connection connection = null;
79 Session session = null;
80 Destination destination = null;
81 MessageProducer producer = null;
82 String destinationName = null;
83 final int NUM_MSGS;
84
85 if ((args.length < 1) || (args.length > 2)) {
86 System.out.println("Usage: java SimpleProducer <destination-name> [<number-of-messages>]");
87 System.exit(1);
88 }
89 destinationName = new String(args[0]);
90 System.out.println("Destination name is " + destinationName);
91 if (args.length == 2) {
92 NUM_MSGS = (new Integer(args[1])).intValue();
93 }
94 else {
95 NUM_MSGS = 1;
96 }
97
98
99
100
101 try {
102 jndiContext = new InitialContext();
103 }
104 catch (NamingException e) {
105 System.out.println("Could not create JNDI API context: " + e.toString());
106 System.exit(1);
107 }
108
109
110
111
112 try {
113 connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
114 destination = (Destination) jndiContext.lookup(destinationName);
115 }
116 catch (NamingException e) {
117 System.out.println("JNDI API lookup failed: " + e);
118 System.exit(1);
119 }
120
121
122
123
124
125
126
127
128
129 try {
130 connection = connectionFactory.createConnection();
131 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
132 producer = session.createProducer(destination);
133 TextMessage message = session.createTextMessage();
134 for (int i = 0; i < NUM_MSGS; i++) {
135 message.setText("This is message " + (i + 1));
136 System.out.println("Sending message: " + message.getText());
137 producer.send(message);
138 }
139
140
141
142
143 producer.send(session.createMessage());
144 }
145 catch (JMSException e) {
146 System.out.println("Exception occurred: " + e);
147 }
148 finally {
149 if (connection != null) {
150 try {
151 connection.close();
152 }
153 catch (JMSException e) {
154 }
155 }
156 }
157 }
158 }
159
160