Skip to main content

Develop an Asynchronous Logging Framework using log4j with JMS and WebSphere MQ Source Code


Step 2. JNDI and JMS caching classes lookupObject method



	public Object lookupService(String serviceName) throws ServiceNotFoundException 
	{
		try
		{
			if(initialContext == null)
			{
				if(properties != null)
				{
					initialContext = new InitialContext(properties);
				}
				else
				{
					initialContext = new InitialContext();
				}
			}
			java.lang.Object obj = initialContext.lookup(serviceName);
			return obj;
		}
		catch(NamingException ne)
		{
			ne.printStackTrace(System.out);
			throw new ServiceNotFoundException(ne.getExplanation());
		}
	}


Step 3. Log4j JMS Appender class Extend AppenderSkeleton class


	public boolean requiresLayout() 
	{
		return true;
	}

	protected void append(LoggingEvent event) 
	{
		try
		{
			JMSServiceFactory jmsService = 
				JMSServiceFactory.getJmsServiceFactory();
			QueueConnectionFactory queueFactory = 
			      (QueueConnectionFactory)jmsService.getTargetJMSConnectionFactory
									("jms/LogConnectionFactory",
									  serviceLocator);
			QueueConnection queueConnection = 
				queueFactory.createQueueConnection();
			QueueSession queueSession = 
				queueConnection.createQueueSession(false,
									     Session.AUTO_ACKNOWLEDGE);
			Queue queue = 
			  (Queue)jmsService.getTargetJMSQueue("jms/LogQueue",
									   serviceLocator);
			QueueSender queueSender = 
				queueSession.createSender(queue);
			TextMessage msg = 
				queueSession.createTextMessage();
			msg.setText(getLayout().format(event));
			queueSender.send(msg);
		}
		catch(ServiceNotFoundException snfe)
		{
			snfe.printStackTrace(System.out);
		}
		catch(JMSException jmse)
		{
			jmse.printStackTrace(System.out);
		}
	}


Step 4. Simple XML Layout and Message classes Overridden toXML() message


	public String toXML() 
	{
	    StringBuffer buf = new StringBuffer(4098);
		
          buf.append("\n\r\n");
	    buf.append("\t\r\n");
	    buf.append("\t\t" + getCategory() + "\n");
	    buf.append("\t" + "\r\n");
	    buf.append("\t\r\n");
	    buf.append("\t\t" + getClassName() + "\n");
	    buf.append("\t" + "\r\n");
	    buf.append("\t\r\n");
	    buf.append("\t\t" +getDate().toString() + "\n");
	    buf.append("\t\r\n");
	    buf.append("\t\r\n");
	    buf.append("\t\t" +getMessage() + "\n");
	    buf.append("\t\r\n");  
	    buf.append("\r\n\r\n");
    return buf.toString();
	}


Step 7. Test Servlet and JSP to write to log goGet method that writes to the log


public void doGet(javax.servlet.http.HttpServletRequest request,
			javax.servlet.http.HttpServletResponse response)
		throws javax.servlet.ServletException, java.io.IOException 
		{
			PropertyConfigurator.configure
				(getServletContext().getRealPath("/") + 
                    "/WEB-INF/classes/BasicConfig.lcf");
			LogClass log = new LogClass();
			log.debug("Debug Test",this);
			log.info("Info Test",this);
			log.warn("Warn Test",this);
			log.error("Error Test",this);
			log.fatal("Fatal Test",this);
			request.getRequestDispatcher
               ("/LogResult.jsp").forward(request,response);
		}


Step 9. Run JMSAdmin Script for JNDI and JMS Objects JMSAdmin.config file


#
#  This is the default configuration file for the MQSeries Classes for
#  Java Message Service Administration Tool.
#
#  The following line specifies which JNDI service provider is in use.
#  It currently indicates an LDAP service provider. If a different
#  service provider is used, this line should be commented out and the
#  appropriate one should be uncommented.
#
#INITIAL_CONTEXT_FACTORY=com.sun.jndi.ldap.LdapCtxFactory
INITIAL_CONTEXT_FACTORY=com.sun.jndi.fscontext.RefFSContextFactory
#INITIAL_CONTEXT_FACTORY=com.ibm.ejs.ns.jndi.CNInitialContextFactory
#
#  The following line specifies the URL of the service provider's initial
#  context. It currently refers to an LDAP root context. Examples of a
#  file system URL and WebSphere's JNDI namespace are also shown, commented
#  out.
#
#PROVIDER_URL=ldap://polaris/o=ibm,c=us
PROVIDER_URL=file:/C:/LOG4JBindings
#PROVIDER_URL=iiop://localhost/
#
#  The following line specifies the security authentication model in use,
#  and may be 'none' (for anonymous authentication), 'simple', or 'CRAM-MD5'.
#
SECURITY_AUTHENTICATION=none


Step 3. Rerun JMSAdmin for WebSphere Advanced Edition and configure through the Admin Client Modify the JMSAdmin configuration file to point to WebSphere 4.0 as our JNDI Server



#
#  This is the default configuration file for the MQSeries Classes for
#  Java Message Service Administration Tool.
#
#  The following line specifies which JNDI service provider is in use.
#  It currently indicates an LDAP service provider. If a different
#  service provider is used, this line should be commented out and the
#  appropriate one should be uncommented.
#
#INITIAL_CONTEXT_FACTORY=com.sun.jndi.ldap.LdapCtxFactory
#INITIAL_CONTEXT_FACTORY=com.sun.jndi.fscontext.RefFSContextFactory
INITIAL_CONTEXT_FACTORY=com.ibm.ejs.ns.jndi.CNInitialContextFactory
#
#  The following line specifies the URL of the service provider's initial
#  context. It currently refers to an LDAP root context. Examples of a
#  file system URL and WebSphere's JNDI namespace are also shown, commented
#  out.
#
#PROVIDER_URL=ldap://polaris/o=ibm,c=us
#PROVIDER_URL=file:/C:/LOG4JBindings
PROVIDER_URL=iiop://localhost/
#
#  The following line specifies the security authentication model in use,
#  and may be 'none' (for anonymous authentication), 'simple', or 'CRAM-MD5'.
#
SECURITY_AUTHENTICATION=none