View Javadoc

1   /*
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
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
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.
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   * A simple application that consumes logging events sent by a {@link
42   * JMSAppender}.
43   *
44   *
45   * @author Ceki Gülcü 
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      // Loop until the word "exit" is typed
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(Exception e) {
109       logger.error("Could not read JMS message.", e);
110     }
111   }
112 
113   public void onMessage(javax.jms.Message message) {
114     LoggingEvent event;
115     Logger remoteLogger;
116 
117     try {
118       if(message instanceof  ObjectMessage) {
119 	ObjectMessage objectMessage = (ObjectMessage) message;
120 	event = (LoggingEvent) objectMessage.getObject();
121 	remoteLogger = Logger.getLogger(event.getLoggerName());
122 	remoteLogger.callAppenders(event);
123       } else {
124 	logger.warn("Received message is of type "+message.getJMSType()
125 		    +", was expecting ObjectMessage.");
126       }      
127     } catch(JMSException jmse) {
128       logger.error("Exception thrown while processing incoming message.", 
129 		   jmse);
130     }
131   }
132 
133 
134   protected static Object lookup(Context ctx, String name) throws NamingException {
135     try {
136       return ctx.lookup(name);
137     } catch(NameNotFoundException e) {
138       logger.error("Could not find name ["+name+"].");
139       throw e;
140     }
141   }
142 
143   static void usage(String msg) {
144     System.err.println(msg);
145     System.err.println("Usage: java " + JMSSink.class.getName()
146             + " TopicConnectionFactoryBindingName TopicBindingName username password configFile");
147     System.exit(1);
148   }
149 }