Tag: logging

JBossAS7 controlling Client side Logging while using jboss-client.jar

Hi,

JBossAS7 provides “jboss-as-7.1.1.Final/bin/client/jboss-client.jar” file which need to be placed on the client side while interacting with JBoss like remoting/ ejb/jms…etc. But when we run a client with the help of this Jar file the we see a lots of unnecessary logging on the client side which creates some confusion and pulls unnecessary attention, So here in this article we will see how to avoid these client side Logging or How to control these client side loggging which interacting with JBossAS7.1.1.Final

Default Client side Logging while invoking a simple EJB on JBossAS7

Usually we see following kind of unnecessary logging on the client side while invoking an EJB or while using jboss remoting on JBossAS7

.
   java  TestEjbClient


   Jun 12, 2012 1:15:43 AM org.xnio.Xnio <clinit>
   INFO: XNIO Version 3.0.3.GA
   Jun 12, 2012 1:15:43 AM org.xnio.nio.NioXnio <clinit>
   INFO: XNIO NIO Implementation Version 3.0.3.GA
   Jun 12, 2012 1:15:43 AM org.jboss.remoting3.EndpointImpl <clinit>
   INFO: JBoss Remoting version 3.2.3.GA
   Jun 12, 2012 1:15:44 AM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
   INFO: Received server version 1 and marshalling strategies [river]
   Jun 12, 2012 1:15:44 AM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
   INFO: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@32dcb03b, receiver=Remoting connection EJB receiver [connection=Remoting connection <7ac2b2f6>,channel=jboss.ejb,nodename=localhost]} on channel Channel ID da49cdbe (outbound) of Remoting connection 4b00ebec to localhost/127.0.0.1:4447
   Jun 12, 2012 1:15:44 AM org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
   WARN: Unsupported message received with header 0xffffffff
   Jun 12, 2012 1:15:44 AM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
   INFO: Received server version 1 and marshalling strategies [river]
   Jun 12, 2012 1:15:44 AM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
   INFO: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@31ad98ef, receiver=Remoting connection EJB receiver [connection=Remoting connection <16df1832>,channel=jboss.ejb,nodename=localhost]} on channel Channel ID 97e5a096 (outbound) of Remoting connection 31b5998f to localhost/127.0.0.1:4447
   Jun 12, 2012 1:15:44 AM org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
   WARN: Unsupported message received with header 0xffffffff
     
     	--------------------------
   Got initial Context: javax.naming.InitialContext@2cf18e8e
   remote.testMethod("MiddlewareMagic") = [CallerBean] testMethod() returned Hello MiddlewareMagic
.

Steps to Control JBossAS7 Client Side Logging

Step-1) Create a file with name “logging.properties” somewhere in your filesystem like “/home/userone/logging.properties” following:

# Logging
handlers =  java.util.logging.ConsoleHandler
.level = ALL
INFO
 
# Console Logging
java.util.logging.ConsoleHandler.level=SEVERE

NOTE: Based on your requirement you can choose the Logging level as java.util.logging.ConsoleHandler.level= CONFIG, INFO, WARNING, SEVERE, ALL and OFF

Step-2) Now while running your Client program make sure that you use the following JAVA_OPTIONS ” -Djava.util.logging.manager=java.util.logging.LogManager -Djava.util.logging.config.file=/home/userone/logging.properties”

Example:
=========

.
  java   -Djava.util.logging.manager=java.util.logging.LogManager    -Djava.util.logging.config.file=/home/userone/logging.properties  TestEjbClient

     
   --------------------------
   Got initial Context: javax.naming.InitialContext@2cf18e8e
   remote.testMethod("MiddlewareMagic") = [CallerBean] testMethod() returned Hello MiddlewareMagic
.

.
.
Thanks
Middleware Magic Team 🙂


Application level log4j.xml and logging in JBoss AS7

Hi,

Log4j or any logging framework is very useful entity of any enterprise application, So that we can have a separate Application level logging using our own version of log4j.jar or any other logger framework version.

In this demonstration we will see how to use the log4j logging inside our Web Application, with it’s own version of “log4j.xml” file. We will also talk about the following points:

In this example we will mainly focus on following points

Point-1). How to use the “log4j.xml” which is placed inside “${YOUR_WAR}/WEB-INF/classes” directory.

Point-2). How to avoid using the JBoss provided logging framework or How to exclude the “org.apache.log4j” module provided by JBoss with the help of “${YOUR_WAR}/META-INF/jboss-deployment-structure.xml” file.

Point-3). Where to place your own version of “log4j.jar” file inside your WAR file. “${YOUR_WAR}/WEB-INF/lib”

Point-4). The Source code of this Demo can be downloaded from the following link (Git Repository):
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/ApplicationLevelLog4jDemo

Another Demo (uploaded on 26 Nov 2012) at EAR level.
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/EAR_Level_Log4jDemo

Developing WAR with it’s own logging

Step-1). Here we are using JBoss AS7.1 Final release latest build “jboss-as-7.1.0.Final” which can be downloaded from the following link: http://www.jboss.org/jbossas/downloads

Step-2). Create a directory with the name “src” somewhere in your file system like “/home/userone/ApplicationLevelLog4jDemo/”. Inside the “src” directory we are going to place our all source code and xml files for development.

Step-3). Write a simple “index.jsp” file inside “/home/userone/ApplicationLevelLog4jDemo/src” as following:

<html>
<head>
<title>Application Level Log4j Demo In JBoss AS7</title>
</head>
<body>

      <form action="TestServlet" method="Get">
           Choose Log Level : 
                           <select name="logLevel">
                              <option>TRACE</option>
                              <option>DEBUG</option>
                              <option>WARN</option>
                              <option selected="true">INFO</option>
                           </select>
                           <BR>
           Enter Mesage to be logged: <input type="textarea" name="logMessage"/> <BR>
           <input type="submit" name="submit" value="log the message"/> 
      </form>
      
</body>
</html>

Step-4). Now we will write a simple Servlet based on Servlet3.0 specification with name “TestServlet.java” inside “/home/userone/ApplicationLevelLog4jDemo/src” as following

package servlets;
import java.io.IOException;
import java.io.PrintWriter;

//servlet-api
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//log4j
import org.apache.log4j.Logger;

@WebServlet(value="/TestServlet")
public class TestServlet extends HttpServlet 
  {
	private static final long serialVersionUID = 1L;
        private static Logger logger = null;

        public void init() throws ServletException {
         logger= Logger.getLogger(TestServlet.class);   
        }

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			PrintWriter out=response.getWriter();
                        String logLevel=request.getParameter("logLevel");
                        String logMessage=request.getParameter("logMessage");
                        out.println("<html><head><title>Log4j Logging Demo</title></head><body>");
                        if(logLevel.equals("TRACE"))
                           {
                             out.println("["+logLevel+"] "+logMessage+" (logged inside log file).");
                             logger.trace(logMessage);
                           }
                        if(logLevel.equals("DEBUG"))
                           {
                             out.println("["+logLevel+"] "+logMessage+" (logged inside log file).");
                             logger.debug(logMessage);
                           }
                        if(logLevel.equals("INFO"))
                           {
                             out.println("["+logLevel+"] "+logMessage+" (logged inside log file).");
                             logger.info(logMessage);
                           }
                        if(logLevel.equals("WARN"))
                           {
                             out.println("["+logLevel+"] "+logMessage+" (logged inside log file).");
                             logger.warn(logMessage);
                           }

                        out.println("<BR><BR><a href="index.jsp"> Want a different logging level? </a></body></html>");
		     } 
                 catch (Exception e) 
                     {
			e.printStackTrace();
		     }
	}
}

Step-5). The most important part, We will need to write the following kind of “log4j.xml” file inside “/home/userone/ApplicationLevelLog4jDemo/src” as following :

NOTE: We need to just make sure that once the application is build this file should be present inside the ${WAR_NAME}/WEB-INF/classes directory.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
     <appender name="AppLogAppender" class="org.apache.log4j.DailyRollingFileAppender">
         <param name="DatePattern" value="'.'yyyy-MM-dd"/>
	 <param name="File" value="${jboss.server.log.dir}/MyAppLogsFile.log" />
	 <param name="Append" value="false"/>
         <layout class="org.apache.log4j.PatternLayout">
	     <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
	 </layout>
     </appender>

     <!-- The "category" represents the package names of your Application APIs which we want to log -->
     <category name="servlets">
         <priority value="TRACE"/>
     </category>

     <root>
         <priority value ="TRACE"/>
         <appender-ref ref="AppLogAppender"/>
     </root>
 </log4j:configuration>

Step-6). Download and place the desired version of log4j jar (like “log4j-1.2.16.jar”) inside the “/home/userone/ApplicationLevelLog4jDemo/src”. So that after building the application this jar will be available inside the “${WAR_NAME}/WEB-INF/lib” directory.

Step-7). Now the most important part, In order to avoid using JBoss provided logging APIs, we need to place the following kind of “jboss-deployment-structure.xml” file inside “/home/userone/ApplicationLevelLog4jDemo/src” so that we can exclude the jboss logging APIs for our application and our application can use it’s own version of logging APIs.

NOTE: after building the WAR file make sure that the “jboss-deployment-structure.xml” is present inside the “META-INF” directory of your WAR file. Example: ${WAR_NAME}/META-INF/jboss-deployment-structure.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
  <deployment>
    <exclusions>
        <module name="org.apache.log4j" />
    </exclusions>
  </deployment>
</jboss-deployment-structure>

NOTE: If you are using JBossAS 7.1.2 then In order to avoid this ClassCastException you will need to include the following System property in your JBoss -Dorg.jboss.as.logging.per-deployment=false
If you are using JBoss AS7.1.2 and not using the above mentioned “jboss-deployment-structure” file then you will see the following kind of exception at the time of deployment of your WAR:

18:05:41,507 ERROR [stderr] (MSC service thread 1-3) log4j:ERROR Could not create an Appender. Reported error follows.
18:05:41,509 ERROR [stderr] (MSC service thread 1-3) java.lang.ClassCastException: org.apache.log4j.DailyRollingFileAppender cannot be cast to org.apache.log4j.Appender
18:05:41,509 ERROR [stderr] (MSC service thread 1-3) 	at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:248)
18:05:41,510 ERROR [stderr] (MSC service thread 1-3) 	at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:176)
18:05:41,510 ERROR [stderr] (MSC service thread 1-3) 	at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:191)
18:05:41,510 ERROR [stderr] (MSC service thread 1-3) 	at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:523)
18:05:41,510 ERROR [stderr] (MSC service thread 1-3) 	at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:492)
18:05:41,511 ERROR [stderr] (MSC service thread 1-3) 	at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:1001)
18:05:41,511 ERROR [stderr] (MSC service thread 1-3) 	at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:867)
18:05:41,511 ERROR [stderr] (MSC service thread 1-3) 	at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:794)
18:05:41,512 ERROR [stderr] (MSC service thread 1-3) 	at org.jboss.as.logging.LoggingConfigurationProcessor.deploy(LoggingConfigurationProcessor.java:111)
18:05:41,512 ERROR [stderr] (MSC service thread 1-3) 	at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:116)
18:05:41,513 ERROR [stderr] (MSC service thread 1-3) 	at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
18:05:41,513 ERROR [stderr] (MSC service thread 1-3) 	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
18:05:41,513 ERROR [stderr] (MSC service thread 1-3) 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
18:05:41,514 ERROR [stderr] (MSC service thread 1-3) 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
18:05:41,514 ERROR [stderr] (MSC service thread 1-3) 	at java.lang.Thread.run(Thread.java:722)

Step-8). Now in order to build and deploy the above application we will write a very simple ant build script “build.xml” inside “/home/userone/jboss-as-7.1.0.Final/standalone/TESTING/ApplicationLevelLog4jDemo” as following:

<project name="Log4j_Basic_Demo" default="deploy">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.0.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="Log4jDemo.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}" />
           <mkdir dir="${tmp.dir}/WEB-INF/lib"/>
           <mkdir dir="${tmp.dir}/WEB-INF/classes"/>
           <mkdir dir="${tmp.dir}/WEB-INF/META-INF"/>
           <mkdir dir="${tmp.dir}/META-INF"/>
        </target>
 
        <target name="build" depends="init">
           <javac srcdir="${src.dir}" destdir="${tmp.dir}/WEB-INF/classes"  includes="*.java" classpathref="jboss.classpath" />
           <copy todir="${tmp.dir}/WEB-INF/classes">
                <fileset dir="${src.dir}" includes="**/*.java"/>
           </copy>
           <copy file="${src.dir}/index.jsp" tofile="${tmp.dir}/index.jsp"/>   

           <!-- Make sure to place the "log4j.xml" file inside ${WAR_NAME}/WEB-INF/classes  directory--> 
           <copy file="${src.dir}/log4j.xml" todir="${tmp.dir}/WEB-INF/classes"/>

           <!-- Make sure to place the "log4j-1.2.16.jar" file inside ${WAR_NAME}/WEB-INF/lib  directory--> 
           <copy file="${src.dir}/log4j-1.2.16.jar" todir="${tmp.dir}/WEB-INF/lib"/>

           <!-- Using this file we are going to satisfy the log4j dependency without directly placing the log4j.jar inside "WEB-INF/lib"-->
           <copy file="${src.dir}/jboss-deployment-structure.xml" todir="${tmp.dir}/META-INF"/> 

           <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>

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-9). 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-10). Now once the PATH is set In the command/Shell prompt you can move inside the directory “/home/userone/jboss-as-7.1.0.Final/standalone/TESTING/ApplicationLevelLog4jDemo” and then run the ant to build & deploy the webapplication. by running the command “ant deploy”

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

init:
   [delete] Deleting directory /home/userone/ApplicationLevelLog4jDemo/build
    [mkdir] Created dir: /home/userone/ApplicationLevelLog4jDemo/build
    [mkdir] Created dir: /home/userone/ApplicationLevelLog4jDemo/tmp
    [mkdir] Created dir: /home/userone/ApplicationLevelLog4jDemo/tmp/WEB-INF/lib
    [mkdir] Created dir: /home/userone/ApplicationLevelLog4jDemo/tmp/WEB-INF/classes
    [mkdir] Created dir: /home/userone/ApplicationLevelLog4jDemo/tmp/WEB-INF/META-INF
    [mkdir] Created dir: /home/userone/ApplicationLevelLog4jDemo/tmp/META-INF

build:
    [javac] Compiling 1 source file to /home/userone/ApplicationLevelLog4jDemo/tmp/WEB-INF/classes
     [copy] Copying 1 file to /home/userone/ApplicationLevelLog4jDemo/tmp/WEB-INF/classes
     [copy] Copying 1 file to /home/userone/ApplicationLevelLog4jDemo/tmp
     [copy] Copying 1 file to /home/userone/ApplicationLevelLog4jDemo/tmp/WEB-INF/classes
     [copy] Copying 1 file to /home/userone/ApplicationLevelLog4jDemo/tmp/WEB-INF/lib
     [copy] Copying 1 file to /home/userone/ApplicationLevelLog4jDemo/tmp/META-INF
      [jar] Building jar: /home/userone/ApplicationLevelLog4jDemo/tmp/Log4jDemo.war
     [copy] Copying 1 file to /home/userone/ApplicationLevelLog4jDemo/build

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

BUILD SUCCESSFUL
Total time: 2 seconds

Step-11). Now you will able to access your application. “http://localhost:8080/Log4jDemo/index.jsp” try to access the servlet and then check the “${jboss.server.log.dir}/MyAppLogsFile.log” file to see if the application level logging is happening properly.

.
.
Thanks
Middleware Magic Team


Apache CXF Service with Complex Type 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 use a Complex DataType inside our CXF Based WebService and 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.CR1bb” in this demo.

Point-5). How to use the JAXB Annotations like “XmlAccessorType”, “XmlAccessType” and “XmlType” to define and use the Complex Datatypes inside our webservice using the “SOAPBinding.ParameterStyle.BARE”

Point-6). 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/ComplexTypes_CXF_WebService_JBossAS7” then create another directory “src” inside the “/home/userone/ComplexTypes_CXF_WebService_JBossAS7”.

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

package ws;
import javax.jws.WebParam;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

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

public class DemoCXF
{
  @WebMethod()
  @WebResult(targetNamespace="http://test.org",name="updatedEmployee")
  public Employee processEmployeeSalary(@WebParam(partName = "employee", name = "employee", targetNamespace = "http://test.org")
        Employee emp, @WebParam(partName = "incrementAmount", name = "incrementAmount", targetNamespace = "http://test.org") Long incrementAmount)
  {
    System.out.println("[DemoCXF] Method Invoked....processEmployeeSalary");
    System.out.println("[DemoCXF] Before processing: "+emp);
    long incrementedSalary=emp.getEmpSalary()+incrementAmount;
    emp.setEmpSalary(incrementedSalary);
    System.out.println("[DemoCXF] After processing: "+emp);

    // Some Business Logic to Store the Employee's Updated Details in Database or Messaging System.

    return emp;
  }
}

Step-3). Write a simple Complex Type “Employee.java” as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7/src”:

package ws;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "employee", propOrder = {
    "empName",
    "empNo",
    "empSalary"
})

public class Employee
  {
    public String empName;
    public Long empNo;
    public Long empSalary;

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String value) {
        this.empName = value;
    }

    public Long getEmpNo() {
        return empNo;
    }

    public void setEmpNo(Long value) {
        this.empNo = value;
    }

    public Long getEmpSalary() {
        return empSalary;
    }

    public void setEmpSalary(Long value) {
        this.empSalary = value;
    }

  }

Step-4). Write the “web.xml” file to define the WebService as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7/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-5). Write the WebService client code “Test_CXF_Client.java” as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7/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());


        Employee emp=new Employee();
        emp.setEmpNo(1000L);
        emp.setEmpName("MiddlewaremagicEmployee");
        emp.setEmpSalary(6000L);

        System.out.println("nnBefore  EmpNo: "+emp.getEmpNo()+",  Name:"+emp.getEmpName()+",  Sal:"+emp.getEmpSalary());
        emp=port.processEmployeeSalary(emp,1000L);
        System.out.println("nnAfter   EmpNo: "+emp.getEmpNo()+",  Name:"+emp.getEmpName()+",  Sal:"+emp.getEmpSalary());
      }
  }

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/ComplexTypes_CXF_WebService_JBossAS7/src”:

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

log4j.appender.WSClientAppender=org.apache.log4j.ConsoleAppender
log4j.appender.WSClientAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.WSClientAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

NOTE: In this example we are using the “ConsoleAppender” whihc means in the Clients Console (Shell Pormpt) we will be able to see the Incoming and Outgoing soap request/response.

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/ComplexTypes_CXF_WebService_JBossAS7”:

<project name="JBoss_Complex_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="CXFComplexTypeDemoWAR" />
<property name="war.name" value="CXFComplexTypeDemo.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/CXFComplexTypeDemo?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/CXFComplexTypeDemo?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>
                <arg value="http://localhost:8080/CXFComplexTypeDemo"/>
            </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.

Point-7). Now Start your JBoss Profile as following from inside the directory “/home/userone/jboss-as-7.1.0.CR1b/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/ComplexTypes_CXF_WebService_JBossAS7” 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 ComplexTypes_CXF_WebService_JBossAS7]$ ant deploy
Buildfile: build.xml

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

build:
    [javac] Compiling 2 source files to /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/WEB-INF/classes
     [copy] Copying 1 file to /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/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/ComplexTypes_CXF_WebService_JBossAS7/build -classdir /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build -d /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/WEB-INF/wsdl -verbose -wsdl -cp ::::::::: -wrapperbean -createxsdimports ws.DemoCXF
[wsprovide] java2ws - Apache CXF 2.4.4
[wsprovide] 
      [jar] Building jar: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemo.war

deploy:
     [echo] *******************  Deploying   *********************
     [echo] ********** CXFComplexTypeDemo.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: 5 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):

15:53:37,164 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) Starting deployment of "CXFComplexTypeDemo.war"
15:53:37,203 INFO  [org.jboss.wsf.stack.cxf.metadata.MetadataBuilder] (MSC service thread 1-5) Add Service
 id=DemoCXF
 address=http://localhost:8080/CXFComplexTypeDemo
 implementor=ws.DemoCXF
 invoker=org.jboss.wsf.stack.cxf.JBossWSInvoker
 serviceName={http://test.org}DemoCXFService
 portName={http://test.org}DemoCXFPort
 wsdlLocation=null
 mtomEnabled=false
15:53:37,204 INFO  [org.jboss.ws.common.management.DefaultEndpointRegistry] (MSC service thread 1-5) register: jboss.ws:context=CXFComplexTypeDemo,endpoint=DemoCXF
15:53:37,213 INFO  [org.apache.cxf.service.factory.ReflectionServiceFactoryBean] (MSC service thread 1-5) Creating Service {http://test.org}DemoCXFService from class ws.DemoCXF
15:53:37,238 INFO  [org.apache.cxf.endpoint.ServerImpl] (MSC service thread 1-5) Setting the server's publish address to be http://localhost:8080/CXFComplexTypeDemo
15:53:37,247 INFO  [org.jboss.wsf.stack.cxf.deployment.WSDLFilePublisher] (MSC service thread 1-5) WSDL published to: file:/home/userone/jboss-as-7.1.0.CR1b/standalone/data/wsdl/CXFComplexTypeDemo.war/DemoCXFService.wsdl
15:53:37,251 INFO  [org.jboss.as.webservices] (MSC service thread 1-8) JBAS015539: Starting service jboss.ws.endpoint."CXFComplexTypeDemo.war".DemoCXF
15:53:37,260 INFO  [org.jboss.web] (MSC service thread 1-8) registering web context: /CXFComplexTypeDemo
15:53:37,268 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018565: Replaced deployment "CXFComplexTypeDemo.war" with deployment "CXFComplexTypeDemo.war"

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

[userone@localhost ComplexTypes_CXF_WebService_JBossAS7]$ 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/CXFComplexTypeDemo?wsdl          

client:
   [delete] Deleting directory /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
    [mkdir] Created dir: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
[wsconsume] Consuming wsdl: http://localhost:8080/CXFComplexTypeDemo?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/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
    [javac] Compiling 1 source file to /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
      [jar] Building jar: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff/DemoCXFClient.jar
   [delete] Deleting directory /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff/client
   [delete] Deleting: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff/log4j.properties

run:
      Feb 4, 2012 3:54:51 PM client.DemoCXFService <clinit>
      INFO: Can not initialize the default wsdl from service.wsdl
      15:54:52,844  INFO ReflectionServiceFactoryBean:366 - Creating Service {http://test.org}DemoCXFService from WSDL: http://localhost:8080/CXFComplexTypeDemo?wsdl
      
      
      Before  EmpNo: 1000,  Name:MiddlewaremagicEmployee,  Sal:6000
      15:54:53,275  INFO LoggingOutInterceptor:151 - Outbound Message
      ---------------------------
      ID: 1
      Address: http://localhost:8080/CXFComplexTypeDemo
      Encoding: UTF-8
      Content-Type: text/xml
      Headers: {Accept=[*/*], SOAPAction=[""]}
      Payload: 
            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
             <soap:Body>
                  <ns2:employee xmlns:ns2="http://test.org">
                      <empName>MiddlewaremagicEmployee</empName>
                      <empNo>1000</empNo>
                      <empSalary>6000</empSalary>
                  </ns2:employee>
                  <ns2:incrementAmount xmlns:ns2="http://test.org">1000</ns2:incrementAmount>
              </soap:Body>
            </soap:Envelope>
      --------------------------------------
      15:54:53,289  INFO LoggingInInterceptor:151 - Inbound Message
      ----------------------------
      ID: 1
      Response-Code: 200
      Encoding: UTF-8
      Content-Type: text/xml;charset=UTF-8
      Headers: {Content-Length=[268], content-type=, Date=[Sat, 04 Feb 2012 10:24:52 GMT], Server=[Apache-Coyote/1.1]}
      Payload: 
            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
               <soap:Body>
                  <ns2:updatedEmployee xmlns:ns2="http://test.org">
                      <empName>MiddlewaremagicEmployee</empName>
                      <empNo>1000</empNo>
                      <empSalary>7000</empSalary>
                  </ns2:updatedEmployee>
               </soap:Body>
            </soap:Envelope>
      --------------------------------------
      
      
      After   EmpNo: 1000,  Name:MiddlewaremagicEmployee,  Sal:7000

BUILD SUCCESSFUL
Total time: 9 seconds

Step-11). The Generated WSDL File will look something like following :
http://localhost:8080/CXFComplexTypeDemo?wsdl

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="DemoCXFService" targetNamespace="http://test.org" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.org" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:types>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test.org" xmlns="http://test.org" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="employee">
    <xs:sequence>
      <xs:element minOccurs="0" name="empName" type="xs:string"/>
      <xs:element minOccurs="0" name="empNo" type="xs:long"/>
      <xs:element minOccurs="0" name="empSalary" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="employee" nillable="true" type="employee"/>
  <xs:element name="incrementAmount" nillable="true" type="xs:long"/>
  <xs:element name="updatedEmployee" nillable="true" type="employee"/>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="processEmployeeSalaryResponse">
    <wsdl:part element="tns:updatedEmployee" name="updatedEmployee">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="processEmployeeSalary">
    <wsdl:part element="tns:employee" name="employee">
    </wsdl:part>
    <wsdl:part element="tns:incrementAmount" name="incrementAmount">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="DemoCXF">
    <wsdl:operation name="processEmployeeSalary">
      <wsdl:input message="tns:processEmployeeSalary" name="processEmployeeSalary">
    </wsdl:input>
      <wsdl:output message="tns:processEmployeeSalaryResponse" name="processEmployeeSalaryResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="DemoCXFServiceSoapBinding" type="tns:DemoCXF">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="processEmployeeSalary">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="processEmployeeSalary">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="processEmployeeSalaryResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="DemoCXFService">
    <wsdl:port binding="tns:DemoCXFServiceSoapBinding" name="DemoCXFPort">
      <soap:address location="http://localhost:8080/CXFComplexTypeDemo"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

.
.
Thanks 🙂
Middleware Magic Team


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