Tag: logging

Apache CXF Client Side Logging in JBoss AS7

Hi,

JBoss AS7 uses the JBossWS-CXF as default webservice provider which provides most of the features coming with Apache CXF (including WS-Security, WS-Policy, WS-Addressing, WS-ReliableMessaging, basic WS-Trust, MTOM) as well as common JBossWS stack features like endpoint metrics, record management, endpoint address rewrite, etc.

In this example we will see how we can enable the client side Logging for the incoming and outgoing SOAP Request/Responses, which are most desired for trouble shooting and debugging purpose.

In this example we will mainly focus on following points

Point-1). How to use the JBoss Specific Tools to build the WebServices. The details of these tools like “wsconsume” and “wsprovide” are mentioned in the following link: https://docs.jboss.org/author/display/JBWS/JAX-WS+Tools

Point-2). Where we need to place the “log4j.properties fileat the client side in order to generate a separate Log file containing the SOAP request/response.

Point-3). How to use “org.apache.cxf.interceptor.LoggingInInterceptor” and “org.apache.cxf.interceptor.LoggingOutInterceptor” at client side.

Point-4). Here we are using JBoss AS7.1.0 CR1b which can be downloaded from the following link: http://www.jboss.org/jbossas/downloads. I am using “jboss-as-7.1.0.CR1b” in this demo.

Point-5). Source Code of this Demo is available at https://github.com/jaysensharma/MiddlewareMagicDemos

Developing Simple WebService

Step-1). Create a directory somewhere in your file system as “/home/userone/CXFClientSide_Logging_Demo” then create another directory “src” inside the “/home/userone/CXFClientSide_Logging_Demo”.

Step-2). Write a simple webservice implementation pojo class “DemoCXF.java” as following inside the directory “/home/userone/CXFClientSide_Logging_Demo/src”:


package ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;
import java.util.Collection;

@WebService(name = "DemoCXF", serviceName ="DemoCXFService", portName ="DemoCXFPort", targetNamespace="http://test.org")

public class DemoCXF
{
  @Resource
  WebServiceContext ctx;

  @WebMethod()
  public String sayHello(String name)
  {
    System.out.println("nnt Method Invoked....String sayHello("+name+")");
    return "Hello JBossAS7 User: "+name;
  }

  @WebMethod()
  public String getProperty(String propertyName)
  {
    System.out.println("nnt Method Invoked....String getProperty(String propertyName)");
    return "RETURNED: "+(Collection) ctx.getMessageContext().values();
  }
}

Step-3). Write the “web.xml” file to define the WebService as following inside the directory “/home/userone/CXFClientSide_Logging_Demo/src”:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet> 
    <servlet-name>DemoCXF</servlet-name>  
    <servlet-class>ws.DemoCXF</servlet-class>  
  </servlet>  

  <servlet-mapping> 
    <servlet-name>DemoCXF</servlet-name>  
    <url-pattern>/*</url-pattern> 
  </servlet-mapping>  
</web-app>

Step-4). Write the WebService client code “Test_CXF_Client.java” as following inside the directory “/home/userone/CXFClientSide_Logging_Demo/src”:


package client;
import java.net.URL;
import javax.xml.namespace.QName;

// Client side Logging related CXF APIs 
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

public class Test_CXF_Client
  {
     public static void main(String ar[]) throws Exception
      {
        String WSDL_URL=ar[0]+"?wsdl";
        DemoCXFService service=new DemoCXFService(new URL(WSDL_URL));
        DemoCXF port=service.getDemoCXFPort();

        /* Following part of code is needed for client side Logging of Soap request/response */
        /* We need to make sure that we place the "log4j.properties" file inside clients classpath */
        Client client = ClientProxy.getClient(port);
        client.getInInterceptors().add(new LoggingInInterceptor());
        client.getOutInterceptors().add(new LoggingOutInterceptor()); 

         
        System.out.println("nt port.sayHello("MiddlewareMagic") = "+port.sayHello("MiddlewareMagic"));
      }
  }

Step-5). As we are going to generate the client side logging in a separate log file at client side using CXF APIs so we will need to write a “log4j.properties” file as following inside the directory “/home/userone/CXFClientSide_Logging_Demo/src”:

# Direct log messages to a log file
log4j.rootLogger=INFO, WSClientAppender


log4j.appender.WSClientAppender=org.apache.log4j.RollingFileAppender
log4j.appender.WSClientAppender.File=/NotBackedUp/Downloads/CXFClientDemo/clientStuff/webserviceClient.log
log4j.appender.WSClientAppender.MaxFileSize=10MB
log4j.appender.WSClientAppender.MaxBackupIndex=3
log4j.appender.WSClientAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.WSClientAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

NOTE: Make sure that you have defined the right Location for the Log File and the Directory must exist, which is defined in the above file property “log4j.appender.WSClientAppender.File”.

Step-6). Now we will write simple ANT build script “build.xml” which will build / deploy and run the webservice & client as following inside the directory “/home/userone/CXFClientSide_Logging_Demo”:

<project name="JBoss_Service" default="deploy">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.0.CR1b" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />

<property name="basedir" value="." />
<property name="war.dir" value="CXFClientLoggingDemoWAR" />
<property name="war.name" value="CXFClientLoggingDemo.war" />
<property name="src.dir" value="src" />
<property name="client.src.dir" value="${basedir}/clientSrc" />
<property name="output.dir" value="build" />
<property name="client.dir" value="${basedir}/clientStuff" />
<property name="client.jar.name" value="DemoCXFClient.jar" />
 
   <path id="jboss.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>  
   </path>

   <path id="client.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>  
     <fileset dir="${client.dir}">
        <include name="*.jar"/>
     </fileset>  
   </path>

   <taskdef name="wsprovide" classname="org.jboss.ws.tools.ant.WSProvideTask">
       <classpath refid="jboss.classpath"/>
   </taskdef>

   <taskdef name="wsconsume" classname="org.jboss.ws.tools.ant.WSConsumeTask">
       <classpath refid="jboss.classpath"/>
   </taskdef>

        <target name="init">
           <delete dir="${output.dir}" />
           <mkdir dir="${output.dir}" />
           <mkdir dir="${output.dir}/${war.dir}"/>
           <mkdir dir="${output.dir}/${war.dir}/META-INF"/>
           <mkdir dir="${output.dir}/${war.dir}/WEB-INF"/>
           <mkdir dir="${output.dir}/${war.dir}/WEB-INF/classes"/>
           <delete dir="${client.dir}" />
           <mkdir dir="${client.dir}/META-INF"/>
        </target>
	 
    <target name="build" depends="init">
       <javac srcdir="${src.dir}" destdir="${output.dir}/${war.dir}/WEB-INF/classes"  includes="*.java" excludes="Test_CXF_Client.java" classpathref="jboss.classpath"/>
        <copy todir="${output.dir}/${war.dir}/WEB-INF">
	  <fileset dir="${basedir}/src">
	      <include name="web.xml"/>
	  </fileset>
	</copy>   
       <wsprovide
        	fork="false"
        	keep="true"
        	destdir="${output.dir}"
        	resourcedestdir="${output.dir}/${war.dir}/WEB-INF/wsdl"
        	sourcedestdir="${output.dir}"
        	genwsdl="true" 
        	verbose="true"
        	sei="ws.DemoCXF">
            	<classpath>
                	  <pathelement path="${output.dir}/${war.dir}/WEB-INF/classes"/>
            	</classpath>
      </wsprovide>

      <jar jarfile="${output.dir}/${war.name}" basedir="${output.dir}/${war.dir}" compress="true" /> 
      
    </target>

        <target name="deploy" depends="build">
            <echo message="*******************  Deploying   *********************" />  
            <echo message="********** ${war.name} to ${jboss.home}/standalone/deployments **********" />  
            <copy file="${output.dir}/${war.name}" todir="${jboss.home}/standalone/deployments/"/>

            <echo message="*******************  Deployed Successfully   *********************" />  
        </target>
  
        <target name="post-deploy" >
            <echo message="*******************  NOTE  *********************" />
            <echo message="***** You should be able to access your WSDL using Browser now *****" />
            <echo message="                http://localhost:8080/CXFClientLoggingDemo?wsdl          " />
        </target>  

        <target name="client" depends="post-deploy">
           <delete dir="${client.dir}" />
           <mkdir dir="${client.dir}"/>             
             <wsconsume
                      fork="true"
                      keep="true"
                      destdir="${client.dir}"
                      sourcedestdir="${client.dir}"
                      package="client"
                      wsdlLocation="service.wsdl"
                      wsdl="http://localhost:8080/CXFClientLoggingDemo?wsdl">
            </wsconsume>


            <!-- We need to make sure that log4j.properties is present inside client's classpath-->
            <copy file="${src.dir}/log4j.properties" todir="${client.dir}" />

            <javac srcdir="${src.dir}" destdir="${client.dir}"  includes="Test_CXF_Client.java" classpathref="client.classpath">
	        <classpath>
	            <pathelement location="${client.dir}"/>
	            <path refid="client.classpath"/>
	        </classpath>
            </javac>

            <jar jarfile="${client.dir}/${client.jar.name}" basedir="${client.dir}" compress="true" /> 


            <!-- Cleaning Client Stuff Directory --> 
            <delete dir="${client.dir}/client"/> 
            <delete file="${client.dir}/log4j.properties"/> 
       </target>

        <target name="run" depends="client">
            <java classname="client.Test_CXF_Client" fork="true" >
	        <classpath>
	            <pathelement location="${client.dir}/DemoCXFClient.jar"/>
	            <path refid="client.classpath"/>
	        </classpath>
                <jvmarg value="-Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger" />
                <arg value="http://localhost:8080/CXFClientLoggingDemo"/>
            </java>
        </target>  

</project>

NOTE: The only change in the above file you need to do is to change the “jboss.home” directory path in the second line of the above script to point to your own JBoss AS7 directory.

Step-7). Now Start your JBoss Profile as following from inside the directory “/home/userone/jboss-as-7.1.0.CR1/bin”:

.
 ./standalone.sh -c standalone-full.xml
.

Step-8). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.6.5/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.6.5/bin;%PATH%

Step-9). Now once the PATH is set In the command/Shell prompt you can move inside the directory “/home/userone/CXFClientSide_Logging_Demo” and then run the ant to build and deploy the WebService application on your JBoss Standalone full profile, by running the command “ant deploy”

[userone@localhost CXFClientSide_Logging_Demo]$ ant deploy
Buildfile: build.xml

init:
   [delete] Deleting directory /home/userone/CXFClientSide_Logging_Demo/build
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/META-INF
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF/classes
   [delete] Deleting directory /home/userone/CXFClientSide_Logging_Demo/clientStuff
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/clientStuff/META-INF

build:
    [javac] Compiling 1 source file to /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF/classes
     [copy] Copying 1 file to /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF
[wsprovide] Generating from endpoint: ws.DemoCXF
[wsprovide] log4j:WARN No appenders could be found for logger (org.apache.cxf.common.logging.LogUtils).
[wsprovide] log4j:WARN Please initialize the log4j system properly.
[wsprovide] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[wsprovide] java2ws -s /home/userone/CXFClientSide_Logging_Demo/build -classdir /home/userone/CXFClientSide_Logging_Demo/build -d /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF/wsdl -verbose -wsdl -cp ::::::::: -wrapperbean -createxsdimports ws.DemoCXF
[wsprovide] java2ws - Apache CXF 2.4.4
[wsprovide] 
      [jar] Building jar: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemo.war

deploy:
     [echo] *******************  Deploying   *********************
     [echo] ********** CXFClientLoggingDemo.war to /home/userone/jboss-as-7.1.0.CR1b/standalone/deployments **********
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.0.CR1b/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************

BUILD SUCCESSFUL
Total time: 6 seconds

As soon as the WebService will be deployed on the JBoss AS7 you will see the following kind of output on your JBoss Console (STDOUT):

12:55:32,698 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) Starting deployment of "CXFClientLoggingDemo.war"
12:55:32,953 INFO  [org.jboss.wsf.stack.cxf.metadata.MetadataBuilder] (MSC service thread 1-2) Add Service
 id=DemoCXF
 address=http://localhost:8080/CXFClientLoggingDemo
 implementor=ws.DemoCXF
 invoker=org.jboss.wsf.stack.cxf.JBossWSInvoker
 serviceName={http://test.org}DemoCXFService
 portName={http://test.org}DemoCXFPort
 wsdlLocation=null
 mtomEnabled=false
12:55:32,954 INFO  [org.jboss.ws.common.management.DefaultEndpointRegistry] (MSC service thread 1-2) register: jboss.ws:context=CXFClientLoggingDemo,endpoint=DemoCXF
12:55:33,174 INFO  [org.apache.cxf.service.factory.ReflectionServiceFactoryBean] (MSC service thread 1-2) Creating Service {http://test.org}DemoCXFService from class ws.DemoCXF
12:55:33,482 INFO  [org.apache.cxf.endpoint.ServerImpl] (MSC service thread 1-2) Setting the server's publish address to be http://localhost:8080/CXFClientLoggingDemo
12:55:33,605 INFO  [org.jboss.wsf.stack.cxf.deployment.WSDLFilePublisher] (MSC service thread 1-2) WSDL published to: file:/home/userone/jboss-as-7.1.0.CR1b/standalone/data/wsdl/CXFClientLoggingDemo.war/DemoCXFService.wsdl
12:55:33,613 INFO  [org.jboss.as.webservices] (MSC service thread 1-8) JBAS015539: Starting service jboss.ws.port-component-link
12:55:33,622 INFO  [org.jboss.as.webservices] (MSC service thread 1-3) JBAS015539: Starting service jboss.ws.endpoint."CXFClientLoggingDemo.war".DemoCXF
12:55:33,647 INFO  [org.jboss.web] (MSC service thread 1-7) registering web context: /CXFClientLoggingDemo
12:55:33,667 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "CXFClientLoggingDemo.war"

Step-10). Now we will try to compile and run the WebService Client application by running the command “ant run”

[userone@localhost CXFClientSide_Logging_Demo]$ ant run
Buildfile: build.xml

post-deploy:
     [echo] *******************  NOTE  *********************
     [echo] ***** You should be able to access your WSDL using Browser now *****
     [echo]                 http://localhost:8080/CXFClientLoggingDemo?wsdl          

client:
   [delete] Deleting directory /home/userone/CXFClientSide_Logging_Demo/clientStuff
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/clientStuff
[wsconsume] Consuming wsdl: http://localhost:8080/CXFClientLoggingDemo?wsdl
[wsconsume] log4j:WARN No appenders could be found for logger (org.apache.cxf.common.logging.LogUtils).
[wsconsume] log4j:WARN Please initialize the log4j system properly.
[wsconsume] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
     [copy] Copying 1 file to /home/userone/CXFClientSide_Logging_Demo/clientStuff
    [javac] Compiling 1 source file to /home/userone/CXFClientSide_Logging_Demo/clientStuff
      [jar] Building jar: /home/userone/CXFClientSide_Logging_Demo/clientStuff/DemoCXFClient.jar
   [delete] Deleting directory /home/userone/CXFClientSide_Logging_Demo/clientStuff/client
   [delete] Deleting: /home/userone/CXFClientSide_Logging_Demo/clientStuff/log4j.properties

run:
      Feb 4, 2012 12:55:45 PM client.DemoCXFService <clinit>
      INFO: Can not initialize the default wsdl from service.wsdl
      
      	 port.sayHello("MiddlewareMagic") = Hello JBossAS7 User: MiddlewareMagic

BUILD SUCCESSFUL
Total time: 9 seconds

Step-11). Once the webservice is invoked successfully then you will be able to see the Client Side log file is generated in the location which is mentioned in the “log4j.properties” file…something like following:

12:55:46,418  INFO ReflectionServiceFactoryBean:366 - Creating Service {http://test.org}DemoCXFService from WSDL: http://localhost:8080/CXFClientLoggingDemo?wsdl
12:55:46,930  INFO LoggingOutInterceptor:151 - Outbound Message
---------------------------
ID: 1
Address: http://localhost:8080/CXFClientLoggingDemo
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://test.org"><arg0>MiddlewareMagic</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
12:55:47,061  INFO LoggingInInterceptor:151 - Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[235], content-type=, Date=[Sat, 04 Feb 2012 07:25:46 GMT], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://test.org"><return>Hello JBossAS7 User: MiddlewareMagic</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------

Enabling Logging On JBoss Server Side

You can add the following System Property (-Dorg.apache.cxf.logging.enabled=true) on JBoss Configuration like “standalone-full.xml” as following:

<server xmlns="urn:jboss:domain:1.3">

    <extensions>
        <extension module="org.jboss.as.clustering.infinispan"/>
        <extension module="org.jboss.as.cmp"/>
       .
       .
       .
        <extension module="org.jboss.as.webservices"/>
        <extension module="org.jboss.as.weld"/>
    </extensions>

    <system-properties>
        <property name="org.apache.cxf.logging.enabled" value="true"/>
    </system-properties>


    <management>
        <security-realms>
       .
       .
        </security-realms>
   </management>

.
.
Thanks 🙂
Middleware Magic Team


Enabling DataSource Spy Logging in JBoss AS7

Hi,

Some times we face issues while invoking some JDBC statements using the DataSource JDBC Connection. Many times it is not easily identifiable that which part of the JDBC code is actually causing the failure or issue. So it is better to have a Spy logging kind of feature inside the datasource which can tell us all the activities which an application performs on the Jdbc connection & Statements retrieved from the DataSource.

JBoss AS7 uses Jacmar for implementing it’s datasource mechanism. Also JBoss AS7 provides a very effecient way of enabling the spy logging which can log all the informations related to the connection/statements and any method whcih is invoked on the JDBC Objects.

In order to enable the DataSource Spy logging in JBoss AS7 we will need to do the following two things:

Step1). Enable the “jboss.jdbc.spy” to DEBUG inside your “standalone-full.xml” or “domain.xml” file present inside “jboss-as-7.1.0.Beta1/standalone/confguration” directory as following:

        <subsystem xmlns="urn:jboss:domain:logging:1.1">
            <console-handler name="CONSOLE">
                <level name="INFO"/>
                <formatter>
                    <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
                </formatter>
            </console-handler>
            <periodic-rotating-file-handler name="FILE">
                <formatter>
                    <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
                </formatter>
                <file relative-to="jboss.server.log.dir" path="server.log"/>
                <suffix value=".yyyy-MM-dd"/>
                <append value="true"/>
            </periodic-rotating-file-handler>
            <logger category="com.arjuna">
                <level name="WARN"/>
            </logger>
            <logger category="org.apache.tomcat.util.modeler">
                <level name="WARN"/>
            </logger>
            <logger category="sun.rmi">
                <level name="WARN"/>
            </logger>
            <logger category="jacorb">
                <level name="WARN"/>
            </logger>
            <logger category="jacorb.config">
                <level name="ERROR"/>
            </logger>

            <!-- ************* Enabling Spy logging category ************ -->
            <logger category="jboss.jdbc.spy">
                <level name="TRACE"/>
            </logger>
            <!-- *************************************************-->

            <root-logger>
                <level name="INFO"/>
                <handlers>
                    <handler name="CONSOLE"/>
                    <handler name="FILE"/>
                </handlers>
            </root-logger>
        </subsystem>

Step2). Create a DataSource from JBoss Console or using “jboss-admin.sh” utility. You can refer to the following article for more details on creating DataSources:
How to create Oracle DataSource And Install Oracle JDBC Driver in JBoss AS7
OR
How to create MySQL datasource And Install MySQL JDBC driver in JBoss AS7

NOTE: The only thing you need to remember is to add the spy=”true” attribute in your DataSource configuration as following:

        <subsystem xmlns="urn:jboss:domain:datasources:1.0">
            <datasources>
                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="H2DS" enabled="true">
                    <connection-url>
                        jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
                    </connection-url>
                    <driver>
                        h2
                    </driver>
                    <security>
                        <security-domain>
                            encrypted-ds
                        </security-domain>
                    </security>
                </datasource>


                <!-- ****** Below is MySqlDS with Spy logging enabled ****** -->
                <datasource jndi-name="java:/MySqlDS" pool-name="MySqlDS_Pool" enabled="true" spy="true" jta="false" use-ccm="false">
                    <connection-url>
                        jdbc:mysql://localhost:3306/testDB
                    </connection-url>
                    <driver-class>
                        com.mysql.jdbc.Driver
                    </driver-class>
                    <driver>
                        mysql-connector-java-5.1.13-bin.jar
                    </driver>
                    <security>
                        <security-domain>
                            encrypted-ds
                        </security-domain>
                    </security>
                </datasource>
                <!-- ****** Above is MySqlDS with Spy logging enabled ****** -->


                <drivers>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>
                            org.h2.jdbcx.JdbcDataSource
                        </xa-datasource-class>
                    </driver>
                </drivers>
            </datasources>
        </subsystem>

NOTE: In order to enable the spy logging you must follow both the steps “Step-1” and “Step-2” together.

Step3). Now suppose if you have following kind of JSP Page inside your application which uses the JBoss DataSource Jdbc Connection in order to perform some JDBC operations:

<%@page contentType="text/html" import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*"%>

<%
DataSource ds = null;
Connection con = null; 
PreparedStatement pr = null; 
InitialContext ic; 

out.println("<html>");
out.println("<body>");
out.println("<center>");

	try 
	{
		ic = new InitialContext();
		ds = (DataSource)ic.lookup("java:/MySqlDS");
		con = ds.getConnection(); 
		
		out.println("<font color=green>");
		out.println("<b>Got Connection </b> = "+con);
		out.println("<br><br><br><br><br><br><b> This means our DataSource has been configured properly </b> ");
		out.println("</font>");
		System.out.println("==> Got Connection = "+con);

                Statement stmt=con.createStatement();
                ResultSet rs=stmt.executeQuery("SELECT * FROM emp");
                out.println("<TABLE>");
                while(rs.next())
                 {
                     out.println("<TR><TD>"+rs.getString(1)+"</TD><TD>"+rs.getString(2)+"</TD></TR>");
                 }
                out.println("</TABLE>");
	}

	catch (SQLException s)
	{
		out.println("<font color=red>");
		out.println("<b>!!! Did not get the connection </b> = " +s);
		out.println("<br><br><br><br><br><br><b>This means we might have to check our datasource configuration (i.e. URL, Username, Password etc) or might be the database is down </b> ");
		out.println("</font>");
		System.out.println("!!! Did not get the connection = " +s);
	}

	catch(Exception e)
	{
		out.println("<font color=red>");
		out.println("<b>!!! Exception thrown </b> = " +e);
		out.println("<br><br><br><br><br><br><b>This means we might have to check our datasource configuration (i.e. URL, Username, Password etc) or might be the database is down </b> ");
		out.println("</font>");
		System.out.println("!!! Exception thrown = "+e);
	}

	finally
	{
		if(con != null)
		{
			con.close();
		}      
	} 

out.println("</center>");
out.println("</body>");
out.println("</html>");
%>

Step4). Restart your JBoss after enabling the Spy Logging and then access the application which performs some activity on the JDBC Connection retrieved from the JBoss AS7 DataSource. The you will notice in your “server.log” file following kind of DEBUG messages:

23:01:14,744 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [Connection] createStatement()
23:01:14,748 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [Statement] executeQuery(SELECT * FROM emp)
23:01:14,756 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [ResultSet] next()
23:01:14,756 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [ResultSet] getString(1)
23:01:14,757 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [ResultSet] getString(2)
23:01:14,757 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [ResultSet] next()
23:01:14,757 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [Connection] close()

Means whatever operations our application is performing on the JDBC Connection/Statement…etc objects everything is being logged. This can be very helpful in debugging/troubleshooting many JDBC Related issues.

Following is not part of Spy Logging but Useful

If you are running in Development environment and if it’s OK to have more logging then you can also enable the following category “org.jboss.jca” in your “standalone-full.xml” or “domain.xml” file to get more informations related to your DataSource:

            <logger category="org.jboss.jca">
                <level name="TRACE"/>
            </logger>
23:58:12,112 TRACE [org.jboss.jca.core.connectionmanager.NoTxConnectionManager] (http-localhost-127.0.0.1-8080-1) Subject: Subject:
	Principal: root
	Private Credential: javax.resource.spi.security.PasswordCredential@dd9c93cc

23:58:12,122 DEBUG [org.jboss.jca.core.connectionmanager.pool.idle.IdleRemover] (http-localhost-127.0.0.1-8080-1) internalRegisterPool: about to notify thread: old next: 1322937792121, new next: 1322937792121
23:58:12,123 TRACE [org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject] (http-localhost-127.0.0.1-8080-1) ManagedConnectionPool: 52e0cdc9
Method: getConnection(Subject:
	Principal: root
	Private Credential: javax.resource.spi.security.PasswordCredential@dd9c93cc
, null)
ManagedConnectionFactory:
  Class: org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory
  Object: 4200309
ConnectionListenerFactory:
  Class: org.jboss.jca.core.connectionmanager.notx.NoTxConnectionManagerImpl
  Object: 51a745fc
Pool:
  Name: MySqlDS_Pool
PoolConfiguration:
  MinSize: 0
  MaxSize: 20
  BlockingTimeout: 30000
  IdleTimeout: 1800000
  BackgroundValidation: false
  BackgroundValidationMillis: 0
  StrictMin: false
  UseFastFail: false
Available:
InUse:
Statistics:
  ActiveCount: 0
  AvailableCount: 20
  AverageBlockingTime: 0
  AverageCreationTime: 0
  CreatedCount: 0
  DestroyedCount: 0
  MaxCreationTime: 0
  MaxUsedCount: 0
  MaxWaitCount: 0
  MaxWaitTime: 0
  TimedOut: 0
  TotalBlockingTime: 0
  TotalCreationTime: 0
23:58:12,124 TRACE [org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory] (http-localhost-127.0.0.1-8080-1) Using properties: {user=root, password=--hidden--}
23:58:12,124 TRACE [org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory] (http-localhost-127.0.0.1-8080-1) Checking driver for URL: jdbc:mysql://localhost:3306/testDB
23:58:12,125 TRACE [org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory] (http-localhost-127.0.0.1-8080-1) Driver not yet registered for url: jdbc:mysql://localhost:3306/testDB
23:58:12,125 DEBUG [org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory] (http-localhost-127.0.0.1-8080-1) Driver loaded and instance created:com.mysql.jdbc.Driver@75589559
23:58:12,180 DEBUG [org.jboss.jca.core.tx.jbossts.XAResourceRecoveryImpl] (Periodic Recovery) RecoverySecurityDomain was empty
23:58:12,180 DEBUG [org.jboss.jca.core.tx.jbossts.XAResourceRecoveryImpl] (Periodic Recovery) Subject for recovery was null
23:58:12,338 TRACE [org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject] (http-localhost-127.0.0.1-8080-1) supplying new ManagedConnection: org.jboss.jca.core.connectionmanager.listener.NoTxConnectionListener@4b7d94f8[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@21bf4c80 connection handles=0 lastUse=1322936892337 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject@163778cf pool internal context=org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool@52e0cdc9]
23:58:12,338 TRACE [org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject] (http-localhost-127.0.0.1-8080-1) Got connection from pool: org.jboss.jca.core.connectionmanager.listener.NoTxConnectionListener@4b7d94f8[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@21bf4c80 connection handles=0 lastUse=1322936892337 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject@163778cf pool internal context=org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool@52e0cdc9]
23:58:12,347 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) ==> Got Connection = org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@28892a87
23:58:12,350 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [Connection] createStatement()
23:58:12,355 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [Statement] executeQuery(SELECT * FROM emp)
23:58:12,362 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [ResultSet] next()
23:58:12,363 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [ResultSet] getString(1)
23:58:12,363 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [ResultSet] getString(2)
23:58:12,363 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [ResultSet] next()
23:58:12,363 DEBUG [jboss.jdbc.spy] (http-localhost-127.0.0.1-8080-1) java:/MySqlDS [Connection] close()
23:58:12,365 TRACE [org.jboss.jca.core.connectionmanager.listener.NoTxConnectionListener] (http-localhost-127.0.0.1-8080-1) unregisterConnection: 0 handles left
23:58:12,365 TRACE [org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject] (http-localhost-127.0.0.1-8080-1) ManagedConnectionPool: 52e0cdc9
Method: returnConnection(4b7d94f8, false)
ManagedConnectionFactory:
  Class: org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory
  Object: 4200309
ConnectionListenerFactory:
  Class: org.jboss.jca.core.connectionmanager.notx.NoTxConnectionManagerImpl
  Object: 51a745fc
Pool:
  Name: MySqlDS_Pool
PoolConfiguration:
  MinSize: 0
  MaxSize: 20
  BlockingTimeout: 30000
  IdleTimeout: 1800000
  BackgroundValidation: false
  BackgroundValidationMillis: 0
  StrictMin: false
  UseFastFail: false
Available:
InUse:
  4b7d94f8 (NORMAL)
Statistics:
  ActiveCount: 1
  AvailableCount: 19
  AverageBlockingTime: 0
  AverageCreationTime: 212
  CreatedCount: 1
  DestroyedCount: 0
  MaxCreationTime: 212
  MaxUsedCount: 1
  MaxWaitCount: 0
  MaxWaitTime: 0
  TimedOut: 0
  TotalBlockingTime: 0
  TotalCreationTime: 212
23:58:12,366 TRACE [org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject] (http-localhost-127.0.0.1-8080-1) Returning connection to pool org.jboss.jca.core.connectionmanager.listener.NoTxConnectionListener@4b7d94f8[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@21bf4c80 connection handles=0 lastUse=1322936892366 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject@163778cf pool internal context=org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool@52e0cdc9]

.
.
Thanks
MiddlewareMagic Team


Fetching Client IP Address and Header information in JBoss AS7 access log

Hi,

Many times and usually in production environments it is desired to generate the “access.log” file separately for different web applications so that we can monitor how the users are accessing the application, from what all IP Addresses/Hostnames the clients are accessing the application. We also want to monitor sometimes that how much time was taken by JBoss in order to process a clients request in milliseconds or in seconds.

The most desired things the cookie, incoming header, outgoing response headers, the Session or something else in the ServletRequest. So to fulfil all these requirements JBoss provides a valve “org.apache.catalina.valves.AccessLogValve” which can be enabled at an individual application level as well with the help of jboss specific deployment descriptor “jboss-web.xml”.

The AccessLogValve creates log files in the same format as those created by standard web servers. These logs can later be analyzed by standard log analysis tools to track page hit counts, user session activity, and so on. The files produces by this Valve are rolled over nightly at midnight. More informations about this Valve can be found in the following link: http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html#Access_Log_Valve
Some of the useful patterns which can be used along with this valve are following:

%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%h - Remote host name (or IP address if resolveHosts is false)
%H - Request protocol
%l - Remote logical username from identd (always returns '-')
%m - Request method (GET, POST, etc.)
%p - Local port on which this request was received
%q - Query string (prepended with a '?' if it exists)
%r - First line of the request (method and request URI)
%s - HTTP status code of the response
%S - User session ID
%t - Date and time, in Common Log Format
%u - Remote user that was authenticated (if any), else '-'
%U - Requested URL path
%v - Local server name
%D - Time taken to process the request, in millis
%T - Time taken to process the request, in seconds
%I - current request thread name (can compare later with stacktraces)

Step1). Create a directory somewhere in your file system like “/home/userone/DemoRemoteAddrValve”

Step2). Now create a directory with name “src” inside the “/home/userone/DemoRemoteAddrValve” and then place the following kind of “web.xml” file inside the “src” folder.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

        <!-- There is nothing much to write here for this demonstration. -->

</web-app>

Step3). Now write a JBoss specific we application deployment descriptor “jboss-web.xml” file inside “/home/userone/DemoRemoteAddrValve/src” directory as following:

<jboss-web>
 <valve>
   <class-name>org.apache.catalina.valves.AccessLogValve</class-name>
     <param>
        <param-name>prefix</param-name>
        <param-value>TestAccessLogValveWebApp</param-value>
     </param>
     <param>
        <param-name>suffix</param-name>
        <param-value>.log</param-value>
     </param>
     <param>
        <param-name>pattern</param-name>
        <param-value>%h %l %u %t "%r" %s %b %{User-Agent}i %{JSESSIONID}c</param-value>
     </param>
     <param>
        <param-name>directory</param-name>
        <param-value>../standalone/log</param-value>
     </param>
     <param>
        <param-name>resolveHosts</param-name>
        <param-value>false</param-value>
     </param>
   </valve>
</jboss-web>

NOTE: There is also support to write information from the cookie, incoming header, outgoing response headers, the Session or something else in the ServletRequest. It is modeled after the apache syntax:

%{xxx}i for incoming request headers
%{xxx}o for outgoing response headers
%{xxx}c for a specific request cookie
%{xxx}r xxx is an attribute in the ServletRequest
%{xxx}s xxx is an attribute in the HttpSession

Example:

     <param>
        <param-name>pattern</param-name>
        <param-value>%h %l %u %t "%r" %s %b %{User-Agent}i %{JSESSIONID}c</param-value>
     </param>

So the above pattern will also display the JSESSIONID cookie information and the User-Agent (clients browser) informations as well.

Step4). Now we will simply write a Simle JSP “index.jsp” So that we can see as soon as user will hit 10-15 request to this application the access logging will happen. Place the following JSP file inside “/home/userone/DemoRemoteAddrValve/src”

<html>
<body bgcolor=maroon>
<center><h1> Welcome Page </h1>
<font color=white>
    <h1> Check the "/home/userone/jboss-as-7.0.1.Final/standalone/log" directory</h1>
    <h3>The access log for the application might be created with the name specified in the jboss-web.xml file. </h3>
</font>
</body>
</html>

NOTE: The access log data is not immediately written in the access log …it is buffered in order to improve the performance so hit the page atleast 10-15 times and then check the access log.

Step5). Now we will write a Simple ANT Build script “build.xml” file to build the WAR file and then to deploy the application on JBoss AS7. place the following “build.xml” file inside “/home/userone/DemoRemoteAddrValve”

<project name="SingletonStartupService" default="deploy">
<property name="jboss.home" value="/home/jsenshar/JBoss_All/jboss-as-7.0.1.Final" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="basedir" value="." />
<property name="tmp.dir" value="tmp" />
<property name="output.dir" value="build" />
<property name="src.dir" value="src" />
<property name="war.name" value="TestAccessLogValveWebApp.war" />

   <path id="jboss.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>
   </path>

        <target name="init">
           <delete dir="${output.dir}" />
           <mkdir dir="${output.dir}" />
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
        </target>

        <target name="build" depends="init">
           <copy todir="${tmp.dir}/WEB-INF">
                <fileset dir="${src.dir}/">
                  <include name="web.xml"/>
                  <include name="jboss-web.xml"/>
                </fileset>
           </copy>
          <mkdir dir="${tmp.dir}/WEB-INF/lib" />
          <copy file="${src.dir}/index.jsp" tofile="${tmp.dir}/index.jsp"/>
          <copy file="${src.dir}/jbossweb-7.0.1.Final.jar" tofile="${tmp.dir}/WEB-INF/lib/jbossweb-7.0.1.Final.jar"/>
          <jar jarfile="${tmp.dir}/${war.name}" basedir="${tmp.dir}" compress="true" />
          <copy file="${tmp.dir}/${war.name}" tofile="${output.dir}/${war.name}"/>
          <delete includeEmptyDirs="true">
              <fileset dir="${tmp.dir}"/>
          </delete>
        </target>

        <target name="deploy" depends="build">
            <echo message="*******************  Deploying the WAR file ${war.name} *********************" />
            <echo message="********** ${output.dir}/${war.name} to ${jboss.home}/standalone/deployments **********" />
            <copy todir="${jboss.home}/standalone/deployments/">
                <fileset dir="${output.dir}/">
                  <include name="${war.name}"/>
                </fileset>
            </copy>
            <echo message="*******************  Deployed Successfully   *********************" />
        </target>
</project>

Step6). Now open a Shell prompt and then set the PATH pointing to the ANT bin directory and the JDK bin directory … like following and the just run the “ant” command:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.6.5/bin:$PATH

OR

For Windows Based OS:
set PATH=C:jdk1.6.0_21bin:C:org.apache.ant_1.6.5bin:%PATH%

[userone@localhost TestEE6Feature]$ ant
Buildfile: build.xml

init:
   [delete] Deleting directory /home/userone/TestAccessLogValve/build
    [mkdir] Created dir: /home/userone/TestAccessLogValve/build
    [mkdir] Created dir: /home/userone/TestAccessLogValve/tmp

build:
     [copy] Copying 2 files to /home/userone/TestAccessLogValve/tmp/WEB-INF
    [mkdir] Created dir: /home/userone/TestAccessLogValve/tmp/WEB-INF/lib
     [copy] Copying 1 file to /home/userone/TestAccessLogValve/tmp
     [copy] Copying 1 file to /home/userone/TestAccessLogValve/tmp/WEB-INF/lib
      [jar] Building jar: /home/userone/TestAccessLogValve/tmp/TestAccessLogValveWebApp.war
     [copy] Copying 1 file to /home/userone/TestAccessLogValve/build
   [delete] Deleting 5 files from /home/userone/TestAccessLogValve/tmp
   [delete] Deleted 3 directories from /home/userone/TestAccessLogValve/tmp

deploy:
     [echo] *******************  Deploying the WAR file TestAccessLogValveWebApp.war *********************
     [echo] ********** build/TestAccessLogValveWebApp.war to /home/userone/jboss-as-7.0.1.Final/standalone/deployments **********
     [copy] Copying 1 file to /home/userone//deployments
     [echo] *******************  Deployed Successfully   *********************

BUILD SUCCESSFUL
Total time: 0 seconds

NOTE: The only change you will need to make is to change the “jboss.home” value in the above ANT build.xml file to point to your own JBoss AS& directory.

Step7). Start the JBoss AS& like following from inside your “/home/userone/jboss-as-7.0.1.Final/bin” directory.

   ./standalone.sh --server-config standalone-preview.xml

NOTE: Due to some reasons if you find that access log is not getting generated after deploying/redeploying your application then  Undeploy the application then clear the “data” and “tmp” directory and then freshly deploy your application. or check the path of the access log which you entered is correct or not?

<param>
<param-name>directory</param-name>
<param-value>../standalone/log</param-value>
</param>

Enabling Access Log Valve at Whole JBossAS 7.1.2 level

Similarly if you want to configure Access Log Valve at whole server level then you can add the tag inside the “web” subsystem of your server. AS 7.1.1 or AS 7.1.2

[standalone@localhost:9999 /] /subsystem=web/virtual-server=default-host/access-log=configuration:add(pattern="%h %l %u %t "%r" %s %b %{User-Agent}i %{JSESSIONID}c")
{"outcome" => "success"}


[standalone@localhost:9999 /] /subsystem=web/virtual-server=default-host/access-log=configuration/directory=configuration:add(path="TestAccessLog", relative-to="jboss.server.log.dir")
{"outcome" => "success"}


[standalone@localhost:9999 /] /subsystem=web/virtual-server=default-host/access-log=configuration/:read-resource(recursive=true,proxies=false,include-runtime=false,include-defaults=true)
{
    "outcome" => "success",
    "result" => {
        "extended" => false,
        "pattern" => "%h %l %u %t "%r" %s %b %{User-Agent}i %{JSESSIONID}c",
        "prefix" => false,
        "resolve-hosts" => false,
        "rotate" => true,
        "directory" => {"configuration" => {
            "path" => "TestAccessLog",
            "relative-to" => "jboss.server.log.dir"
        }}
    }
}

It will generate the following kind of configuration inside your “web” subsystem:

        <subsystem xmlns="urn:jboss:domain:web:1.1" default-virtual-server="default-host" native="false">
            <connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>
            <virtual-server name="default-host" enable-welcome-root="true">
                <alias name="localhost"/>
                <alias name="example.com"/>

                <access-log pattern="%h %l %u %t "%r" %s %b %{User-Agent}i %{JSESSIONID}c">
                    <directory path="TestAccessLog"/>
                </access-log>

            </virtual-server>
        </subsystem>

.
.
Thanks
Middleware Magic Team


Copyright © 2010-2012 Middleware Magic. All rights reserved. |