View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.classic.net;
12  
13  import javax.jms.ObjectMessage;
14  import javax.jms.Session;
15  import javax.jms.Topic;
16  import javax.jms.TopicConnection;
17  import javax.jms.TopicConnectionFactory;
18  import javax.jms.TopicPublisher;
19  import javax.jms.TopicSession;
20  import javax.naming.Context;
21  
22  import ch.qos.logback.classic.spi.LoggingEvent;
23  import ch.qos.logback.core.AppenderBase;
24  import ch.qos.logback.core.net.JMSAppenderBase;
25  
26  /**
27   * A simple appender that publishes events to a JMS Topic. The events are
28   * serialized and transmitted as JMS message type {@link
29   * javax.jms.ObjectMessage}.
30   * 
31   * For more information about this appender, please refer to
32   * http://logback.qos.ch/manual/appenders.html#JMSTopicAppender
33   * 
34   * @author Ceki Gülcü
35   */
36  public class JMSTopicAppender extends JMSAppenderBase<LoggingEvent> {
37  
38    static int SUCCESSIVE_FAILURE_LIMIT = 3;
39  
40    String topicBindingName;
41    String tcfBindingName;
42    TopicConnection topicConnection;
43    TopicSession topicSession;
44    TopicPublisher topicPublisher;
45  
46    int successiveFailureCount = 0;
47  
48    public JMSTopicAppender() {
49    }
50  
51    /**
52     * The <b>TopicConnectionFactoryBindingName</b> option takes a string value.
53     * Its value will be used to lookup the appropriate
54     * <code>TopicConnectionFactory</code> from the JNDI context.
55     */
56    public void setTopicConnectionFactoryBindingName(String tcfBindingName) {
57      this.tcfBindingName = tcfBindingName;
58    }
59  
60    /**
61     * Returns the value of the <b>TopicConnectionFactoryBindingName</b> option.
62     */
63    public String getTopicConnectionFactoryBindingName() {
64      return tcfBindingName;
65    }
66  
67    /**
68     * The <b>TopicBindingName</b> option takes a string value. Its value will be
69     * used to lookup the appropriate <code>Topic</code> from the JNDI context.
70     */
71    public void setTopicBindingName(String topicBindingName) {
72      this.topicBindingName = topicBindingName;
73    }
74  
75    /**
76     * Returns the value of the <b>TopicBindingName</b> option.
77     */
78    public String getTopicBindingName() {
79      return topicBindingName;
80    }
81  
82    /**
83     * Options are activated and become effective only after calling this method.
84     */
85    public void start() {
86      TopicConnectionFactory topicConnectionFactory;
87  
88      try {
89        Context jndi = buildJNDIContext();
90  
91        // addInfo("Looking up [" + tcfBindingName + "]");
92        topicConnectionFactory = (TopicConnectionFactory) lookup(jndi,
93            tcfBindingName);
94        // addInfo("About to create TopicConnection.");
95        if (userName != null) {
96          this.topicConnection = topicConnectionFactory.createTopicConnection(
97              userName, password);
98        } else {
99          this.topicConnection = topicConnectionFactory.createTopicConnection();
100       }
101 
102       // addInfo(
103       // "Creating TopicSession, non-transactional, "
104       // + "in AUTO_ACKNOWLEDGE mode.");
105       this.topicSession = topicConnection.createTopicSession(false,
106           Session.AUTO_ACKNOWLEDGE);
107 
108       // addInfo("Looking up topic name [" + topicBindingName + "].");
109       Topic topic = (Topic) lookup(jndi, topicBindingName);
110 
111       // addInfo("Creating TopicPublisher.");
112       this.topicPublisher = topicSession.createPublisher(topic);
113 
114       // addInfo("Starting TopicConnection.");
115       topicConnection.start();
116 
117       jndi.close();
118     } catch (Exception e) {
119       addError("Error while activating options for appender named [" + name
120           + "].", e);
121     }
122 
123     if (this.topicConnection != null && this.topicSession != null
124         && this.topicPublisher != null) {
125       super.start();
126     }
127   }
128 
129   /**
130    * Close this JMSAppender. Closing releases all resources used by the
131    * appender. A closed appender cannot be re-opened.
132    */
133   public synchronized void stop() {
134     // The synchronized modifier avoids concurrent append and close operations
135     if (!this.started) {
136       return;
137     }
138 
139     this.started = false;
140 
141     try {
142       if (topicSession != null) {
143         topicSession.close();
144       }
145       if (topicConnection != null) {
146         topicConnection.close();
147       }
148     } catch (Exception e) {
149       addError("Error while closing JMSAppender [" + name + "].", e);
150     }
151 
152     // Help garbage collection
153     topicPublisher = null;
154     topicSession = null;
155     topicConnection = null;
156   }
157 
158 
159   /**
160    * This method called by {@link AppenderBase#doAppend} method to do most
161    * of the real appending work.
162    */
163   public void append(LoggingEvent event) {
164     if (!isStarted()) {
165       return;
166     }
167 
168     try {
169       ObjectMessage msg = topicSession.createObjectMessage();
170 
171       msg.setObject(event);
172       topicPublisher.publish(msg);
173       successiveFailureCount = 0;
174     } catch (Exception e) {
175       successiveFailureCount++;
176       if (successiveFailureCount > SUCCESSIVE_FAILURE_LIMIT) {
177         stop();
178       }
179       addError("Could not publish message in JMSTopicAppender [" + name + "].", e);
180     }
181   }
182 
183   /**
184    * Returns the TopicConnection used for this appender. Only valid after
185    * start() method has been invoked.
186    */
187   protected TopicConnection getTopicConnection() {
188     return topicConnection;
189   }
190 
191   /**
192    * Returns the TopicSession used for this appender. Only valid after start()
193    * method has been invoked.
194    */
195   protected TopicSession getTopicSession() {
196     return topicSession;
197   }
198 
199   /**
200    * Returns the TopicPublisher used for this appender. Only valid after start()
201    * method has been invoked.
202    */
203   protected TopicPublisher getTopicPublisher() {
204     return topicPublisher;
205   }
206 }