JMS interop examples updated for common code / logging.
[stompngo_examples.git] / jinterop / artemis / Sender.java
blob0096347e19fbf2a17f9a6689bc2094b8e51791e2
1 //
2 // Copyright © 2012-2016 Guy M. Allard
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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;
30 /**
31 * Sends messages on the queue.
33 public class Sender
36 public static void main(String[] args) throws Exception
38 System.out.println("Sends messages on the queue...");
39 new Sender().go();
40 } // end of main
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");
47 ictx.close();
49 QueueConnection qconn = qcf.createQueueConnection();
50 QueueSession qsess = qconn.createQueueSession(true, 0);
52 QueueSender qsend = qsess.createSender(queue);
53 TextMessage msg = qsess.createTextMessage();
55 int i;
56 for (i = 0; i < Constants.NUM_MESSAGES; i++) {
57 if (Constants.DO_WORK_WAIT)
59 try
61 Thread.sleep(Constants.WORK_WAIT_MS); // Simulate message build
63 catch(InterruptedException ie)
65 // Ignore
68 String s = "Java Sender Test Number " + i;
69 msg.setText(s);
70 qsend.send(msg);
71 System.out.println("Sent: " + s);
72 qsess.commit(); //
74 System.out.println(i + " messages sent.");
75 qconn.close();
77 } // end of go
78 } // end of class Sender