1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.net;
15
16 import java.io.BufferedReader;
17 import java.io.InputStreamReader;
18 import java.util.Properties;
19
20 import javax.jms.JMSException;
21 import javax.jms.ObjectMessage;
22 import javax.jms.Session;
23 import javax.jms.Topic;
24 import javax.jms.TopicConnection;
25 import javax.jms.TopicConnectionFactory;
26 import javax.jms.TopicSession;
27 import javax.jms.TopicSubscriber;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NameNotFoundException;
31 import javax.naming.NamingException;
32
33 import org.slf4j.LoggerFactory;
34
35 import ch.qos.logback.classic.Logger;
36 import ch.qos.logback.classic.LoggerContext;
37 import ch.qos.logback.classic.spi.ILoggingEvent;
38 import ch.qos.logback.classic.util.ContextInitializer;
39
40
41
42
43
44
45
46 public class JMSTopicSink implements javax.jms.MessageListener {
47
48 private Logger logger = (Logger)LoggerFactory.getLogger(JMSTopicSink.class);
49
50 static public void main(String[] args) throws Exception {
51 if (args.length < 2) {
52 usage("Wrong number of arguments.");
53 }
54
55 String tcfBindingName = args[0];
56 String topicBindingName = args[1];
57 String username = null;
58 String password = null;
59 if (args.length == 4) {
60 username = args[2];
61 password = args[3];
62 }
63
64 LoggerContext loggerContext = (LoggerContext) LoggerFactory
65 .getILoggerFactory();
66 new ContextInitializer(loggerContext).autoConfig();
67
68 new JMSTopicSink(tcfBindingName, topicBindingName, username, password);
69
70 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
71
72 System.out.println("Type \"exit\" to quit JMSTopicSink.");
73 while (true) {
74 String s = stdin.readLine();
75 if (s.equalsIgnoreCase("exit")) {
76 System.out.println("Exiting. Kill the application if it does not exit "
77 + "due to daemon threads.");
78 return;
79 }
80 }
81 }
82
83 public JMSTopicSink(String tcfBindingName, String topicBindingName,
84 String username, String password) {
85
86 try {
87 Properties env = new Properties();
88 env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
89 env.put(Context.PROVIDER_URL, "tcp://localhost:61616");
90 Context ctx = new InitialContext(env);
91 TopicConnectionFactory topicConnectionFactory;
92 topicConnectionFactory = (TopicConnectionFactory) lookup(ctx,
93 tcfBindingName);
94 System.out.println("Topic Cnx Factory found");
95 Topic topic = (Topic) ctx.lookup(topicBindingName);
96 System.out.println("Topic found: " + topic.getTopicName());
97
98 TopicConnection topicConnection = topicConnectionFactory
99 .createTopicConnection(username, password);
100 System.out.println("Topic Connection created");
101
102 TopicSession topicSession = topicConnection.createTopicSession(false,
103 Session.AUTO_ACKNOWLEDGE);
104
105 TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
106
107 topicSubscriber.setMessageListener(this);
108
109 topicConnection.start();
110 System.out.println("Topic Connection started");
111
112 } catch (Exception e) {
113 logger.error("Could not read JMS message.", e);
114 }
115 }
116
117 public void onMessage(javax.jms.Message message) {
118 ILoggingEvent event;
119 try {
120 if (message instanceof ObjectMessage) {
121 ObjectMessage objectMessage = (ObjectMessage) message;
122 event = (ILoggingEvent) objectMessage.getObject();
123 Logger log = (Logger) LoggerFactory.getLogger(event.getLoggerName());
124 log.callAppenders(event);
125 } else {
126 logger.warn("Received message is of type " + message.getJMSType()
127 + ", was expecting ObjectMessage.");
128 }
129 } catch (JMSException jmse) {
130 logger.error("Exception thrown while processing incoming message.", jmse);
131 }
132 }
133
134 protected Object lookup(Context ctx, String name)
135 throws NamingException {
136 try {
137 return ctx.lookup(name);
138 } catch (NameNotFoundException e) {
139 logger.error("Could not find name [" + name + "].");
140 throw e;
141 }
142 }
143
144 static void usage(String msg) {
145 System.err.println(msg);
146 System.err
147 .println("Usage: java "
148 + JMSTopicSink.class.getName()
149 + " TopicConnectionFactoryBindingName TopicBindingName Username Password");
150 System.exit(1);
151 }
152 }