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