KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > net > JMSSink


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.net;
18
19 import org.apache.log4j.spi.LoggingEvent;
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.LogManager;
22 import org.apache.log4j.spi.RendererSupport;
23 import org.apache.log4j.spi.LoggerRepository;
24 import org.apache.log4j.or.jms.MessageRenderer;
25 import org.apache.log4j.PropertyConfigurator;
26 import org.apache.log4j.xml.DOMConfigurator;
27 import org.apache.log4j.helpers.LogLog;
28
29 import javax.jms.Message JavaDoc;
30 import javax.jms.MessageListener JavaDoc;
31 import javax.jms.TopicConnection JavaDoc;
32 import javax.jms.Topic JavaDoc;
33 import javax.jms.TopicConnectionFactory JavaDoc;
34 import javax.jms.TopicSubscriber JavaDoc;
35 import javax.jms.Session JavaDoc;
36 import javax.jms.TopicSession JavaDoc;
37 import javax.jms.ObjectMessage JavaDoc;
38 import javax.jms.JMSException JavaDoc;
39
40 import java.io.BufferedReader JavaDoc;
41 import java.io.InputStreamReader JavaDoc;
42
43 import javax.naming.InitialContext JavaDoc;
44 import javax.naming.Context JavaDoc;
45 import javax.naming.NameNotFoundException JavaDoc;
46 import javax.naming.NamingException JavaDoc;
47 import java.util.Properties JavaDoc;
48
49 /**
50  * A simple application that consumes logging events sent by a {@link
51  * JMSAppender}.
52  *
53  *
54  * @author Ceki Gülcü
55  * */

56 public class JMSSink implements javax.jms.MessageListener JavaDoc {
57
58   static Logger logger = Logger.getLogger(JMSSink.class);
59
60   static public void main(String JavaDoc[] args) throws Exception JavaDoc {
61     if(args.length != 5) {
62       usage("Wrong number of arguments.");
63     }
64     
65     String JavaDoc tcfBindingName = args[0];
66     String JavaDoc topicBindingName = args[1];
67     String JavaDoc username = args[2];
68     String JavaDoc password = args[3];
69     
70     
71     String JavaDoc configFile = args[4];
72
73     if(configFile.endsWith(".xml")) {
74       new DOMConfigurator().configure(configFile);
75     } else {
76       new PropertyConfigurator().configure(configFile);
77     }
78     
79     new JMSSink(tcfBindingName, topicBindingName, username, password);
80
81     BufferedReader JavaDoc stdin = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
82     // Loop until the word "exit" is typed
83
System.out.println("Type \"exit\" to quit JMSSink.");
84     while(true){
85       String JavaDoc s = stdin.readLine( );
86       if (s.equalsIgnoreCase("exit")) {
87     System.out.println("Exiting. Kill the application if it does not exit "
88                + "due to daemon threads.");
89     return;
90       }
91     }
92   }
93
94   public JMSSink( String JavaDoc tcfBindingName, String JavaDoc topicBindingName, String JavaDoc username,
95           String JavaDoc password) {
96     
97     try {
98       Context JavaDoc ctx = new InitialContext JavaDoc();
99       TopicConnectionFactory JavaDoc topicConnectionFactory;
100       topicConnectionFactory = (TopicConnectionFactory JavaDoc) lookup(ctx,
101                                                                tcfBindingName);
102
103       TopicConnection JavaDoc topicConnection =
104                             topicConnectionFactory.createTopicConnection(username,
105                                          password);
106       topicConnection.start();
107
108       TopicSession JavaDoc topicSession = topicConnection.createTopicSession(false,
109                                                        Session.AUTO_ACKNOWLEDGE);
110
111       Topic JavaDoc topic = (Topic JavaDoc)ctx.lookup(topicBindingName);
112
113       TopicSubscriber JavaDoc topicSubscriber = topicSession.createSubscriber(topic);
114     
115       topicSubscriber.setMessageListener(this);
116
117     } catch(Exception JavaDoc e) {
118       logger.error("Could not read JMS message.", e);
119     }
120   }
121
122   public void onMessage(javax.jms.Message JavaDoc message) {
123     LoggingEvent event;
124     Logger remoteLogger;
125
126     try {
127       if(message instanceof ObjectMessage JavaDoc) {
128     ObjectMessage JavaDoc objectMessage = (ObjectMessage JavaDoc) message;
129     event = (LoggingEvent) objectMessage.getObject();
130     remoteLogger = Logger.getLogger(event.getLoggerName());
131     remoteLogger.callAppenders(event);
132       } else {
133     logger.warn("Received message is of type "+message.getJMSType()
134             +", was expecting ObjectMessage.");
135       }
136     } catch(JMSException JavaDoc jmse) {
137       logger.error("Exception thrown while processing incoming message.",
138            jmse);
139     }
140   }
141
142
143   protected static Object JavaDoc lookup(Context JavaDoc ctx, String JavaDoc name) throws NamingException JavaDoc {
144     try {
145       return ctx.lookup(name);
146     } catch(NameNotFoundException JavaDoc e) {
147       logger.error("Could not find name ["+name+"].");
148       throw e;
149     }
150   }
151
152   static void usage(String JavaDoc msg) {
153     System.err.println(msg);
154     System.err.println("Usage: java " + JMSSink.class.getName()
155             + " TopicConnectionFactoryBindingName TopicBindingName username password configFile");
156     System.exit(1);
157   }
158 }
159
Popular Tags