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