Returning auto generated id in spring – mybatis

Faced a problem with returning the autogenerated id for new rows being created for a postgres table through spring – mybatis.

@Insert("insert into user (name) values (#{name})")
public void insertActor(User user);

One solution ofcourse was to simply get nextval from the sequence, but I wanted something cleaner where I need not be worried about multithreading issues.

Solution one Tried: One good solution found for postgres was using “Returning” keyword with Insert statement

insert into user (name) values (#{name}) Returning id;

Somehow that did not work with mybatis.

Solution that worked:

@Options(useGeneratedKeys=true, keyProperty = "userId", keyColumn="id")
@Insert("insert into user (name) values (#{name})")
public void insertActor(User user);

@Options, with useGeneratedKeys flag worked just fine. keyProperty defined the property name in Java and keyColumn name defined name of column in table.

Branch predictor

Whenever in our code a conditional statement is executed, Branch Precitor tries to predict which way the flow will go and executes that even before conditional statement actually returns the output. This is done to stop wastage of time in waiting for conditional statement execution.


From wikipedia
- In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g. an if-then-else structure) will go before this is known for sure. The purpose of the branch predictor is to improve the flow in the instruction pipeline. Branch predictors play a critical role in achieving high effective performance in many modern pipelined microprocessor architectures such as x86.

Here is a good example to see what Branch predictor can do

package Permissions.Permissions;

import java.util.Arrays;
import java.util.Random;

public class test {

public static void main(String[] args)
{
// Generate data
int arraySize = 32768;
int data[] = new int[arraySize];

Random rnd = new Random(0);
for (int c = 0; c < arraySize; ++c)
data[c] = rnd.nextInt() % 256;

// !!! With this, the next loop runs faster
// Arrays.sort(data);

// Test
long start = System.nanoTime();
long sum = 0;

for (int i = 0; i < 100000; ++i)
{
// Primary loop
for (int c = 0; c < arraySize; ++c)
{
if (data[c] >= 128)
sum += data[c];
}
}

System.out.println((System.nanoTime() - start) / 1000000000.0);
System.out.println("sum = " + sum);
}

}

Try uncommenting the sort array part and see the difference.

Source- stackoverflow

Java- How to read an xlsx file

Here is a sample code to read xlsx file from java

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class UploadSchools {

public static void main(String s[])
{
UploadSchools u=new UploadSchools();
u.readExcel(“/home/kamalmeet/schools.xlsx”);
}

public void readExcel(String path)
{
try{

File file = new File(path);
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook workBook = new XSSFWorkbook (fis);
XSSFSheet sheet = workBook.getSheetAt(0);
Iterator rows = sheet.iterator();

while (rows.hasNext()) {
Row currentRow = rows.next();

Iterator cells = currentRow.cellIterator();
while (cells.hasNext()) {

Cell currentCell = cells.next();
System.out.print(currentCell.toString()+”\t”);
}
System.out.println(“”);
}
workBook.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Note that I am simply printing string value of the cells. You can check the cell type and read the value accordingly. For example, add following utility method

private String getCellValue(Cell cell) {
if (cell == null) {
return null;
}
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
return cell.getNumericCellValue() + “”;
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return cell.getBooleanCellValue() + “”;
}else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
return cell.getStringCellValue();
}else if(cell.getCellType() == Cell.CELL_TYPE_ERROR){
return cell.getErrorCellValue() + “”;
}
else {
return null;
}
}

References :

http://stackoverflow.com/questions/12526310/returning-decimal-instead-of-string-poi-jar

http://java67.blogspot.in/2014/09/how-to-read-write-xlsx-file-in-java-apache-poi-example.html

Aspect Oriented Programming- Implementation

In my last post I talked about AOP basics. Here I will try to take up aspectj implementation of AOP.

Continuing from last example where I had a simple employee class.

public class Employee {
int id;
String name;

public Employee(int id, String name)
{
this.id=id;
this.name=name;
}

public void work()
{
System.out.println(“Employee:”+ name+” is working”);
}
}

In last post I mentioned how Employee will need to maintain a bookKeeper to maitain start and end work. Whereas same can be achived throuogh AOP in a cleaner way, that is without disturbing Employee at all.

You will need to add aspectj libraries. If you are using Maven it is easy


org.aspectj
aspectjrt
RELEASE


org.aspectj
aspectjtools
RELEASE

If you are using spring, you will need to tell config file that I am using aspects. In my case I am using java based configuration so I will add following in config file.
@Configuration
@EnableAspectJAutoProxy

And then modify your BookKeeper code to tell it that it should execute its methods before and after any class’s method under test package executes.

@Component
@Aspect
public class BookKeeper {

java.util.Date date= new java.util.Date();
/**
* This method records when employee has started working
*/
@Before(“execution(* com.test.*.*(..))”)
public void employeeStartsWorking()
{
System.out.println(“Employee has started working”+new Timestamp(date.getTime()));
}

/**
* This method indicates when employee has stopped working
*/
@After(“execution(* com.test.*.*(..))”)
public void employeeEndsWorking()
{
System.out.println(“Employee has ended working”+new Timestamp(date.getTime()));
}

}

Aspect Oriented Programming

In object oriented programming, each object or class is made up of some properties and behavior. For example an Employee object might have properties like employee id, name etc and behavior like works.

so a simple Employee class would look like

public class Employee {
int id;
String name;

public Employee(int id, String name)
{
this.id=id;
this.name=name;
}

public void work()
{
System.out.println(“Employee:”+ name+” is working”);
}
}

But in real world a lot of things are happening around objects, for example lets say a bookkeeper needs to take a note of everytime an employee starts working and finishes working.

public class BookKeeper {

java.util.Date date= new java.util.Date();
/**
* This method records when employee has started working
*/
public void employeeStartsWorking()
{
System.out.println(“Employee has started working”+new Timestamp(date.getTime()));
}

/**
* This method indicates when employee has stopped working
*/
public void employeeEndsWorking()
{
System.out.println(“Employee has ended working”+new Timestamp(date.getTime()));
}

}

And my Employee class should be modified to

public class Employee {
int id;
String name;
BookKeeper bookKeeper=new BookKeeper();

public Employee(int id, String name)
{
this.id=id;
this.name=name;
}

public void work()
{
bookKeeper.employeeStartsWorking();
System.out.println(“Employee:”+ name+” is working”);
bookKeeper.employeeEndsWorking();
}
}

The challenge here is that my Employee should now be aware of BookKeeper and maintain its calls. Also it would need to actually handle error conditions (what if bookKeeper is null).

AOP or Aspect oriented programming comes to rescue here. Wikipedia states “In computing, aspect-oriented programming (AOP) is a patented[1] programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.”

Cross cutting concerns are the functionalities which are distributed accross the application but are not part of any core module. So for example, we have employee class, manager class, consultant class all implemeting work and book keeper keeoing record. So Book keeper needs to be available to all these objects, though it is not part of their core functionality. AOP recommends to move these functionality out of class and provides methods and tools for this.

Some frequently occuring cases for AOP are logging (ever class has need for logging, but it is not part of core functionality), transaction management, authentication and authrization etc.

I will talk about implementation of AOP part in next post.

Postgres error: Relation Does not exist

Faced an interesting error while accessing postgres table with a simple java code.

"select * from User"

A little googling revealed that if you are using multicase table name (U is caps), you will need to give the table name in quotes.

http://stackoverflow.com/questions/695289/cannot-simply-use-postgresql-table-name-relation-does-not-exist

A better fix would be to always use lower case characters when declaring tables in postgres.

Understanding Maven

Official definition of Maven states “Apache Maven is a software project management and comprehension tool.” For me, Maven is a build tool, that helps me manage dependencies as well. The advantage I get over Ant tool, is that it not only manages dependencies, but also downloads them automatically. Also I need not worry about transitive depeendies, that is if a jar I need for my project is inturn dependent on other jars, maven will take care of it for me.

Getting started: Using maven with Eclipse is simple. Eclipse comes with embedded maven so you need not worry about downloading or installing.

In New->Project, you can select Maven Project. It will ask you to select Archtype. An Archtype is predefine project templates. For example you can use Spring project, you can choose spring archtype. This will help you getting started by providing a sample template project, on which you can build further.

Once you create a project, you will find a pom.xml. POM stands for Project Object Model. It is fundamental Unit of Work in Maven.

A simple POM file might 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>TEST</groupId>
<artifactId>TEST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Permission</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Note the dependencies part. This is telling the maven which version of the required dependency is needed by application.

ExecutorService with Callable and Future

In last post I talked about ExecutorService basics.

Let us take it forward to understand usage of Future keyword. So far we have seen Executor service usage to start executing a thread in controlled way. What if we need to get a returned object from the thread being executed. Callable interface comes to help. Instead of implmenting Runnable, we can implement Callable to create thread. In this case we will override call() method method instead of run().

From Javadocs: The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

The result of callable class is retrived in Future instance. Instead of executor.execute, we will call submit method which forces ExecutorService to return the Future instance. execute can also be used with Runnable instance, but in that case it will return null on completion as run() method cannot return an object.

Here is an example

public class CallableTest {
int num=1;
public static void main(String s[])
{
ExecutorService executor = Executors.newFixedThreadPool(5);
//for(int i=0;i<10;i++)
{
Future ft=executor.submit(new RunTest());
Future ft1=executor.submit(new RunTest2());
System.out.println(“step 1″);
try {
CallableTest ct=ft.get();
System.out.println(“ct val:”+ct.num);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“step 2″);
try {
System.out.println(ft1.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“step 3″);

}
executor.shutdown();
}
}

class RunTest implements Callable
{
@Override
public CallableTest call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CallableTest ct=new CallableTest();
ct.num=10;
// TODO Auto-generated method stub
return ct;
}

}

class RunTest2 implements Runnable
{
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Further Reading

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html

ExecutorService

Here is refresher of multithreading in java.

Java (since 1.5) has come up with more controlled and cleaner way to handle threads, i.e. ExecutorService.

ExecutorService is a way in which we can create thread pools and execute threads in controlled manner, i.e. can define number of threads to be allowed at the same time.

ExecutorService ex = Executors.newSingleThreadExecutor();
ExecutorService ex = Executors.newFixedThreadPool(5);
ExecutorService ex = Executors.newScheduledThreadPool(10);

In first case we are creating a singlethreaded pool, whereas in second case we are defining a pool of fixed length. In third case we are creating a pool of threads which can perform activities at a defined time.

Read more
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
http://tutorials.jenkov.com/java-util-concurrent/executorservice.html

A simple code example

public class test {
public static void main(String s[])
{
ExecutorService executor = Executors.newFixedThreadPool(5);
for(int i=0;i<10;i++)
{
executor.execute(new RunTest());
executor.execute(new RunTest2());
}
executor.shutdown();
}
}

class RunTest implements Runnable
{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
System.out.println("run"+Thread.currentThread().getName());
}

}

class RunTest2 implements Runnable
{
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
System.out.println("run2"+Thread.currentThread().getName());
}

}

Play around with thread pool size to understand the usage.

Kill a process in linux

There can be times that a process becomes unresposive and you want to kill it forcefully.

In linux, we will need to first find out the process id

ps command- it gives a list of all processes running.

ps aux- gives all process details with, a = show processes for all users, u = display the process’s user/owner, x = also show processes not attached to a terminal

to fetch only required process- use grep, ps aux| grep ‘name’

Then kill it forcefully- kill -9 PID

Putting it all together.

Lets say I want to kill eclipse

kamalmeet@System:~$ ps aux | grep eclipse

1101 6070 1.3 1.8 46584 5500 ? Sl 12:34 1:38 /usr/bin/java -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx512m -jar /home/kamalmeet/eclipse//plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar

kamalmeet@System:~$ kill -9 6070