JmsLogAppenderSupport xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * 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.codehaus.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  /***
    36   * An abstract base class for implementation inheritence for a log4j JMS appender
    37   *
    38   * @version $Revision: 1.1 $
    39   */
    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             // lets ensure we're all created
    125             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     // Implementation methods
    134     //-------------------------------------------------------------------------
    135     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         System.out.println("#### Calling append with event: " + event);
    147         try {
    148             Message message = createMessage(event);
    149             Destination destination = getDestination(event);
    150             getProducer().send(destination, message);
    151         }
    152         catch (Exception e) {
    153             getErrorHandler().error("Could not send message due to: " + e, e, JMS_PUBLISH_ERROR_CODE, event);
    154         }
    155     }
    156
    157     protected Message createMessage(LoggingEvent event) throws JMSException, NamingException {
    158         Message answer = null;
    159         Object value = event.getMessage();
    160         if (allowTextMessages && value instanceof String) {
    161             answer = getSession().createTextMessage((String) value);
    162         }
    163         else {
    164             answer = getSession().createObjectMessage((Serializable) value);
    165         }
    166         answer.setStringProperty("level", event.getLevel().toString());
    167         answer.setIntProperty("levelInt", event.getLevel().toInt());
    168         answer.setStringProperty("threadName", event.getThreadName());
    169         return answer;
    170     }
    171
    172     protected Destination getDestination(LoggingEvent event) throws JMSException, NamingException {
    173         String name = subjectPrefix + event.getLoggerName();
    174         return getSession().createTopic(name);
    175     }
    176 }