2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.net;
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;
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;
41 * A simple application that consumes logging events sent by a {@link
45 * @author Ceki Gülcü
47 public class JMSSink implements javax.jms.MessageListener {
49 static Logger logger = Logger.getLogger(JMSSink.class);
51 static public void main(String[] args) throws Exception {
52 if(args.length != 5) {
53 usage("Wrong number of arguments.");
56 String tcfBindingName = args[0];
57 String topicBindingName = args[1];
58 String username = args[2];
59 String password = args[3];
62 String configFile = args[4];
64 if(configFile.endsWith(".xml")) {
65 DOMConfigurator.configure(configFile);
67 PropertyConfigurator.configure(configFile);
70 new JMSSink(tcfBindingName, topicBindingName, username, password);
72 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
73 // Loop until the word "exit" is typed
74 System.out.println("Type \"exit\" to quit JMSSink.");
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.");
85 public JMSSink( String tcfBindingName, String topicBindingName, String username,
89 Context ctx = new InitialContext();
90 TopicConnectionFactory topicConnectionFactory;
91 topicConnectionFactory = (TopicConnectionFactory) lookup(ctx,
94 TopicConnection topicConnection =
95 topicConnectionFactory.createTopicConnection(username,
97 topicConnection.start();
99 TopicSession topicSession = topicConnection.createTopicSession(false,
100 Session.AUTO_ACKNOWLEDGE);
102 Topic topic = (Topic)ctx.lookup(topicBindingName);
104 TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
106 topicSubscriber.setMessageListener(this);
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);
117 public void onMessage(javax.jms.Message message) {
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);
128 logger.warn("Received message is of type "+message.getJMSType()
129 +", was expecting ObjectMessage.");
131 } catch(JMSException jmse) {
132 logger.error("Exception thrown while processing incoming message.",
138 protected static Object lookup(Context ctx, String name) throws NamingException {
140 return ctx.lookup(name);
141 } catch(NameNotFoundException e) {
142 logger.error("Could not find name ["+name+"].");
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");