Friday, February 28, 2014

REST API Knowledge JAXB, JAX RS, etc.

What JAXB Does?

1. Generating Java Code from XML Schema

  Step 1: Create XSD File

<?xml version="1.0"?>
<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           targetNamespace="http://a.bc/xsd/TestScheme"
           xmlns="http://a.bc/xsd/TestScheme">
    <xs:element name="govind" type="Student"/>

    <xs:complexType name="Student">
        <xs:sequence>
            <xs:element name="sname" type="xs:string"/>
            <xs:element name="lname" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Step 2. Run below command

[your java home dir]\bin\xjc.exe c:\test_schema.xsd

Step3 : Check out the generated output
C:\Program Files (x86)\IBM\SDP\jdk\bin>xjc.exe c:\test_schema.xsd
parsing a schema...
compiling a schema...
bc\a\xsd\testscheme\ObjectFactory.java
bc\a\xsd\testscheme\PersonInfoType.java
bc\a\xsd\testscheme\package-info.java

ObjectFactory.java
@XmlRegistry
public class ObjectFactory {

    private final static QName _Govind_QNAME = new QName("http://a.bc/xsd/TestScheme", "govind");

     public ObjectFactory() {  }
    public Student createStudent() {
        return new Student()}

    @XmlElementDecl(namespace = "http://a.bc/xsd/TestScheme", name = "govind")
    public JAXBElement<Student> createGovind(Student value) {
        return new JAXBElement<Student>(_Govind_QNAME, Student.class, null, value);
    }}

package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://a.bc/xsd/TestScheme", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package bc.a.xsd.testscheme;

Student.java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Student", namespace = "http://a.bc/xsd/TestScheme", propOrder = {
    "sname",
    "lname"
})
public class Student {
    @XmlElement(required = true)
    protected String sname;
    @XmlElement(required = true)
    protected String lname;

    public String getSname() {
        return sname;
    }    public void setSname(String value) {
        this.sname = value;
    }

    public String getLname() {
        return lname;
    }
    public void setLname(String value) {
        this.lname = value;
    }

Points:

JAXB Limitation.
I worked on JAXB, as per my opinion its a nice way of dealing with data between XML and Java objects. The Positive sides are its proven and better in performance and control over the data during runtime. With a good usage of built tools or scripts it will takes away lot of coding efforts.
I found the configuration part is not a straight away task, and spent hours in getting the development environment setup.
However I dropped this solution due to a silly limitation I faced. My XML Schema Definition ( XSD ) has a attribute/element with name "value" and that I have to use XSD as it is. This very little constraint forced the my binding step XJC failed with a Error "Property 'Value' already used."
This is due to the JAXB implementation, the binding process tries to create Java objects out of XSD by adding few attributes to each class and one of them being a value attribute. When it processed my XSD it complained that there is already a property with that name.
 
http://theopentutorials.com/examples/java/jaxb/generate-java-class-from-xml-schema-in-eclipse-ide/

1. JAXB (xjc utility) is installed together with JDK6 by default.
2. Using Eclipse IDE:
  1. Download http://java.net/downloads/jaxb-workshop/IDE%20plugins/org.jvnet.jaxbw.zip
  2. Extract the zip file .
  3. Place the org.jvnet.jaxbw.eclipse_1.0.0 folder into .eclipse\plugins folder
  4. Restart the eclipse.
  5. Right click on XSD file and you can find contect menu. JAXB 2.0 -> Run XJC .
  1. copy the xsd into a new/existing project.
  2. Make sure you have JAXB required JARs in you classpath. You can download one here.
  3. Right click on the XSD file -> Generate -> JAXB classes. 

 


2.  Java object to / from XML file

Example link

Eclipse Article

Eclipse

Technical Good Links

System Property

SSL CA JKS TS Management

Android Article

JUnit Article

Responsibility of Tech Leads




























-----------------------------
Trust Store in Java
-----------------------
ClassFileServer:
private static ServerSocketFactory getServerSocketFactory(String type) {
    if (type.equals("TLS")) {
        SSLServerSocketFactory ssf = null;

        Properties systemProps = System.getProperties();
        systemProps.put( "javax.net.ssl.trustStore", "cacerts.jks");
        systemProps.put( "javax.net.ssl.trustStorePassword", "p@ssw0rd");
        System.setProperties(systemProps);

        try {
            // set up key manager to do server authentication
            SSLContext ctx;
            KeyManagerFactory kmf;
            KeyStore ks;
            char[] passphrase = "p@ssw0rd".toCharArray();

            ctx = SSLContext.getInstance("TLS");
            kmf = KeyManagerFactory.getInstance("SunX509");
            ks = KeyStore.getInstance("JKS");
            ks.load(new FileInputStream("keystore.jks"), passphrase);
            kmf.init(ks, passphrase);
            ctx.init(kmf.getKeyManagers(), null, null);

            ssf = ctx.getServerSocketFactory();
            return ssf;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
SSLSocketClientWithClientAuth:
    try {

        /*
         * Set up a key manager for client authentication
         * if asked by the server.  Use the implementation's
         * default TrustStore and secureRandom routines.
         */
        Properties systemProps = System.getProperties();
        systemProps.put( "javax.net.ssl.trustStore", "cacerts.jks");
        systemProps.put( "javax.net.ssl.trustStorePassword", "changeit");
        System.setProperties(systemProps);

        SSLSocketFactory factory = null;
        try {
            SSLContext ctx;
            KeyManagerFactory kmf;
            KeyStore ks;
            char[] passphrase = "changeit".toCharArray();

            ctx = SSLContext.getInstance("TLS");
            kmf = KeyManagerFactory.getInstance("SunX509");
            ks = KeyStore.getInstance("JKS");

            ks.load(new FileInputStream("keystore.jks"), passphrase);

            kmf.init(ks, passphrase);
            ctx.init(kmf.getKeyManagers(), null, null);

            factory = ctx.getSocketFactory();
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }

        SSLSocket socket = (SSLSocket)factory.createSocket(host, port);

Thursday, February 27, 2014

Tomcat JVM - need to know

Tomcat JVM - What You Need To Know

Apache Tomcat is a Java servlet container, and is run on a Java Virtual Machine, or JVM.  Tomcat utilizes the Java servlet specification to execute servlets generated by requests, often with the help of JSP pages, allowing dynamic content to be generated much more efficiently than with a CGI script.
If you want to run a high-performing installation of Tomcat, taking some time to learn about your JVM is essential.  In this article, we'll learn how Tomcat and the JVM interact, look at a few of the different JVMs available, explain how to tune the JVM for better performance, and provide information about some of the tools available for monitoring your JVM's performance.

How Tomcat Interacts With The JVM

Utilizing servlets allows the JVM to handle each request within a separate Java thread, as each servlet is in fact a standard Java class, with special elements that allow it to respond to HTTP requests.
Tomcat's main function is to pass HTTP requests to the correct components to serve them, and return the dynamically generated results to the correct location after the JVM has processed them.  If the JVM can't efficiently serve the requests Tomcat passes to it, Tomcat's performance will be negatively affected.

Choosing the Right JVM

There are many JVMs to choose from, and Tomcat runs on many of them, from open source projects such as Sun Microsystem's HotSpot or Apache Harmony, to proprietary JVMs like Azul VM.
Despite the wide variety of available JVM flavors, the majority of Tomcat users favor Sun Microsystem's HotSpot JVM, because its just-in-time compilation and adaptive optimization features are particularly suited for efficiently handling Tomcat's servlet requests.
So for the majority of Tomcat users, HotSpot is the JVM to use.  However, if you are attracted a feature that is specific to a certain JDK, there is nothing wrong with installing Tomcat on two different JVMs and running some benchmarks to see which solution is best for your needs.  In the end, it's a balancing act.  Choose the JVM that provides the best balance of performance and features for your site.

How to Configure Tomcat's Default JVM Preferences

Once you decide on a JVM and install it on your server, configuring Tomcat to run on it is a very simple process.  Simply edit catalina.sh, found in Tomcat's bin folder, and change the JAVA_HOME environment variable to the directory of your chosen JVM's JDK.  When you restart Tomcat, it will be running on your new JVM.

Optimizing your JVM for Best Performance

The better your JVM performs, the better your installation of Tomcat will perform.  It's as simple as that.  Getting the most out of your JVM is a matter of configuring its settings to match your real-world performance needs as closely as possible.  Update your JVM to the latest version, establish some accurate benchmarks so you have a way of quantifying any changes you make, and then get down to business.

Effective Memory Management

The main thing to consider when tuning your JVM for Tomcat performance is how to avoid wasting memory and draining your server's power to process requests.  Certain automatic JVM processes, such as garbage collection and memory reallocation, can chew through memory if they occur more frequently than necessary.  You can make sure these processes only occur when they need to by using the JAVA_OPTS -Xmx and -Xms switches to control how JVM handles its heap memory.
If your JVM is invoking garbage collection too frequently, use the -Xmx switch to start the JVM with a higher maximum heap memory.  This will free up CPU time for the processes you really care about.
To get even more out of this change, you can include the -Xms switch.  This switch makes the JVM's initial heap memory size equal to the maximum allocated memory.  This means the JVM will never have to reallocate more memory, a costly process that can eat up power you want being used to serve incoming requests.
If your web applications can handle a lower garbage collection throughput, you can also experiment with the -Xincgc switch, which enables incremental garbage collection.  This means that rather than halting in place to perform garbage collection tasks, the JVM will execute garbage collection in small phases.
It can be tricky to determine the most balanced configuration for your site's needs.  Fortunately, there's an easy way to capture data on how your JVM is handling garbage collection.  Simply use the -verbose:gc switch to generate logs you can use to help you arrive at the best solution.

Configuring Threads

Next, let's take a look at the way your JVM handles threads.  There are two types of Java threads - green and native.  Native threads are scheduled by your OS, while green threads are managed entirely within the user space of your Java Virtual Machine.  If your JVM supports both, you should try both models to determine the best choice for your site.
Generally, native threads offer the best performance, especially if you are running a lot of I/O bound applications (which is very likely, since you are running Tomcat).  However, green threads outperform native threads in some specific areas, such as synchronization and thread activation.  Try both and see which option gives you the biggest performance boost.

Managing Your JVM

Tuning for performance is not a finite process.  Usage situations change over time, and problems that are not immediately apparent can expose themselves over a longer period of time.  There are a number of tools available to help you keep an eye on your JVM's performance.
One of the most convenient solution is VisualVM, a tool that is packaged with the JDK, and can provide you with great performance statistics.  Other commonly used JVM monitoring tools included with the SDK include console, jps, and jstack.  Run regular tests on your JVM to make sure its configuration still suits your needs, and you can be sure that your Tomcat instances will always perform at their best!

Wednesday, February 26, 2014

What is Core, Processor and CPU?

The terms 'Processor', 'Core', and 'CPU' are all poorly defined and have undergone many changes in meaning over the year as computer architectures have evolved and changed.

Under modern terminology, 'Processor' and 'CPU' mean more or less exactly the same thing. It would be more accurate to refer to the 'processor package' because there's no real standard to what a the package contains. Very old CPUs from 15-20 years ago contained little more than the bare minimum to execute tasks.











A core is usually the basic computation unit of the CPU - it can run a single program context (or multiple ones if it supports HW threads such as hyperthreading on intel CPUs), maintaining the correct program state, registers, and correct execution order, and performing the operations through ALUs. For optmization purposes, a core can also hold on-core caches with copies of frequently used memory chunks.

A CPU may have one or more cores to perform tasks at a given time. These tasks are usually software processes and threads that the OS schedules. Note that the OS may have many threads to run, but the CPU can only run at a given time X such tasks, where X = number cores * number of HW-threads per core. The rest would have to wait for the OS to schedule them whether by preempting currently running tasks or any other means.

In addition to the one or many cores, the CPU will include some interconnect that connects the cores to the outside world, and usually also a large "last-level" shared cache. There are multiple other key elements required to make a CPU work, but their exact locations may differ according to design. You'll need a memory controller to talk to the memory, IO controllers (display, PCIe, USB, etc..). In the past these elements were outside the CPU, in the complementary "chipset", but most modern design have integrated them into the CPU.

In addition the CPU may have an integrated GPU, and pretty much everything else the designer wanted to keep close for performance, power and manufacturing considerations. CPU design is mostly trending in to what's called "SOC" - system on chip.

They contained the ALUs, fetch and decode hardware, Instruction pipeline, Interrupt handling hardware, some IO control hardware and that's about it. After this, cache memory was added to the CPU to improve execution

Then, the execution parts of the processor were duplicated. The ALUs, fetch and decode, instruction pipline, and some cache memory were organized into what we now call "cores". Each core is capable of functioning on its own and contain all the resources necessary to perform computational tasks that do not involve interacting with components outside the CPU. IO Control, interrupt handling, etc... were all shared between all the cores.

More recently the memory controller itself has been moved into the CPU package. It sits along side the CPU cores but it is not part of them. Thus it is part of the package, or part of the processor/CPU but it is not part of the 'cores'. Intel used to specifically refer to this as the 'un-core'.

This gets even more complicated when we talk about systems which have multiple physical processor packages installed. Many server and workstation platforms can have 2, 4, or even more processor packages installed. Each processor package contains the same hardware.

Thus, the number of 'cores' in a machine can be computed by taking the number of cores per package and multiplying it by the number of packages in the system. A computer which has two quad core processors has the same number of cores as a computer which has a single octal core processor.


In simpler terms,

Core: Cores are what handle the arbitrary mathematical and logical workloads. They take high level machine instructions (x86, ARM, MIPS, etc...) and 'decode' them into physical circuit operations. Many other parts of the system, such as GPUs and chipsets operate in a similar manner but are designed with specific purposes in mind which makes them more efficient at these particular tasks. CPU cores are designed with a general purpose, making them jacks of all trades.

Processor / CPU: The combination of one or more 'cores' with supporting hardware and shared resources.

Processor package: The physical casing in which one or more processors or CPUs is contained. The pinout from the package is what allows the processor to interface with the rest of the system. Some processor packages may contain more than one processor die inside of them, or may have the cores and shared resources on separate pieces. The contents of the processor package and how it is organized are up to the manufacturer, hence the distinction between Processor and Processor Package.

For example, the Core 2 Quad processors were actually two individual Core 2 Duo processors in the same package with some very simple supporting hardware to allow them to work together. This stands in contrast to the more modern Sandybridge processors which have all 4 cores and supporting hardware on one chip, a far more efficient design.