2 // Copyright © 2012-2016 Guy M. Allard
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 import javax
.jms
.JMSException
;
18 import javax
.jms
.Queue
;
19 import javax
.jms
.QueueConnection
;
20 import javax
.jms
.QueueConnectionFactory
;
21 import javax
.jms
.QueueSender
;
22 import javax
.jms
.QueueSession
;
23 import javax
.jms
.TextMessage
;
24 import javax
.naming
.Context
;
25 import javax
.naming
.InitialContext
;
26 import javax
.naming
.NamingException
;
28 import javax
.jms
.Session
;
31 * Sends messages on the queue.
36 public static void main(String
[] args
) throws Exception
38 System
.out
.println("Sends messages on the queue...");
42 private void go() throws NamingException
, JMSException
44 Context ictx
= new InitialContext();
45 QueueConnectionFactory qcf
= (QueueConnectionFactory
) ictx
.lookup("ConnectionFactory");
46 Queue queue
= (Queue
) ictx
.lookup("queue/exampleQueue");
49 QueueConnection qconn
= qcf
.createQueueConnection();
50 QueueSession qsess
= qconn
.createQueueSession(true, 0);
52 QueueSender qsend
= qsess
.createSender(queue
);
53 TextMessage msg
= qsess
.createTextMessage();
56 for (i
= 0; i
< Constants
.NUM_MESSAGES
; i
++) {
57 if (Constants
.DO_WORK_WAIT
)
61 Thread
.sleep(Constants
.WORK_WAIT_MS
); // Simulate message build
63 catch(InterruptedException ie
)
68 String s
= "Java Sender Test Number " + i
;
71 System
.out
.println("Sent: " + s
);
74 System
.out
.println(i
+ " messages sent.");
78 } // end of class Sender