1 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 ; 24 import java.util.Properties ; 25 import javax.jms.*; 26 import javax.naming.InitialContext ; 27 import javax.naming.Context ; 28 import javax.naming.NameNotFoundException ; 29 import javax.naming.NamingException ; 30 31 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 initialContextFactory; 45 String providerUrl; 46 String queueBindingName; 47 String queueConnectionFactoryBindingName; 48 49 public 50 JMSQueueAppender() { 51 } 52 53 54 59 public void setInitialContextFactory(String initialContextFactory) { 60 this.initialContextFactory = initialContextFactory; 61 } 62 63 66 public String getInitialContextFactory() { 67 return initialContextFactory; 68 } 69 70 75 public void setProviderUrl(String providerUrl) { 76 this.providerUrl = providerUrl; 77 } 78 79 82 public String getProviderUrl() { 83 return providerUrl; 84 } 85 86 91 public void setQueueConnectionFactoryBindingName(String queueConnectionFactoryBindingName) { 92 this.queueConnectionFactoryBindingName = queueConnectionFactoryBindingName; 93 } 94 95 98 public String getQueueConnectionFactoryBindingName() { 99 return queueConnectionFactoryBindingName; 100 } 101 102 107 public void setQueueBindingName(String queueBindingName) { 108 this.queueBindingName = queueBindingName; 109 } 110 111 114 public String getQueueBindingName() { 115 return queueBindingName; 116 } 117 118 119 123 public void activateOptions() { 124 125 QueueConnectionFactory queueConnectionFactory; 126 127 try { 128 129 Context 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 e) { 144 errorHandler.error("Error while activating options for appender named ["+name+ 145 "].", e, ErrorCode.GENERIC_FAILURE); 146 } 147 } 148 149 protected InitialContext getInitialContext() throws NamingException { 150 try { 151 Hashtable ht = new Hashtable (); 152 153 ht.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 155 ht.put(Context.PROVIDER_URL, providerUrl); 156 157 return (new InitialContext (ht)); 158 159 } catch (NamingException 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 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 190 public synchronized 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 e) { 205 LogLog.error("Error while closing JMSQueueAppender ["+name+"].", e); 206 } 207 208 queueSender = null; 210 queueSession = null; 211 queueConnection = null; 212 } 213 214 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 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 |