Friday, September 28, 2012

What is the difference between a synchronized method and synchronized block in Java?


First, we write the following code for synchronized method. 
Non-static synchronized method:
public synchronized void method(){
            System.out.println ("non-static synchronized method.");
      }
As per synchronization only one thread will process synchronized method at a time. That means, when one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object and it is equivalent to following code as it blocks on its object.
Non-static synchronized block:
public void method(){
            synchronized (this) {
                  System.out.println("non-static synchronized block.");
            }                
      }

And for static,
Static synchronized method:
      public static void method() {
            System.out.println("static synchronized method.");
}

And it is equivalent to,  
Static synchronized block:
public void method(){
            synchronized (SynchronizationDemo.class) {
                  System.out.println("static synchronized block.");
            }                
      }
Here, JVM converts the synchronized methods to synchronized blocks implicitly. Synchronized blocks are more convenient to specify synchronization to specific statements or objects (not null) like following.
      public void method() {
            synchronized(notnullobject){
                  System.out.println("synchronization on specific object.");
            }
            System.out.println("you can leave thread safe statements from block.");
      }
     

Wednesday, September 26, 2012

WSDL File Document Literal


xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://calc.lnn.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://calc.lnn.com" xmlns:intf="http://calc.lnn.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://calc.lnn.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <complexType name="InputParams">
    <sequence>
     <element name="xnum" type="xsd:double"/>
     <element name="ynum" type="xsd:double"/>
    </sequence>
   </complexType>
   <element name="inputParams" type="impl:InputParams"/>
   <complexType name="OutputParams">
    <sequence>
     <element name="add" type="xsd:double"/>
     <element name="sub" type="xsd:double"/>
    </sequence>
   </complexType>
   <element name="executeReturn" type="impl:OutputParams"/>
  </schema>
 </wsdl:types>

   <wsdl:message name="executeRequest">

      <wsdl:part element="impl:inputParams" name="inputParams">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="executeResponse">

      <wsdl:part element="impl:executeReturn" name="executeReturn">

      </wsdl:part>

   </wsdl:message>

   <wsdl:portType name="Operation">

      <wsdl:operation name="execute" parameterOrder="inputParams">

         <wsdl:input message="impl:executeRequest" name="executeRequest">

       </wsdl:input>

         <wsdl:output message="impl:executeResponse" name="executeResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="OperationSoapBinding" type="impl:Operation">

      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

      <wsdl:operation name="execute">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="executeRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="executeResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="OperationService">

      <wsdl:port binding="impl:OperationSoapBinding" name="Operation">

         <wsdlsoap:address location="http://localhost:8080/first/services/Operation"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

WSDL File Document/Literal (wrapped)


xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://calc.lnn.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://calc.lnn.com" xmlns:intf="http://calc.lnn.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://calc.lnn.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="execute">
    <complexType>
     <sequence>
      <element name="inputParams" type="impl:InputParams"/>
     </sequence>
    </complexType>
   </element>
   <complexType name="InputParams">
    <sequence>
     <element name="xnum" type="xsd:double"/>
     <element name="ynum" type="xsd:double"/>
    </sequence>
   </complexType>
   <element name="executeResponse">
    <complexType>
     <sequence>
      <element name="executeReturn" type="impl:OutputParams"/>
     </sequence>
    </complexType>
   </element>
   <complexType name="OutputParams">
    <sequence>
     <element name="add" type="xsd:double"/>
     <element name="sub" type="xsd:double"/>
    </sequence>
   </complexType>
  </schema>
 </wsdl:types>

   <wsdl:message name="executeRequest">

      <wsdl:part element="impl:execute" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="executeResponse">

      <wsdl:part element="impl:executeResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:portType name="Operation">

      <wsdl:operation name="execute">

         <wsdl:input message="impl:executeRequest" name="executeRequest">

       </wsdl:input>

         <wsdl:output message="impl:executeResponse" name="executeResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="OperationSoapBinding" type="impl:Operation">

      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

      <wsdl:operation name="execute">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="executeRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="executeResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="OperationService">

      <wsdl:port binding="impl:OperationSoapBinding" name="Operation">

         <wsdlsoap:address location="http://localhost:8080/first/services/Operation"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

WSDL File Rpc/Encoded


xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://calc.lnn.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://calc.lnn.com" xmlns:intf="http://calc.lnn.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <wsdl:types>
  <schema targetNamespace="http://calc.lnn.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
   <complexType name="InputParams">
    <sequence>
     <element name="xnum" type="xsd:double"/>
     <element name="ynum" type="xsd:double"/>
    </sequence>
   </complexType>
   <complexType name="OutputParams">
    <sequence>
     <element name="add" type="xsd:double"/>
     <element name="sub" type="xsd:double"/>
    </sequence>
   </complexType>
  </schema>
 </wsdl:types>

   <wsdl:message name="executeRequest">

      <wsdl:part name="inputParams" type="impl:InputParams">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="executeResponse">

      <wsdl:part name="executeReturn" type="impl:OutputParams">

      </wsdl:part>

   </wsdl:message>

   <wsdl:portType name="Operation">

      <wsdl:operation name="execute" parameterOrder="inputParams">

         <wsdl:input message="impl:executeRequest" name="executeRequest">

       </wsdl:input>

         <wsdl:output message="impl:executeResponse" name="executeResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="OperationSoapBinding" type="impl:Operation">

      <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

      <wsdl:operation name="execute">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="executeRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://calc.lnn.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="executeResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://calc.lnn.com" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="OperationService">

      <wsdl:port binding="impl:OperationSoapBinding" name="Operation">

         <wsdlsoap:address location="http://localhost:8080/first/services/Operation"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>