KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JMSQueueAppender


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import org.apache.log4j.AppenderSkeleton;
18 import org.apache.log4j.spi.LoggingEvent;
19 import org.apache.log4j.spi.ErrorHandler;
20 import org.apache.log4j.spi.ErrorCode;
21 import org.apache.log4j.helpers.LogLog;
22
23 import java.util.Hashtable JavaDoc;
24 import java.util.Properties JavaDoc;
25 import javax.jms.*;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.NameNotFoundException JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 /**
32  * A Simple JMS (P2P) Queue Appender.
33  *
34  * @author Ceki Gülcü
35  * @author Jamie Tsao
36 */

37 public class JMSQueueAppender extends AppenderSkeleton {
38
39     protected QueueConnection queueConnection;
40     protected QueueSession queueSession;
41     protected QueueSender queueSender;
42     protected Queue queue;
43     
44     String JavaDoc initialContextFactory;
45     String JavaDoc providerUrl;
46     String JavaDoc queueBindingName;
47     String JavaDoc queueConnectionFactoryBindingName;
48     
49     public
50     JMSQueueAppender() {
51     }
52
53   
54     /**
55      * The <b>InitialContextFactory</b> option takes a string value.
56      * Its value, along with the <b>ProviderUrl</b> option will be used
57      * to get the InitialContext.
58      */

59     public void setInitialContextFactory(String JavaDoc initialContextFactory) {
60     this.initialContextFactory = initialContextFactory;
61     }
62
63     /**
64      * Returns the value of the <b>InitialContextFactory</b> option.
65      */

66     public String JavaDoc getInitialContextFactory() {
67     return initialContextFactory;
68     }
69
70     /**
71      * The <b>ProviderUrl</b> option takes a string value.
72      * Its value, along with the <b>InitialContextFactory</b> option will be used
73      * to get the InitialContext.
74      */

75     public void setProviderUrl(String JavaDoc providerUrl) {
76     this.providerUrl = providerUrl;
77     }
78
79     /**
80      * Returns the value of the <b>ProviderUrl</b> option.
81      */

82     public String JavaDoc getProviderUrl() {
83     return providerUrl;
84     }
85
86     /**
87      * The <b>QueueConnectionFactoryBindingName</b> option takes a
88      * string value. Its value will be used to lookup the appropriate
89      * <code>QueueConnectionFactory</code> from the JNDI context.
90      */

91     public void setQueueConnectionFactoryBindingName(String JavaDoc queueConnectionFactoryBindingName) {
92     this.queueConnectionFactoryBindingName = queueConnectionFactoryBindingName;
93     }
94   
95     /**
96      * Returns the value of the <b>QueueConnectionFactoryBindingName</b> option.
97      */

98     public String JavaDoc getQueueConnectionFactoryBindingName() {
99     return queueConnectionFactoryBindingName;
100     }
101     
102     /**
103      * The <b>QueueBindingName</b> option takes a
104      * string value. Its value will be used to lookup the appropriate
105      * destination <code>Queue</code> from the JNDI context.
106      */

107     public void setQueueBindingName(String JavaDoc queueBindingName) {
108     this.queueBindingName = queueBindingName;
109     }
110   
111     /**
112        Returns the value of the <b>QueueBindingName</b> option.
113     */

114     public String JavaDoc getQueueBindingName() {
115     return queueBindingName;
116     }
117     
118
119     /**
120      * Overriding this method to activate the options for this class
121      * i.e. Looking up the Connection factory ...
122      */

123     public void activateOptions() {
124     
125     QueueConnectionFactory queueConnectionFactory;
126     
127     try {
128
129         Context JavaDoc ctx = getInitialContext();
130         queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(queueConnectionFactoryBindingName);
131         queueConnection = queueConnectionFactory.createQueueConnection();
132     
133         queueSession = queueConnection.createQueueSession(false,
134                                   Session.AUTO_ACKNOWLEDGE);
135       
136         Queue queue = (Queue) ctx.lookup(queueBindingName);
137         queueSender = queueSession.createSender(queue);
138         
139         queueConnection.start();
140
141         ctx.close();
142
143     } catch(Exception JavaDoc e) {
144         errorHandler.error("Error while activating options for appender named ["+name+
145                    "].", e, ErrorCode.GENERIC_FAILURE);
146     }
147     }
148  
149     protected InitialContext JavaDoc getInitialContext() throws NamingException JavaDoc {
150     try {
151         Hashtable JavaDoc ht = new Hashtable JavaDoc();
152         
153         //Populate property hashtable with data to retrieve the context.
154
ht.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
155         ht.put(Context.PROVIDER_URL, providerUrl);
156         
157         return (new InitialContext JavaDoc(ht));
158         
159     } catch (NamingException JavaDoc ne) {
160         LogLog.error("Could not get initial context with ["+initialContextFactory + "] and [" + providerUrl + "].");
161         throw ne;
162     }
163     }
164
165   
166     protected boolean checkEntryConditions() {
167     
168     String JavaDoc fail = null;
169     
170     if(this.queueConnection == null) {
171         fail = "No QueueConnection";
172     } else if(this.queueSession == null) {
173         fail = "No QueueSession";
174     } else if(this.queueSender == null) {
175         fail = "No QueueSender";
176     }
177     
178     if(fail != null) {
179         errorHandler.error(fail +" for JMSQueueAppender named ["+name+"].");
180         return false;
181     } else {
182         return true;
183     }
184     }
185
186   /**
187    * Close this JMSQueueAppender. Closing releases all resources used by the
188    * appender. A closed appender cannot be re-opened.
189    */

190     public synchronized // avoid concurrent append and close operations
191
void close() {
192
193     if(this.closed)
194         return;
195     
196     LogLog.debug("Closing appender ["+name+"].");
197     this.closed = true;
198     
199     try {
200         if(queueSession != null)
201         queueSession.close();
202         if(queueConnection != null)
203         queueConnection.close();
204     } catch(Exception JavaDoc e) {
205         LogLog.error("Error while closing JMSQueueAppender ["+name+"].", e);
206     }
207
208     // Help garbage collection
209
queueSender = null;
210     queueSession = null;
211     queueConnection = null;
212     }
213     
214     /**
215      * This method called by {@link AppenderSkeleton#doAppend} method to
216      * do most of the real appending work. The LoggingEvent will be
217      * be wrapped in an ObjectMessage to be put on the JMS queue.
218      */

219     public void append(LoggingEvent event) {
220
221     if(!checkEntryConditions()) {
222         return;
223     }
224     
225     try {
226
227         ObjectMessage msg = queueSession.createObjectMessage();
228         msg.setObject(event);
229         queueSender.send(msg);
230
231     } catch(Exception JavaDoc e) {
232         errorHandler.error("Could not send message in JMSQueueAppender ["+name+"].", e,
233                    ErrorCode.GENERIC_FAILURE);
234     }
235     }
236     
237     public boolean requiresLayout() {
238     return false;
239     }
240 }
Popular Tags