Wednesday, July 18, 2012

JAXB2 Plugin for XSD to Java class/VOs

Relying our Model from XML is always considered a better approach as it simplify the relationship and maintenance.
We can use Jaxb2 plugin for generating model driven Java classes.
Apart from this the other benefit is that if you are going to use them in webservices communication you will never encounter of issue which comes during marshaling and de-marshaling by the Webservice API vendors say CXF/ Restlet.

In this post we are going to see conversion from XSD to Java with some utility methods. 
JAXB2 Basics provides a package of plugins which can generate such utility code

 Below is schema.xsd file

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="1.0">

    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings choiceContentProperty="true" generateIsSetMethod="true">
                <!--xjc:noValidator />
                <xjc:noValidatingUnmarshaller /-->
            </jaxb:globalBindings>
            <jaxb:schemaBindings>
                <jaxb:package name="com.techiehouse.pojo"/>
            </jaxb:schemaBindings>
        </xs:appinfo>
    </xs:annotation>

    <xs:element name="heteroSequence" type="heteroSequenceType"/>
    <xs:complexType name="heteroSequenceType">
        <xs:sequence>
            <xs:element name="sa" type="xs:string"/>
            <xs:element name="sb" type="xs:string"/>
            <xs:sequence>
                <xs:element name="sc" type="xs:string"/>
                <xs:element name="sd" type="xs:string"/>
            </xs:sequence>
            <xs:sequence maxOccurs="unbounded">
                <xs:element name="se" type="xs:string"/>
                <xs:element name="sf" type="sequenceType"/>
            </xs:sequence>
        </xs:sequence>
    </xs:complexType>


    <xs:element name="simpleTypes" type="simpleTypesType"/>
    <xs:complexType name="simpleTypesType">
        <xs:sequence>
            <xs:element name="base64Binary" type="xs:base64Binary" minOccurs="0"/>
            <xs:element name="hexBinary" type="xs:hexBinary" minOccurs="0"/>
            <xs:element name="NMTOKEN" type="xs:NMTOKEN" minOccurs="0"/>
            <xs:element name="NMTOKENS" type="xs:NMTOKENS" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="elementWithListAttribute1">
        <xs:attribute name="list1" type="listType1" use="optional"/>
    </xs:complexType>
    <xs:simpleType name="listType1">
        <xs:list itemType="patternType1"/>
    </xs:simpleType>
    <xs:simpleType name="patternType1">
        <xs:restriction base="xs:string">
            <xs:length value="9"/>
            <xs:pattern value="[A-Z]{2}([0-9]|[A-Z]){7}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

wheras POM.xml should look like
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics-sample-basic-maven</artifactId>
    <version>0.6.4</version>
    <packaging>jar</packaging>
    <name>JAXB2 Basics - Sample [basic-maven]</name>
    <dependencies>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.5-2</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-runtime</artifactId>
            <version>0.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-testing</artifactId>
            <version>0.6.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <defaultGoal>test</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-XtoString</arg>
                        <arg>-Xequals</arg>
                        <arg>-XhashCode</arg>
                        <arg>-Xinheritance</arg>
                        <arg>-Xcopyable</arg>
                        <arg>-XenumValue</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.4</version>
                        </plugin>
                    </plugins>
                </configuration>
            </plugin>
            <plugin>
                <inherited>true</inherited>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Maven will generate the Java source file at the mentioned package location.

References: http://confluence.highsource.org/display/J2B/Home

Persistence Specification JDO Vs JPA

We have two standardized Persistence specifications which are  available for use  in the  Market. In most of the cases we think that Hibernate and JPA are the obvious choice for our persistence strategy.
However we completely miss out other options like JDO which has much more capabilities than the Hibernate and JPA implementers provide. 

We should realize the fact that JDO supports much more ORM features than JPA .And the another biggest difference is that JPA is limited to only relational database as data storage, whereas JDO is agnostic to data store technology.

Let’s see the comparisons between JPA vs JDO In detail:

JDO:
  • Largely datastore type agnostic
  • Object-relational mapping standard and a transparent
    object persistence standard both
  • object-oriented
  • JDOQL is very close to Java syntax
  •   Lightweight persistence framework started in 2001
  • latest  version JDO3.
JPA :
  • Target only relational database
  • Object-relational mapping standard
  • A lightweight persistence framework started in 2006. Latest version JPA2


  1.    There are fewer restrictions on the nature of POJOs with JDO than with JPA - if you use 'final' at all, be prepared to have to re-write things for use with JPA. You also have to include an identity field in your POJOs for JPA but not with JDO.
  2.   JDO can persist more types of data structure than JPA. For example, in JPA, we  are limited to persisting collections and maps of Entities. In JDO you can also persist collections and maps of immutable objects like String, Date, BigDecimal etc. Basically, JDO is less intrusive and more versatile - you can persist a wider range of objects, and can more easily persist legacy objects that have not been coded with the framework in mind, and you can persist to more than relational databases .
  3.   JPA is a subset of JDO 2.0 features
  4.     JDO commercial implementations and open source projects also offer a JPA API implementation as an alternative access to their underlying persistence engines 

 JPA 2 supportability

Since nowadays NoSQL data base is speeding very fast,  JDO framework could  gain the pace in Market .
Fore more hit http://db.apache.org/jdo/jdo_v_jpa.html

Tuesday, July 17, 2012

Tomcat Performance Tunning

In order to handle scalability, webserver needs to be controlled by certain configuration.
In this post we wil take Tomcat as a web server and go through ceratin configurations so that It handle heavey requests.

  • Increase JVM heap memory
    JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8
    -server -Xms1024m -Xmx1024m
    -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m
    -XX:MaxPermSize=512m -XX:+DisableExplicitGC"
     
  • Resolve JRE memory leaks Tomcat server.xml contains
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"
     />
     
  • Thread pool setting
    <Connector port="8080" address="localhost"
    maxThreads="250" maxHttpHeaderSize="8192"
    emptySessionPath="true" protocol="HTTP/1.1"
    enableLookups="false" redirectPort="8181" acceptCount="100"
    connectionTimeout="20000" disableUploadTimeout="true" />
     If the number of threads reaches much higher say more than 750 its better to 
    go for clustering. 
    Higher is the max threads more will be the application start up time.
    
    
  • Compression    : server.xml configuration
    <Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8181" compression="500"
    compressableMimeType="text/html,text/xml,text/plain,application/octet-stream" />
     
  • Database performance tuning : Twiking maxIdle, maxActive, and maxWait...
  • Tomcat Native Library
  • Go for stateless application
  • Database connection pooling configuration :Set the max active connection,  connection waiting time out , Idle connection properly.

Thursday, July 12, 2012

Controlling JSONProvider behavior

Sometime JsonProvider needs to be controlled so that It can marshal  the response as per desired.
A typical problem with the provider is It does not transform List element into Json array when the list size is one.

We just need to pass the arraykeys and set serializeAsArray to true to resolve the problem.
<util:list id="jsonKeys">
        <value>listElementName</value>
          </util:list>


        <jaxrs:providers>
            <bean class="org.apache.cxf.jaxrs.provider.JSONProvider">
                  .............................
                ....................
                <property name="serializeAsArray" value="true" />
                <property name="arrayKeys" ref="jsonKeys"/>
            </bean>
        </jaxrs:providers>

Tuesday, July 10, 2012

Remote Debugging

Tomcat:

export JPDA_ADDRESS=8000
export JPDA_TRANSPORT=dt_socket
bin/catalina.sh jpda start