1 18 package org.apache.activemq.util; 19 20 import org.apache.log4j.AppenderSkeleton; 21 import org.apache.log4j.spi.LoggingEvent; 22 23 import javax.jms.Connection ; 24 import javax.jms.Destination ; 25 import javax.jms.JMSException ; 26 import javax.jms.Message ; 27 import javax.jms.MessageProducer ; 28 import javax.jms.Session ; 29 import javax.naming.NamingException ; 30 import java.io.Serializable ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 40 public abstract class JmsLogAppenderSupport extends AppenderSkeleton { 41 42 public static final int JMS_PUBLISH_ERROR_CODE = 61616; 43 44 private Connection connection; 45 private Session session; 46 private MessageProducer producer; 47 private boolean allowTextMessages = true; 48 private String subjectPrefix = "log4j."; 49 50 public JmsLogAppenderSupport() { 51 } 52 53 public Connection getConnection() throws JMSException , NamingException { 54 if (connection == null) { 55 connection = createConnection(); 56 } 57 return connection; 58 } 59 60 public void setConnection(Connection connection) { 61 this.connection = connection; 62 } 63 64 public Session getSession() throws JMSException , NamingException { 65 if (session == null) { 66 session = createSession(); 67 } 68 return session; 69 } 70 71 public void setSession(Session session) { 72 this.session = session; 73 } 74 75 public MessageProducer getProducer() throws JMSException , NamingException { 76 if (producer == null) { 77 producer = createProducer(); 78 } 79 return producer; 80 } 81 82 public void setProducer(MessageProducer producer) { 83 this.producer = producer; 84 } 85 86 public void close() { 87 List errors = new ArrayList (); 88 if (producer != null) { 89 try { 90 producer.close(); 91 } 92 catch (JMSException e) { 93 errors.add(e); 94 } 95 } 96 if (session != null) { 97 try { 98 session.close(); 99 } 100 catch (JMSException e) { 101 errors.add(e); 102 } 103 } 104 if (connection != null) { 105 try { 106 connection.close(); 107 } 108 catch (JMSException e) { 109 errors.add(e); 110 } 111 } 112 for (Iterator iter = errors.iterator(); iter.hasNext();) { 113 JMSException e = (JMSException ) iter.next(); 114 getErrorHandler().error("Error closing JMS resources: " + e, e, JMS_PUBLISH_ERROR_CODE); 115 } 116 } 117 118 public boolean requiresLayout() { 119 return false; 120 } 121 122 public void activateOptions() { 123 try { 124 getProducer(); 126 } 127 catch (Exception e) { 128 getErrorHandler().error("Could not create JMS resources: " + e, e, JMS_PUBLISH_ERROR_CODE); 129 } 130 } 131 132 133 protected abstract Connection createConnection() throws JMSException , NamingException ; 136 137 protected Session createSession() throws JMSException , NamingException { 138 return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE); 139 } 140 141 protected MessageProducer createProducer() throws JMSException , NamingException { 142 return getSession().createProducer(null); 143 } 144 145 protected void append(LoggingEvent event) { 146 try { 147 Message message = createMessage(event); 148 Destination destination = getDestination(event); 149 getProducer().send(destination, message); 150 } 151 catch (Exception e) { 152 getErrorHandler().error("Could not send message due to: " + e, e, JMS_PUBLISH_ERROR_CODE, event); 153 } 154 } 155 156 protected Message createMessage(LoggingEvent event) throws JMSException , NamingException { 157 Message answer = null; 158 Object value = event.getMessage(); 159 if (allowTextMessages && value instanceof String ) { 160 answer = getSession().createTextMessage((String ) value); 161 } 162 else { 163 answer = getSession().createObjectMessage((Serializable ) value); 164 } 165 answer.setStringProperty("level", event.getLevel().toString()); 166 answer.setIntProperty("levelInt", event.getLevel().toInt()); 167 answer.setStringProperty("threadName", event.getThreadName()); 168 return answer; 169 } 170 171 protected Destination getDestination(LoggingEvent event) throws JMSException , NamingException { 172 String name = subjectPrefix + event.getLoggerName(); 173 return getSession().createTopic(name); 174 } 175 } 176 | Popular Tags |