FileDocCategorySizeDatePackage
JMSQueueAppender.javaAPI DocApache log4j 1.2.156966Sat Aug 25 00:09:36 BST 2007None

JMSQueueAppender

public class JMSQueueAppender extends AppenderSkeleton
A Simple JMS (P2P) Queue Appender.
author
Ceki Gülcü
author
Jamie Tsao

Fields Summary
protected QueueConnection
queueConnection
protected QueueSession
queueSession
protected QueueSender
queueSender
protected Queue
queue
String
initialContextFactory
String
providerUrl
String
queueBindingName
String
queueConnectionFactoryBindingName
Constructors Summary
public JMSQueueAppender()

    
Methods Summary
public voidactivateOptions()
Overriding this method to activate the options for this class i.e. Looking up the Connection factory ...

	
	QueueConnectionFactory queueConnectionFactory;
	
	try {

	    Context ctx = getInitialContext();      
	    queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(queueConnectionFactoryBindingName);
	    queueConnection = queueConnectionFactory.createQueueConnection();
    
	    queueSession = queueConnection.createQueueSession(false,
							      Session.AUTO_ACKNOWLEDGE);
      
	    Queue queue = (Queue) ctx.lookup(queueBindingName);
	    queueSender = queueSession.createSender(queue);
	    
	    queueConnection.start();

	    ctx.close();      

	} catch(Exception e) {
	    errorHandler.error("Error while activating options for appender named ["+name+
			       "].", e, ErrorCode.GENERIC_FAILURE);
	}
    
public voidappend(org.apache.log4j.spi.LoggingEvent event)
This method called by {@link AppenderSkeleton#doAppend} method to do most of the real appending work. The LoggingEvent will be be wrapped in an ObjectMessage to be put on the JMS queue.


	if(!checkEntryConditions()) {
	    return;
	}
	
	try {

	    ObjectMessage msg = queueSession.createObjectMessage();
	    msg.setObject(event);
	    queueSender.send(msg);

	} catch(Exception e) {
	    errorHandler.error("Could not send message in JMSQueueAppender ["+name+"].", e, 
			       ErrorCode.GENERIC_FAILURE);
	}
    
protected booleancheckEntryConditions()

	
	String fail = null;
	
	if(this.queueConnection == null) {
	    fail = "No QueueConnection";
	} else if(this.queueSession == null) {
	    fail = "No QueueSession";
	} else if(this.queueSender == null) {
	    fail = "No QueueSender";
	} 
	
	if(fail != null) {
	    errorHandler.error(fail +" for JMSQueueAppender named ["+name+"].");      
	    return false;
	} else {
	    return true;
	}
    
public synchronized voidclose()
Close this JMSQueueAppender. Closing releases all resources used by the appender. A closed appender cannot be re-opened.


	if(this.closed) 
	    return;
	
	LogLog.debug("Closing appender ["+name+"].");
	this.closed = true;    
	
	try {
	    if(queueSession != null) 
		queueSession.close();	
	    if(queueConnection != null) 
		queueConnection.close();
	} catch(Exception e) {
	    LogLog.error("Error while closing JMSQueueAppender ["+name+"].", e);	
	}   

	// Help garbage collection
	queueSender = null;
	queueSession = null;
	queueConnection = null;
    
protected javax.naming.InitialContextgetInitialContext()

	try {
	    Hashtable ht = new Hashtable();
	    
	    //Populate property hashtable with data to retrieve the context.
	    ht.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
	    ht.put(Context.PROVIDER_URL, providerUrl);
	    
	    return (new InitialContext(ht));
	    
	} catch (NamingException ne) {
	    LogLog.error("Could not get initial context with ["+initialContextFactory + "] and [" + providerUrl + "]."); 
	    throw ne;
	}
    
public java.lang.StringgetInitialContextFactory()
Returns the value of the InitialContextFactory option.

	return initialContextFactory;
    
public java.lang.StringgetProviderUrl()
Returns the value of the ProviderUrl option.

	return providerUrl;
    
public java.lang.StringgetQueueBindingName()
Returns the value of the QueueBindingName option.

	return queueBindingName;
    
public java.lang.StringgetQueueConnectionFactoryBindingName()
Returns the value of the QueueConnectionFactoryBindingName option.

	return queueConnectionFactoryBindingName;
    
public booleanrequiresLayout()

	return false;
    
public voidsetInitialContextFactory(java.lang.String initialContextFactory)
The InitialContextFactory option takes a string value. Its value, along with the ProviderUrl option will be used to get the InitialContext.

	this.initialContextFactory = initialContextFactory;
    
public voidsetProviderUrl(java.lang.String providerUrl)
The ProviderUrl option takes a string value. Its value, along with the InitialContextFactory option will be used to get the InitialContext.

	this.providerUrl = providerUrl;
    
public voidsetQueueBindingName(java.lang.String queueBindingName)
The QueueBindingName option takes a string value. Its value will be used to lookup the appropriate destination Queue from the JNDI context.

	this.queueBindingName = queueBindingName;
    
public voidsetQueueConnectionFactoryBindingName(java.lang.String queueConnectionFactoryBindingName)
The QueueConnectionFactoryBindingName option takes a string value. Its value will be used to lookup the appropriate QueueConnectionFactory from the JNDI context.

	this.queueConnectionFactoryBindingName = queueConnectionFactoryBindingName;