2 Years….

You registered on WordPress.com 2 years ago!
Thanks for flying with us. Keep up the good blogging!
Thanks everyone
Ubuntu – Lessons – 1
Have started using Ubuntu recently and still getting used to the new shortcuts and others.
Since the installation was also done by me, encountered few issues.
Following are the few things which one would encounter and have included the links to save time.
How to install JDK on Ubuntu…
https://help.ubuntu.com/community/Java
wget http://download.oracle.com/otn-pub/java/jdk/6u34-b04/jdk-6u34-linux-i586.bin
$ chmod u+x jdk-6u34-linux-i586.bin
$ ./jdk-6u34-linux-i586.bin
$ sudo mkdir -p /usr/lib/jvm
$ sudo mv jdk1.6.0_34 /usr/lib/jvm/
$ sudo update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.6.0_34/bin/java” 1
$ sudo update-alternatives –install “/usr/bin/javac” “javac” “/usr/lib/jvm/jdk1.6.0_34/bin/javac” 1
$ sudo update-alternatives –install “/usr/lib/mozilla/plugins/libjavaplugin.so” “mozilla-javaplugin.so” “/usr/lib/jvm/jdk1.6.0_34/jre/lib/i386/libnpjp2.so” 1
$ sudo update-alternatives –install “/usr/bin/javaws” “javaws” “/usr/lib/jvm/jre1.6.0_34/bin/javaws” 1
IMPORTANT choose the java you installed as default
$ sudo update-alternatives –config java
$ sudo update-alternatives –config javac
$ sudo update-alternatives –config mozilla-javaplugin.so
$ sudo update-alternatives –config javaws
How to enable hibernation
http://askubuntu.com/questions/94754/how-to-enable-hibernation
/etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla
[Re-enable hibernate by default]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes
How to install MongoDB
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
sudo service mongodb stop
How to solve the VirtualBox Error
sudo /etc/init.d/vboxdrv setup
sudo apt-get install linux-headers-$(uname -r)
Thanks everyone who have contributed to the solutions. Without those, it is not possible for people like me to proceed further.
XML->JSON->HashMap
Yes, it is long time since i posted…
Was just trying to see how a XML can be converted to JSON and to HashMap.
The situation is very imaginary.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;public class XML2JSONConvertor {
public static void main(String[] args) throws Exception {InputStream is = new FileInputStream(new File(
“e:\\jagannathan\\personal\\java-projects\\secondtest.xml”));
String xml = IOUtils.toString(is);XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xml);System.out.println(json.toString(2));
printJSON(json.toString(2));
}
public static void printJSON(String jsonString) {
ObjectMapper mapper = new ObjectMapper();try {
Map<String, Object> jsonInMap = mapper.readValue(jsonString,
new TypeReference<Map<String, Object>>() {
});List<String> keys = new ArrayList<String>(jsonInMap.keySet());
for (String key : keys) {
System.out.println(key + “: ” + jsonInMap.get(key));
}} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Dependencies
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.5</version>
</dependency><dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.0</version>
</dependency>
The Input XML
<?xml version=”1.0″ encoding=”UTF-8″?>
<company>
<name>Jags Inc</name>
<employees>
<employee>
<name>Jagan</name>
<sex>Male</sex>
<dob>24-jul</dob>
</employee>
<employee>
<name>Satya</name>
<sex>Male</sex>
<dob>24-apr</dob>
</employee>
</employees>
</company>
The output
7 Feb, 2013 7:20:50 PM net.sf.json.xml.XMLSerializer getType
INFO: Using default type string
{
“name”: “Jags Inc”,
“employees”: [
{
"name": "Jagan",
"sex": "Male",
"dob": "24-jul"
},
{
"name": "Satya",
"sex": "Male",
"dob": "24-apr"
}
]
}
name: Jags Inc
employees: [{name=Jagan, sex=Male, dob=24-jul}, {name=Satya, sex=Male, dob=24-apr}]
Hibernate 4
Was trying to have a small working PoC on Multi-tenancy in Hibernate 4.
All the Hibernate 4 dependencies have been declared in the pom.xml including the hsqldb
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
When the application was tested, the threw the following Exception
org.hibernate.exception.GenericJDBCException: This function is not supported
Caused by: java.sql.SQLException: This function is not supported
Then as usual got some references over the net and found that the problem is due to the hsqldb.
Changed the dependency to the following
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.8</version>
</dependency>
and it started working….
Sample Code
I have been waiting for the 50th post to be special and have been writing it when ever i get some time.
But had a discussion and wanted to post the following code.
My 50th special will come after this
import java.util.ArrayList;
public class FinaleTest {
private ArrayList aList = null;
public void addNew(final ArrayList aList) { // what is the significance of this?
aList.add(“Test”);//aList = new ArrayList(); // what will happen if this line is commented out!
}
public void initArrayList() {
aList = new ArrayList();
aList.add(“Jagan”);
aList.add(“Satya”);
}public ArrayList getaList() {
return aList;
}public void setaList(ArrayList aList) {
this.aList = aList;
}public int getCurrentListCount() {
return aList.size();
}public static void main(String[] args) {
FinaleTest fTest = new FinaleTest();fTest.initArrayList();
System.out.println(fTest.getCurrentListCount());
fTest.addNew(fTest.getaList());System.out.println(fTest.getCurrentListCount());
}
}
Java Collections – Performances – Part II
In the earlier version of this exercise, did it for few collections alone.
I haven’t tried anything on the linkedlist and other collections.
Now, have included whatever possible.
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;public class JavaCollections {
private static int count = 20;public static void main(String[] args) {
stackMan();
listMan();
linkedListMan();
linkedHashMapMan();
treeSetMan();
linkedBlockingDequeMan();
arrayBlockingQueueMan();
concurrentLinkedQueueMan();
delayQueueMan();
treeMapMan();
linkedBlockingQueueMan();
priorityBlockingQueueMan();
priorityQueueMan();
concurrentSkipListSetMan();
copyOnWriteArraySetMan();
hashSetMan();
arrayDequeMan();
}private static void stackMan() {
StopWatch stopWatch = new Log4JStopWatch();
Stack<String> stackContents = new Stack<String>();
for (int i = 0; i < count; i++) {
stackContents.add(i, Integer.toString(i));
}
stopWatch.stop(“stackMan”);
}private static void listMan() {
StopWatch stopWatch = new Log4JStopWatch();
ArrayList<String> listContents = new ArrayList<String>();
for (int i = 0; i < count; i++) {
listContents.add(i, Integer.toString(i));
}
stopWatch.stop(“ListMan”);
}private static void linkedListMan() {
StopWatch stopWatch = new Log4JStopWatch();
LinkedList<String> linkedListContents = new LinkedList<String>();
for (int i = 0; i < count; i++) {
linkedListContents.add(i, Integer.toString(i));
}
stopWatch.stop(“LinkedListMan”);
}private static void linkedHashMapMan() {
StopWatch stopWatch = new Log4JStopWatch();
LinkedHashMap<Integer, String> linkedhashMap = new LinkedHashMap<Integer, String>();
for (int i = 0; i < count; i++) {
linkedhashMap.put(Integer.valueOf(i), Integer.toString(i));
}
stopWatch.stop(“linkedHashMapMan”);
}private static void treeSetMan() {
StopWatch stopWatch = new Log4JStopWatch();
TreeSet<String> treeSet = new TreeSet<String>();
for (int i = 0; i < count; i++) {
treeSet.add(Integer.toString(i));
}
stopWatch.stop(“treeSetMan”);
}private static void arrayBlockingQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(
count);
for (int i = 0; i < count; i++) {
arrayBlockingQueue.add(Integer.toString(i));
}
stopWatch.stop(“arrayBlockingQueueMan”);
}private static void concurrentLinkedQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<String>();
for (int i = 0; i < count; i++) {
concurrentLinkedQueue.add(Integer.toString(i));
}
stopWatch.stop(“concurrentLinkedQueueMan”);
}private static void delayQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
DelayQueue<JaganString> delayQueue = new DelayQueue<JaganString>();
JaganString jString = new JaganString();
for (int i = 0; i < count; i++) {
jString.setText(Integer.toString(i));
delayQueue.add(jString);
}
stopWatch.stop(“delayQueueMan”);
}private static void linkedBlockingDequeMan() {
StopWatch stopWatch = new Log4JStopWatch();
LinkedBlockingDeque<String> linkedBlockingDeque = new LinkedBlockingDeque<String>();
for (int i = 0; i < count; i++) {
linkedBlockingDeque.add(Integer.toString(i));
}
stopWatch.stop(“linkedBlockingDeque”);
}private static void treeMapMan() {
StopWatch stopWatch = new Log4JStopWatch();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
for (int i = 0; i < count; i++) {
treeMap.put(Integer.valueOf(i), Integer.toString(i));
}
stopWatch.stop(“treeMapMan”);
}private static void linkedBlockingQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<String>();
for (int i = 0; i < count; i++) {
linkedBlockingQueue.add(Integer.toString(i));
}
stopWatch.stop(“linkedBlockingQueueMan”);
}private static void priorityBlockingQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
PriorityBlockingQueue<String> priorityBlockingQueue = new PriorityBlockingQueue<String>();
for (int i = 0; i < count; i++) {
priorityBlockingQueue.add(Integer.toString(i));
}
stopWatch.stop(“priorityBlockingQueueMan”);
}private static void priorityQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
PriorityQueue<String> priorityQueue = new PriorityQueue<String>();
for (int i = 0; i < count; i++) {
priorityQueue.add(Integer.toString(i));
}
stopWatch.stop(“priorityQueueMan”);
}private static void concurrentSkipListSetMan() {
StopWatch stopWatch = new Log4JStopWatch();
ConcurrentSkipListSet<String> concurrentSkipListSet = new ConcurrentSkipListSet<String>();
for (int i = 0; i < count; i++) {
concurrentSkipListSet.add(Integer.toString(i));
}
stopWatch.stop(“concurrentSkipListSetMan”);
}private static void copyOnWriteArraySetMan() {
StopWatch stopWatch = new Log4JStopWatch();
CopyOnWriteArraySet<String> copyOnWriteArraySet = new CopyOnWriteArraySet<String>();
for (int i = 0; i < count; i++) {
copyOnWriteArraySet.add(Integer.toString(i));
}
stopWatch.stop(“copyOnWriteArraySetMan”);
}private static void hashSetMan() {
StopWatch stopWatch = new Log4JStopWatch();
HashSet<String> hashSet = new HashSet<String>();
for (int i = 0; i < count; i++) {
hashSet.add(Integer.toString(i));
}
stopWatch.stop(“hashSetMan”);
}private static void arrayDequeMan() {
StopWatch stopWatch = new Log4JStopWatch();
ArrayDeque<String> arrayDeque = new ArrayDeque<String>();
for (int i = 0; i < count; i++) {
arrayDeque.add(Integer.toString(i));
}
stopWatch.stop(“arrayDequeMan”);
}}
We need to have an object which is implementing Delayed. Got the reference from http://sungur.wordpress.com/2011/02/10/java-delayqueue-test/ and implemented the following
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;public class JaganString implements Delayed {
private long endOfDelay;
private String text;
private long queueInsertTime;public long getQueueInsertTime() {
return queueInsertTime;
}public void setQueueInsertTime(long queueInsertTime) {
this.queueInsertTime = queueInsertTime;
}public String getText() {
return text;
}public void setText(String text) {
this.text = text;
}public JaganString() {
// TODO Auto-generated constructor stub
}public long getEndOfDelay() {
return endOfDelay;
}public void setEndOfDelay(long endOfDelay) {
this.endOfDelay = endOfDelay;
}@Override
public long getDelay(TimeUnit unit) {
// TODO Auto-generated method stub
long tmp = unit.convert(
(getQueueInsertTime() – System.currentTimeMillis())
+ endOfDelay, TimeUnit.MILLISECONDS);
return tmp;}
@Override
public int compareTo(Delayed o) {
// TODO Auto-generated method stub
int ret = 0;
JaganString ns = (JaganString) o;if (this.endOfDelay < ns.endOfDelay)
ret = -1;
else if (this.endOfDelay > ns.endOfDelay)
ret = 1;
else if (this.getQueueInsertTime() == ns.getQueueInsertTime())
ret = 0;return ret;
}
}
Ran the program with Sizes – 2000, 20000, 20000 and the results are as follows
| Tag | Avg(ms) – 2000 | Avg(ms) – 20000 | Avg(ms) – 200000 |
| arrayBlockingQueueMan | 1 | 4 | 56 |
| arrayDequeMan | 0 | 4 | 33 |
| concurrentLinkedQueueMan | 2 | 4 | 36 |
| concurrentSkipListSetMan | 7 | 25 | 398 |
| copyOnWriteArraySetMan | 45 | 4003 | 460606 |
| delayQueueMan | 4 | 15 | 67 |
| hashSetMan | 0 | 4 | 79 |
| linkedBlockingDeque | 4 | 9 | 242 |
| linkedBlockingQueueMan | 3 | 5 | 87 |
| linkedHashMapMan | 3 | 8 | 404 |
| LinkedListMan | 1 | 7 | 145 |
| ListMan | 1 | 5 | 2503 |
| priorityBlockingQueueMan | 2 | 11 | 142 |
| priorityQueueMan | 0 | 5 | 26 |
| stackMan | 2 | 7 | 63 |
| treeMapMan | 1 | 10 | 234 |
| treeSetMan | 5 | 21 | 318 |
Some graphical representations
What’s next?
1. Compare the Java implementations of Concurrent Classes with Scala Library?
2. Do we get any performance benefit in having multi-core processors?
3. Concurrent collections with Multi-threaded applications
JFilter….
I was going through one question during a conversation.
How to search the contents in a List or a Map and possibly filter the contents….
As usual, “googled” and found - http://code.google.com/p/jfilter/
Following the code snippet.
Employee.java
public class Employee {
private int empId;
private String fName;
private String sName;
private int age;
private String address1;
private String address2;
private String zipCode;public Employee(int empId, String fName, String sName, int age,
String address1, String address2, String zipCode) {
super();
this.empId = empId;
this.fName = fName;
this.sName = sName;
this.age = age;
this.address1 = address1;
this.address2 = address2;
this.zipCode = zipCode;
}public int getEmpId() {
return empId;
}public void setEmpId(int empId) {
this.empId = empId;
}public String getfName() {
return fName;
}public void setfName(String fName) {
this.fName = fName;
}public String getsName() {
return sName;
}public void setsName(String sName) {
this.sName = sName;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}public String getAddress1() {
return address1;
}public void setAddress1(String address1) {
this.address1 = address1;
}public String getAddress2() {
return address2;
}public void setAddress2(String address2) {
this.address2 = address2;
}public String getZipCode() {
return zipCode;
}public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}}
EmployeeFilters .java
import gk.jfilter.JFilter;
import java.util.ArrayList;
import java.util.List;public class EmployeeFilters {
public static void main(String[] args) {
List<Employee> empList = new ArrayList<Employee>();
int count = 50;
for (int i = 0; i < count; i++) {
Employee emp = new Employee(i, “Jagan” + i, “Asokan” + i, i + 100,
“Address Line 1 ” + i, “Address Line 2 ” + i, “30000″ + i);
empList.add(emp);
}
for (int i = 0; i < count; i++) {
Employee emp = new Employee(i, “Satya” + i, “Asokan” + i, i + 200,
“Address Line 1-1 ” + i, “Address Line 2-1 ” + i, “50000″ + i);
empList.add(emp);
}
for (int i = 0; i < count; i++) {
Employee emp = new Employee(i, “Lakshmi” + i, “Priya” + i, i + 300,
“Address Line 1-2 ” + i, “Address Line 2-2 ” + i, “10000″ + i);
empList.add(emp);
}
for (int i = 0; i < count; i++) {
Employee emp = new Employee(i, “Venu” + i, “Karthik” + i, i + 400,
“Address Line 1-3 ” + i, “Address Line 2-3 ” + i, “40000″ + i);
empList.add(emp);
}JFilter<Employee> empFilter = new JFilter<Employee>(empList, Employee.class);
/** Select employees where age code is less than or equals to 200. */
List<Integer> ageList = empFilter.filter(“{‘age’:{‘$le’:'?1′}}”, 200).<Integer> map(“age”).out(new ArrayList<Integer>());/** Select employees whose address1 is ADDRESS LINE 1. */
List<String> fAddressList = empFilter.filter(“{‘address1′:{‘toUpperCase’:'?1′}}”, “ADDRESS LINE 1″).<String> map(“address1″).out(new ArrayList<String>());
/** Select employees whose address1 starts with Address Line 1. */
List<String> fAddressList1 = empFilter.filter(“{‘address1′:{‘$sw’:'?1′}}”, “Address Line 1″).<String> map(“address1″).out(new ArrayList<String>());System.out.println(ageList.size());
System.out.println(fAddressList.size());
System.out.println(fAddressList1.size());
}}
pom.xml
<dependency>
<groupId>gk.jfilter</groupId>
<artifactId>jfilter</artifactId>
<version>0.5.0-SNAPSHOT</version>
</dependency>
Result
51
0
200
I got the code from http://jfilter.googlecode.com/svn/trunk/ and installed the dependency in my local maven Repo and hence was able to add that one in my pom.xml


