Thursday 15 August 2013

Java Concurrency


Completion Service:
This is quite a nice java API that i came across. This is useful if you submit lots of future object and would like to get a callback when they are complete, regardless of the order.  Alternative would have been to poll each future.



In one thread
completionservice.Submit()


in separate thread
completionService.take()



Ordered Executor
 We had a scenario where we would get several messages related to one  order and they need to be processed in order.



So solution was to have an array of single thread executors and we would submit each message for a given orderid to same executor. to accomplich that we would use method such as below

private ExecutorService getEcecutor(Object key){

  if(numExecutors>1 &&key!=null){
      int index=Math.abs(key.hashCode())%numExecutors;
        return executorArray[index]
}else{
      return executorArray[0]
    }
}

Threads
waiting for another thread If t is a Thread object whose thread is currently executing,
t.join();

 Interupts
use Thread.interrupted() to check if interrupt has been issues

When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.

Producer/ consumer pattern

Use blocking queue which provide blocking put and take methods

Work stealing pattern 

when  the worker exhausts works from its own queue it can steal work from another works queue


Blocking QUEUE interface

methods are summarized in the following table:


Throws exception Special value Blocks Times out
Insert add(e) offer(e) put(e) offer(e, time, unit)
Remove remove() poll() take() poll(time, unit)
Examine element() peek() not applicable not applicable

LOCKS
public class Reentrant{

  public synchronized outer(){
    inner();
  }

  public synchronized inner(){
    //do something
  }
}

If a thread already holds the lock on a monitor object, it has access to all blocks synchronized on the same monitor object. This is called reentrance


details at IBM Blog




  Volatile keyword in Java is used as an indicator to Java compiler and  Thread that do not cache value of this variable and always read it from main memory.

IBM blog http://www.ibm.com/developerworks/library/j-jtp03304/

Shell basics

Shell basics

to scape xml from logs

i had grep "something" filename | cut  | sed. this would work fine on te shell but when i put this into the script it just wont work. after googling i came accross:

http://stackoverflow.com/questions/4651437/how-to-set-a-bash-variable-equal-to-the-output-from-a-command


it turn out that i need  variable=` all the commands`. note no space in variable and  =

finally echo $variable >> out.txt

Tuesday 22 January 2013

Java down casting

Java down casting


public class TInq extends Inq {

    private String tsyInq;

    public String getTsyInq() {
        return tsyInq;
    }

    public void setTsyInq(String tsyInq) {
        this.tsyInq = tsyInq;
    }

}


public class Test {
    public static void main(String[] args) {

        Inq inq = new Inq();
        inq.setId("123");

        inq = new TInq();
        TInq tinq = (TInq) inq;
    }

}
This works because supercalss (Inq) is infact holding refrence to sub class. If i remove highligted line then one would get cast class exceptions.

Saturday 18 February 2012

JPA, Hibernate mapping

Small annoyance that get many people trip over is that if you have JPA entity mapping using property fields and have some mappings such as many to one at setter level you will get exception that it cannot find the entity (mapped in many to one, one to many)

so one need to stick to property mapping (@column ) and relationship maping on property level or at setter level

Thursday 19 January 2012

XA transactions weblogic spring 3.1 and oracle

Hi

I was recently making applications that uses XA transactions to read from JMS queue and write to Oracle Database.

The connection to the database was via weblogic datasource. Connection to the queue was via JNDI lookups for connection factory and JMS queue. I will not go into how to do it as there are several examples.

In Weblogic 10.3.0 (10gR3) i found a bug, basically when creating a datadource if you press enter after the jndi name. it creates a blank jndi!! I have by mistake created a JNDI myDatasource and then 3 enter keystrokes. it created 4 jndi entries for that datasource, and when the webapp tried to create a XA connection it would fail to get XA connection!

Weblogic should have detected this and given proper message. removing blank entries solved the problem.


Sunday 16 May 2010

windows 3.1 on Android

OMG! i would love to have that on my phone! windows 3.1 memories!


http://phandroid.com/2010/05/14/hello-1992-windows-3-1-comes-to-android/

Wednesday 10 March 2010

android growth


android growing really fast in US it has gone from 2.8 to 7.1 % as per comscore report and with mobile internet in india set to expand it would be equally or more popular in india