1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.net;
18
19 import javax.jms.JMSException;
20 import javax.jms.Queue;
21 import javax.jms.QueueConnection;
22 import javax.jms.QueueConnectionFactory;
23 import javax.jms.QueueReceiver;
24 import javax.jms.QueueSession;
25 import javax.jms.Session;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import java.io.BufferedReader;
30 import java.io.InputStreamReader;
31
32
33
34
35
36 public class JMSQueueReceiver extends AbstractJMSReceiver {
37
38
39
40
41
42
43
44
45 public JMSQueueReceiver(String qcfBindingName, String queueBindingName, String username, String password) {
46
47 try {
48 Context ctx = new InitialContext();
49 QueueConnectionFactory queueConnectionFactory;
50 queueConnectionFactory = (QueueConnectionFactory) lookup(ctx, qcfBindingName);
51 QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(username, password);
52 queueConnection.start();
53 QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
54 Queue queue = (Queue) ctx.lookup(queueBindingName);
55 QueueReceiver queueReceiver = queueSession.createReceiver(queue);
56 queueReceiver.setMessageListener(this);
57 } catch (JMSException e) {
58 logger.error("Could not read JMS message.", e);
59 } catch (NamingException e) {
60 logger.error("Could not read JMS message.", e);
61 } catch (RuntimeException e) {
62 logger.error("Could not read JMS message.", e);
63 }
64 }
65
66
67
68
69
70
71 public static void main(String[] args) throws Exception {
72 if (args.length != 4) {
73 usage("Wrong number of arguments.");
74 }
75
76 String qcfBindingName = args[0];
77 String queueBindingName = args[1];
78 String username = args[2];
79 String password = args[3];
80
81 new JMSQueueReceiver(qcfBindingName, queueBindingName, username, password);
82
83 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
84
85 System.out.println("Type \"exit\" to quit JMSQueueReceiver.");
86 while (true) {
87 String s = stdin.readLine();
88 if (s.equalsIgnoreCase("exit")) {
89 System.out.println("Exiting. Kill the application if it does not exit "
90 + "due to daemon threads.");
91 return;
92 }
93 }
94 }
95
96
97 private static void usage(String msg) {
98 System.err.println(msg);
99 System.err.println("Usage: java " + JMSQueueReceiver.class.getName()
100 + " QueueConnectionFactoryBindingName QueueBindingName username password");
101 System.exit(1);
102 }
103 }