1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.net;
15
16 import java.io.Serializable;
17
18 import javax.jms.ObjectMessage;
19 import javax.jms.Session;
20 import javax.jms.Topic;
21 import javax.jms.TopicConnection;
22 import javax.jms.TopicConnectionFactory;
23 import javax.jms.TopicPublisher;
24 import javax.jms.TopicSession;
25 import javax.naming.Context;
26
27 import ch.qos.logback.classic.spi.ILoggingEvent;
28 import ch.qos.logback.core.AppenderBase;
29 import ch.qos.logback.core.net.JMSAppenderBase;
30 import ch.qos.logback.core.spi.PreSerializationTransformer;
31
32
33
34
35
36
37
38
39
40
41
42 public class JMSTopicAppender extends JMSAppenderBase<ILoggingEvent> {
43
44 static int SUCCESSIVE_FAILURE_LIMIT = 3;
45
46 String topicBindingName;
47 String tcfBindingName;
48 TopicConnection topicConnection;
49 TopicSession topicSession;
50 TopicPublisher topicPublisher;
51
52 int successiveFailureCount = 0;
53
54 private PreSerializationTransformer<ILoggingEvent> pst = new LoggingEventPreSerializationTransformer();
55
56 public JMSTopicAppender() {
57 }
58
59
60
61
62
63
64 public void setTopicConnectionFactoryBindingName(String tcfBindingName) {
65 this.tcfBindingName = tcfBindingName;
66 }
67
68
69
70
71 public String getTopicConnectionFactoryBindingName() {
72 return tcfBindingName;
73 }
74
75
76
77
78
79 public void setTopicBindingName(String topicBindingName) {
80 this.topicBindingName = topicBindingName;
81 }
82
83
84
85
86 public String getTopicBindingName() {
87 return topicBindingName;
88 }
89
90
91
92
93 public void start() {
94 TopicConnectionFactory topicConnectionFactory;
95
96 try {
97 Context jndi = buildJNDIContext();
98
99
100 topicConnectionFactory = (TopicConnectionFactory) lookup(jndi,
101 tcfBindingName);
102
103 if (userName != null) {
104 this.topicConnection = topicConnectionFactory.createTopicConnection(
105 userName, password);
106 } else {
107 this.topicConnection = topicConnectionFactory.createTopicConnection();
108 }
109
110
111
112
113 this.topicSession = topicConnection.createTopicSession(false,
114 Session.AUTO_ACKNOWLEDGE);
115
116
117 Topic topic = (Topic) lookup(jndi, topicBindingName);
118
119
120 this.topicPublisher = topicSession.createPublisher(topic);
121
122
123 topicConnection.start();
124
125 jndi.close();
126 } catch (Exception e) {
127 addError("Error while activating options for appender named [" + name
128 + "].", e);
129 }
130
131 if (this.topicConnection != null && this.topicSession != null
132 && this.topicPublisher != null) {
133 super.start();
134 }
135 }
136
137
138
139
140
141 public synchronized void stop() {
142
143 if (!this.started) {
144 return;
145 }
146
147 this.started = false;
148
149 try {
150 if (topicSession != null) {
151 topicSession.close();
152 }
153 if (topicConnection != null) {
154 topicConnection.close();
155 }
156 } catch (Exception e) {
157 addError("Error while closing JMSAppender [" + name + "].", e);
158 }
159
160
161 topicPublisher = null;
162 topicSession = null;
163 topicConnection = null;
164 }
165
166
167
168
169
170
171 public void append(ILoggingEvent event) {
172 if (!isStarted()) {
173 return;
174 }
175
176 try {
177 ObjectMessage msg = topicSession.createObjectMessage();
178 Serializable so = pst.transform(event);
179 msg.setObject(so);
180 topicPublisher.publish(msg);
181 successiveFailureCount = 0;
182 } catch (Exception e) {
183 successiveFailureCount++;
184 if (successiveFailureCount > SUCCESSIVE_FAILURE_LIMIT) {
185 stop();
186 }
187 addError("Could not publish message in JMSTopicAppender [" + name + "].", e);
188 }
189 }
190
191
192
193
194
195 protected TopicConnection getTopicConnection() {
196 return topicConnection;
197 }
198
199
200
201
202
203 protected TopicSession getTopicSession() {
204 return topicSession;
205 }
206
207
208
209
210
211 protected TopicPublisher getTopicPublisher() {
212 return topicPublisher;
213 }
214 }