Isolate the ActiveMQ JMS sexamples.
[stompngo_examples.git] / jinterop / activemq / Receiver.java
blobb3802e56118ff832c66186049f1e52dc39812822
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 java.util.Random;
19 import javax.jms.JMSException;
20 import javax.jms.Message;
21 import javax.jms.ObjectMessage;
22 import javax.jms.Queue;
23 import javax.jms.QueueConnection;
24 import javax.jms.QueueConnectionFactory;
25 import javax.jms.QueueReceiver;
26 import javax.jms.QueueSession;
27 import javax.jms.TextMessage;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
33 /**
34 * Requests messages on the queue.
36 public class Receiver
39 public static void main(String[] args) throws Exception
41 System.out.println("Requests to receive messages...");
42 new Receiver().go();
43 } // end of main
46 private void go() throws NamingException, JMSException
48 Random gener = new Random(System.currentTimeMillis());
49 Context ictx = new InitialContext();
50 Queue queue = (Queue) ictx.lookup("Guys");
51 QueueConnectionFactory qcf = (QueueConnectionFactory) ictx.lookup("ConnectionFactory");
52 ictx.close();
54 QueueConnection qconn = qcf.createQueueConnection();
55 QueueSession qsess = qconn.createQueueSession(true, 0);
56 QueueReceiver qrec = qsess.createReceiver(queue);
57 Message msg;
59 qconn.start();
60 System.out.println("Waits are for " + Constants.RECEIVE_WAIT + "(ms)");
61 int i;
62 // for (i = 0; i < 10; i++) {
63 for (i = 0;; i++) {
64 // msg = qrec.receive();
65 msg = qrec.receive(Constants.RECEIVE_WAIT);
66 if (msg == null) break;
67 if (msg instanceof TextMessage)
69 System.out.println("Text Msg received: " + ((TextMessage) msg).getText());
71 else if (msg instanceof ObjectMessage)
73 System.out.println("Object Msg received: "
74 + ((ObjectMessage) msg).getObject());
76 else
78 System.out.println("Some Other Msg received: " + msg);
80 int nextWait = Constants.MIN_WORK + gener.nextInt(Constants.WORK_SPAN);
81 System.out.println("Next Input Process Wait Length: " + nextWait);
82 try
84 Thread.sleep(nextWait); // process work
86 catch(InterruptedException ie)
91 qsess.commit();
92 System.out.println(i + " messages received.");
93 qconn.close();
96 } // end of class Receiver