2 // Copyright © 2012-2018 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 java
.util
.Random
;
19 import javax
.jms
.JMSException
;
20 import javax
.jms
.Message
;
21 import javax
.jms
.ObjectMessage
;
22 import javax
.jms
.BytesMessage
;
23 import javax
.jms
.MapMessage
;
24 import javax
.jms
.StreamMessage
;
25 import javax
.jms
.Queue
;
26 import javax
.jms
.QueueConnection
;
27 import javax
.jms
.QueueConnectionFactory
;
28 import javax
.jms
.QueueReceiver
;
29 import javax
.jms
.QueueSession
;
30 import javax
.jms
.TextMessage
;
31 import javax
.naming
.Context
;
32 import javax
.naming
.InitialContext
;
33 import javax
.naming
.NamingException
;
37 * Requests messages on the queue.
42 public static void main(String
[] args
) throws Exception
44 System
.out
.println("Requests to receive messages...");
49 private void go() throws NamingException
, JMSException
51 Random gener
= new Random(System
.currentTimeMillis());
53 Context ictx
= new InitialContext();
54 QueueConnectionFactory qcf
= (QueueConnectionFactory
) ictx
.lookup("ConnectionFactory");
55 Queue queue
= (Queue
) ictx
.lookup("queue/exampleQueue");
58 QueueConnection qconn
= qcf
.createQueueConnection();
59 QueueSession qsess
= qconn
.createQueueSession(true, 0);
61 QueueReceiver qrec
= qsess
.createReceiver(queue
);
65 System
.out
.println("Waits are for " + Constants
.RECEIVE_WAIT
+ "(ms)");
67 // for (i = 0; i < 10; i++) {
69 // msg = qrec.receive();
70 msg
= qrec
.receive(Constants
.RECEIVE_WAIT
);
71 if (msg
== null) break;
72 if (msg
instanceof TextMessage
)
74 System
.out
.println("Text Msg received: " + ((TextMessage
) msg
).getText());
76 else if (msg
instanceof ObjectMessage
)
78 System
.out
.println("Object Msg received: "
79 + ((ObjectMessage
) msg
).getObject());
81 else if (msg
instanceof BytesMessage
)
83 BytesMessage bmsg
= (BytesMessage
)msg
;
84 long len
= bmsg
.getBodyLength();
85 byte[] buff
= new byte[(int)len
];
86 int rlen
= bmsg
.readBytes(buff
);
87 String body
= new String(buff
);
88 System
.out
.println("Bytes Msg received: "
89 + "len:" + rlen
+ "\n" + "body:" + body
);
91 else if (msg
instanceof StreamMessage
)
93 System
.out
.println("Stream Msg received: ");
94 System
.out
.println(msg
);
96 else if (msg
instanceof MapMessage
)
98 System
.out
.print("Map Msg received: ");
99 System
.out
.println(msg
);
104 System
.out
.println("Some Other Msg received: " + msg
);
106 int nextWait
= Constants
.MIN_WORK
+ gener
.nextInt(Constants
.WORK_SPAN
);
107 System
.out
.println("Next Input Process Wait Length: " + nextWait
);
110 Thread
.sleep(nextWait
); // process work
112 catch(InterruptedException ie
)
118 System
.out
.println(i
+ " messages received.");
122 } // end of class Receiver