1
2
3
4
5
6
7
8
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
28
29
30
31
32
33
34
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
53
54
55
56 public void setTopicConnectionFactoryBindingName(String tcfBindingName) {
57 this.tcfBindingName = tcfBindingName;
58 }
59
60
61
62
63 public String getTopicConnectionFactoryBindingName() {
64 return tcfBindingName;
65 }
66
67
68
69
70
71 public void setTopicBindingName(String topicBindingName) {
72 this.topicBindingName = topicBindingName;
73 }
74
75
76
77
78 public String getTopicBindingName() {
79 return topicBindingName;
80 }
81
82
83
84
85 public void start() {
86 TopicConnectionFactory topicConnectionFactory;
87
88 try {
89 Context jndi = buildJNDIContext();
90
91
92 topicConnectionFactory = (TopicConnectionFactory) lookup(jndi,
93 tcfBindingName);
94
95 if (userName != null) {
96 this.topicConnection = topicConnectionFactory.createTopicConnection(
97 userName, password);
98 } else {
99 this.topicConnection = topicConnectionFactory.createTopicConnection();
100 }
101
102
103
104
105 this.topicSession = topicConnection.createTopicSession(false,
106 Session.AUTO_ACKNOWLEDGE);
107
108
109 Topic topic = (Topic) lookup(jndi, topicBindingName);
110
111
112 this.topicPublisher = topicSession.createPublisher(topic);
113
114
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
131
132
133 public synchronized void stop() {
134
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
153 topicPublisher = null;
154 topicSession = null;
155 topicConnection = null;
156 }
157
158
159
160
161
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
185
186
187 protected TopicConnection getTopicConnection() {
188 return topicConnection;
189 }
190
191
192
193
194
195 protected TopicSession getTopicSession() {
196 return topicSession;
197 }
198
199
200
201
202
203 protected TopicPublisher getTopicPublisher() {
204 return topicPublisher;
205 }
206 }