Monday, June 29, 2015

JAXB Concept-Marshalling and Unmarshalling

Prerequisites:
1.JAXB libraries :
You don't need extra library if you have JDK version >1.6(by default jdk ship with jaxb-2.x library), but for jdk version <1.6 you need  to have jaxb lib (jaxb-api.jar | jaxb-impl.jar).
2.JAVA Model Classes
a:-java model classes can be generated using xjc tool(which ship with jdk)  and schema/xsd definition.
b:-you write yourself using Jaxb annotation.



Marshalling (Object to XML)
UnMarshalling (XML to Object)
JAXB Arch Diagram :



Monday, June 22, 2015

How to refer links in blogs comment

Some times when you are commenting on blog you want to refer a link or url..
In a comments,If you just want to link url or a page of your blog.

here is a little trick to add links with comments in blog-blogger.

Step 1: Make sure you are on the page for a particular post.

Step 2: make a hyperlink for the comments like below one.


<a href="http://explorejavacoding.blogspot.com/">learn java with coding</a>

Step 3: Simply replace the href="http://xyz.com" and hypelink text="abc". 



and thats it ,you are done ..simple isnt it. , it would show up your comments with link  as 'abc' which will be redirected to url.

Give it a try by commenting...

Thanks for reading!!......


Sunday, June 21, 2015

How to take thread dump from a running JVM(java)

What is thread dump in java?.

A thread dump is a snapshot of all the Java threads that are currently active(whether blocked , running,deadlocked,waiting,suspended,runnable) in a Java Virtual Machine (JVM).

Why we need Thread Dump?.

if CPU usage is  very high due to running java program/application
if Processing performance of a java program/application is  Slow
if Application is not responding.

How to take Thread Dump?.

Below  are the step by step process.

Step 1: Get PID(processID) of java process.


JDK ships with jps command exe which lists all java process ids.
There are multiple way to get the pid..
root>jps

22026 Jps
8555 Bootstrap    (my tomcat server)

root>jps -l

29928 sun.tools.jps.Jps
8555 org.apache.catalina.startup.Bootstrap (my tomcat server)

root>jps -v

13861 Jps -Dapplication.home=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64
8555 Bootstrap -Xms512m -Xmx1024m -XX:MaxPermSize=256m (my tomcat server)

UNIX/Linux Users:
ps -ef | grep java
ps -ef | grep java | awk '{print $2}' ... it will give you PID only.

ps -fC java

Windows Users:
Press Ctrl+Shift+Esc to open the task manager and find  PID of  java process.
jps will work in all enviroment( unix/windows).

there are other way also to take thread dump..
a.JVisualVM if you are able to run this becoze sometime you have limited access on production server.
b.if you want to see/view the threads of a java process instead of  thread dump.
c.JConsole exe tool.
d.programmaticly using Mbean--ManagementFactory.getThreadMXBean() ,

Step 2: Take a Thread Dump from the JVM.

You can use kill -QUIT or kill -3 command command and get threadDump of java process @Linux OS.
kill -3 <pid>

JDK ships with jstack command exe which can be used for taking thread dump of java process.

jstack pid-19327 >> ./app-root/logs/threaddumps-poe-2.log

here is sample for reference.

Full thread dump OpenJDK 64-Bit Server VM (24.65-b04 mixed mode):

"hz._hzInstance_1_dev.cached.thread-2615" prio=10 tid=0x00007f420400a800 nid=0x2d3f waiting on condition [0x00007f42731c5000]
   java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for  <0x00000000c190e018> (a java.util.concurrent.SynchronousQueue$TransferStack)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
at java.util.concurrent.Synch ronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)
at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:359)
at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:942)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

"Attach Listener" daemon prio=10 tid=0x00007f4250002000 nid=0x713e waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"MultiThreadedHttpConnectionManager cleanup" daemon prio=10 tid=0x00007f42242c7800 nid=0x4c07 in Object.wait() [0x00007f42730c4000]
   java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked <0x00000000c26a4da0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$ReferenceQueueThread.run(MultiThreadedHttpConnectionManager.java:1122)

"[DSVQuartzThreads]" prio=10 tid=0x00007f422404f800 nid=0x4c95 in Object.wait() [0x00007f42635f4000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:410)
- locked <0x00000000c1dc5818> (a java.lang.Object)

Step 3: Analyze Thread Dump of JVM.


Use IBM thread analyzer runnable gui jar(jca457.jar) for opening the thread Dump and see all the information->
ftp://public.dhe.ibm.com/software/websphere/appserv/support/tools/jca/jca457.jar

Java Thread Dump-1
Thread Dump in IBM Thread Analyzer


Java Thread Dump-2
Clickable Graphical view of Thread Dump



Thanks for reading!!......



Monday, May 18, 2015

How to Read file's content as a String in java.

Some times we quickly want to pass the file content as String in java method or for unit testing..

Here are the one-line solution directly from my java utilities.

1. using java.util.Scanner ...

import java.util.Scanner;
import java.io.FileNotFoundException;

private static String readFileContentAsString(String fileNameWithPath) throws FileNotFoundException{
return new Scanner(new File(fileNameWithPath)).useDelimiter("\\Z").next();
}

2. using java.nio.*(java 7)....

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

private static String readFile(String path, Charset encoding)   throws IOException {
    return new String(Files.readAllBytes(Paths.get(path)), encoding);
}

3.using apache commons-io library.

import org.apache.commons.io.IOUtils;
private static String readFile(String path) throws IOException{
    return IOUtils.toString(new FileInputStream(new File(path)), "UTF-8");
}

4.using google guava library.

import com.google.common.base.Charsets;
import com.google.common.io.Files;

private static String readFile(String path) throws IOException{
  return  Files.toString(new File(path), Charsets.UTF_8);
}
           


Thanks for reading!!......