1 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 ; 30 import javax.jms.MessageListener ; 31 import javax.jms.TopicConnection ; 32 import javax.jms.Topic ; 33 import javax.jms.TopicConnectionFactory ; 34 import javax.jms.TopicSubscriber ; 35 import javax.jms.Session ; 36 import javax.jms.TopicSession ; 37 import javax.jms.ObjectMessage ; 38 import javax.jms.JMSException ; 39 40 import java.io.BufferedReader ; 41 import java.io.InputStreamReader ; 42 43 import javax.naming.InitialContext ; 44 import javax.naming.Context ; 45 import javax.naming.NameNotFoundException ; 46 import javax.naming.NamingException ; 47 import java.util.Properties ; 48 49 56 public class JMSSink implements javax.jms.MessageListener { 57 58 static Logger logger = Logger.getLogger(JMSSink.class); 59 60 static public void main(String [] args) throws Exception { 61 if(args.length != 5) { 62 usage("Wrong number of arguments."); 63 } 64 65 String tcfBindingName = args[0]; 66 String topicBindingName = args[1]; 67 String username = args[2]; 68 String password = args[3]; 69 70 71 String 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 stdin = new BufferedReader (new InputStreamReader (System.in)); 82 System.out.println("Type \"exit\" to quit JMSSink."); 84 while(true){ 85 String 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 tcfBindingName, String topicBindingName, String username, 95 String password) { 96 97 try { 98 Context ctx = new InitialContext (); 99 TopicConnectionFactory topicConnectionFactory; 100 topicConnectionFactory = (TopicConnectionFactory ) lookup(ctx, 101 tcfBindingName); 102 103 TopicConnection topicConnection = 104 topicConnectionFactory.createTopicConnection(username, 105 password); 106 topicConnection.start(); 107 108 TopicSession topicSession = topicConnection.createTopicSession(false, 109 Session.AUTO_ACKNOWLEDGE); 110 111 Topic topic = (Topic )ctx.lookup(topicBindingName); 112 113 TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic); 114 115 topicSubscriber.setMessageListener(this); 116 117 } catch(Exception e) { 118 logger.error("Could not read JMS message.", e); 119 } 120 } 121 122 public void onMessage(javax.jms.Message message) { 123 LoggingEvent event; 124 Logger remoteLogger; 125 126 try { 127 if(message instanceof ObjectMessage ) { 128 ObjectMessage objectMessage = (ObjectMessage ) 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 jmse) { 137 logger.error("Exception thrown while processing incoming message.", 138 jmse); 139 } 140 } 141 142 143 protected static Object lookup(Context ctx, String name) throws NamingException { 144 try { 145 return ctx.lookup(name); 146 } catch(NameNotFoundException e) { 147 logger.error("Could not find name ["+name+"]."); 148 throw e; 149 } 150 } 151 152 static void usage(String 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 |