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 java.io.BufferedReader;
20 import java.io.InputStreamReader;
21 import java.nio.charset.Charset;
22 import javax.jms.JMSException;
23 import javax.jms.Session;
24 import javax.jms.Topic;
25 import javax.jms.TopicConnection;
26 import javax.jms.TopicConnectionFactory;
27 import javax.jms.TopicSession;
28 import javax.jms.TopicSubscriber;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32
33
34
35
36
37 public class JMSTopicReceiver extends AbstractJMSReceiver {
38
39
40
41
42
43
44
45
46 public JMSTopicReceiver(final String tcfBindingName, final String topicBindingName, final String username,
47 final String password) {
48 try {
49 final Context ctx = new InitialContext();
50 TopicConnectionFactory topicConnectionFactory;
51 topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName);
52 final TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password);
53 topicConnection.start();
54 final TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
55 final Topic topic = (Topic) ctx.lookup(topicBindingName);
56 final TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
57 topicSubscriber.setMessageListener(this);
58 } catch (final JMSException e) {
59 logger.error("Could not read JMS message.", e);
60 } catch (final NamingException e) {
61 logger.error("Could not read JMS message.", e);
62 } catch (final RuntimeException e) {
63 logger.error("Could not read JMS message.", e);
64 }
65 }
66
67
68
69
70
71
72 public static void main(final String[] args) throws Exception {
73 if (args.length != 4) {
74 usage("Wrong number of arguments.");
75 }
76
77 final String tcfBindingName = args[0];
78 final String topicBindingName = args[1];
79 final String username = args[2];
80 final String password = args[3];
81
82 new JMSTopicReceiver(tcfBindingName, topicBindingName, username, password);
83
84 final Charset enc = Charset.defaultCharset();
85 final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
86
87 System.out.println("Type \"exit\" to quit JMSTopicReceiver.");
88 while (true) {
89 final String line = stdin.readLine();
90 if (line == null || line.equalsIgnoreCase("exit")) {
91 System.out.println("Exiting. Kill the application if it does not exit "
92 + "due to daemon threads.");
93 return;
94 }
95 }
96 }
97
98 private static void usage(final String msg) {
99 System.err.println(msg);
100 System.err.println("Usage: java " + JMSTopicReceiver.class.getName()
101 + " TopicConnectionFactoryBindingName TopicBindingName username password");
102 System.exit(1);
103 }
104 }