Friday, November 12, 2010

IODH

Zero Syncronization issues ! Simple ! Clean !

public class Something {
private Something() {
}

private static class LazyHolder {
private static final Something INSTANCE = new Something();
}

public static final Something getInstance() {
return LazyHolder.INSTANCE;
}
}

OR

static Singleton instance;

public static synchronized Singleton getInstance() {
if (instance == null)
instance == new Singleton();
return instance;
}

Why it is called LAZY ? : IODH utilizes lazy class initialization. The JVM won't execute a class's static initializer until you actually touch something in the class. This applies to static nested classes, too...

Read more on : wikipedia , Double-checked_locking

Wednesday, November 3, 2010

Transaction isolation levels and 3 phenomena..


dirty read
A transaction reads data written by a concurrent uncommitted transaction.
nonrepeatable read
A transaction re-reads data it has previously read and finds that data has been modified by another transaction (that committed since the initial read).
phantom read
A transaction re-executes a query returning a set of rows that satisfy a search condition and finds that the set of rows satisfying the condition has changed due to another recently-committed transaction.
Isolation Level Dirty Read Nonrepeatable Read Phantom Read
Read uncommitted Possible Possible Possible
Read committed Not possible Possible Possible
Repeatable read Not possible Not possible Possible
Serializable Not possible Not possible Not possible

see also : http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html Similar things to Java's ReentrantReadWriteLock

Thursday, October 28, 2010

Basic Drools example!

what is wrong with this ??

rule "Apply 10% discount if total purcahses is over 100"           
    no-loop true   
    dialect "java"
    when
        $c : Customer()
        $i : Double(doubleValue  > 100) from accumulate ( Purchase( customer == $c, $price : product.price ),
                                                                    sum( $price ) )
    then
          $c.setDiscount( 10 );
        insertLogical( new Discount($c, 10) );   
        System.out.println( "Customer " + $c.getName() + " now has a shopping total of " + $i );
end

Hi All,

We see a part of .drl file above :) I have found it in the Drools 5.1 examples.I tried to understand it's world. I can not define exactly but i think there is something dirty around here.

I write java. I want to write java. I know java (I guess:))...

But what is the no-loop & accumulate & sum & insertLogical & assigments in text & calling java methods from a text file ?

Why i have have learn some other literals ? If I am expected & free to write Java in there (meaning lovely .drl file) why & what the other things are ?

Come on guys. I am someone who really curious for new technologies. I am not resisting to learn those.


Only those make me scared . Calling my methods  from a text file ??? i was expecting to see only execution paths (in some other clean waylike when->what). Maybe I could implement some interfaces i could return some preDefined RETURN_TYPEs also. Even I could write simple Java Statements in an XML file. Those are all accepted by me.

But,,, Calling java methods from text context, doing assignments in there .Learning its nature .....I felt myself like developing PLSQL . I don't want implement a business logic inside a text file. I don't know why ...Just my comments.

PS: I could validate a xml file or Java file in my IDE (which is not eclipse). I don't want to be mad bec.of a typo or some extra comma while dealing with a weird text file in the future.


Sorry...

I will be searching for another fwk ....

****

A few days later....

A small update :

1) Download JSR94 jar :Java Rule Engine Specification
http://jcp.org/aboutJava/communityprocess/final/jsr094/index.html
2) Download JRuleEngine from :
http://sourceforge.net/projects/jruleengine/files/
3) Create a project with these 2 jars. Compile and run ..

See sample rule.xml :

I liked it very much...Sweet huh ?

Nevertheless I have a question mark for JRuleEngine too. As a commentator :D

In its web page there is a link: To do:

It says :
- Rules graphical editor: a JSP web application to define rules that will be saved on XML file.



****

JSP's and web applications is a good but i think it is not the right choice.
Application Management and Monitoring must be done via MBeans. JConsole a is good tool.

InputStream inStream = new FileInputStream( "/home/sunels/JRuleEngine1.3/examples/example1.xml" );
RuleExecutionSet res1 = ruleAdministrator.getLocalRuleExecutionSetProvider(null).createRuleExecutionSet( inStream, null );


Maybe rules can be served as a mbean while creating the RuleExecutionSet...

Friday, October 22, 2010

Are java constructors thread safe ?

For instance fields YES ...
For static fields NO...
Let's Proove it ...

public class TestObject {
    private static int dangerInt =0 ;

    public TestObject () {
            dangerInt++;
            System.out.println (" Current Value of i :" + dangerInt + "     # -- " + Thread.currentThread ().getName ());
    }
}

public class ConstructorTest {

    public static void main (String[] args) {
        final ConstructorTest test = new ConstructorTest();

        final Runnable r = new Runnable () {
            public void run () {
                while (true) {
                    try {
                        test.printSomething ();
                        Thread.sleep (500);
                    } catch (Exception ex) {
                        System.out.println (" -- Interrupted...");
                        ex.printStackTrace ();
                    }
                }
            }
        };

        for (int i = 0; i < 10; i++) {
            new Thread (r).start ();
        }

    }

    public void printSomething (){
        new TestObject ();
    }
}
Output :  Current Value of i :2     # -- Thread-1
 Current Value of i :2     # -- Thread-0
 Current Value of i :3     # -- Thread-2
 Current Value of i :4     # -- Thread-3
 Current Value of i :5     # -- Thread-4
 Current Value of i :6     # -- Thread-5
 Current Value of i :7     # -- Thread-6
 Current Value of i :8     # -- Thread-7
 Current Value of i :9     # -- Thread-8
 Current Value of i :10     # -- Thread-9
 Current Value of i :11     # -- Thread-1
 Current Value of i :12     # -- Thread-0
 Current Value of i :13     # -- Thread-2
 Current Value of i :14     # -- Thread-3
 Current Value of i :15     # -- Thread-4
 Current Value of i :16     # -- Thread-5
 Current Value of i :17     # -- Thread-6
 Current Value of i :18     # -- Thread-7
 Current Value of i :19     # -- Thread-8
 Current Value of i :20     # -- Thread-9
 Current Value of i :21     # -- Thread-1
 Current Value of i :22     # -- Thread-0
 Current Value of i :23     # -- Thread-2
 Current Value of i :24     # -- Thread-3
 Current Value of i :25     # -- Thread-4
 Current Value of i :26     # -- Thread-5
 Current Value of i :27     # -- Thread-6
 Current Value of i :28     # -- Thread-7
 Current Value of i :29     # -- Thread-8
 Current Value of i :30     # -- Thread-9
 Current Value of i :31     # -- Thread-1
 Current Value of i :32     # -- Thread-0
 Current Value of i :33     # -- Thread-2
 Current Value of i :34     # -- Thread-3
 Current Value of i :35     # -- Thread-4
 Current Value of i :36     # -- Thread-5
 Current Value of i :37     # -- Thread-6
 Current Value of i :38     # -- Thread-7
 Current Value of i :39     # -- Thread-8
 Current Value of i :40     # -- Thread-9
 Current Value of i :41     # -- Thread-1
 Current Value of i :42     # -- Thread-0
 Current Value of i :43     # -- Thread-2
 Current Value of i :44     # -- Thread-3
 Current Value of i :45     # -- Thread-4
 Current Value of i :46     # -- Thread-5
 Current Value of i :47     # -- Thread-6
 Current Value of i :48     # -- Thread-7
 Current Value of i :49     # -- Thread-8
 Current Value of i :50     # -- Thread-9
 Current Value of i :51     # -- Thread-1
 Current Value of i :53     # -- Thread-3
 Current Value of i :55     # -- Thread-5
 Current Value of i :57     # -- Thread-7
 Current Value of i :54     # -- Thread-6
 Current Value of i :58     # -- Thread-8
 Current Value of i :56     # -- Thread-4
 Current Value of i :52     # -- Thread-2
 Current Value of i :51     # -- Thread-0

Monday, October 11, 2010

Solving Leader Election Problem in a Clustered enviroment with JGroups

First of all this blog entry is written for jgroups 2.5.1 and  now jgroups.3-0-1-final is available and example is rewritten by me.

It is remommended to use new api. Because DistributedLock class is deprecated. Have a look at the new blog entry . 

Let me jump in the source code again without any amateur explaination about Leader Election Problem.

Scenario : There will be three competitor in our cluster. First one will be the master (~leader) because there is no other one currently.

Second and Third One(s) won't have a chance to get "Distributed Lock". After a while our master will be killed by us with a big pleasure :)

Then we will see new MASTER(~leader of our cluster).You can see the message on  snapshots below.

import org.jgroups.*;
import org.jgroups.blocks.DistributedLockManager;
import org.jgroups.blocks.VotingAdapter;
import org.jgroups.blocks.NotificationBus;
import org.jgroups.blocks.LockNotGrantedException;

import java.io.Serializable;

public class TheOne extends ReceiverAdapter {

    private JChannel channel;
    private volatile boolean becomeMaster;
    private DistributedLockManager lockManager;
    private VotingAdapter adapter;

    public static void main(String[] args) throws Exception {
        System.setProperty("jgroups.bind_addr", "10.34.34.137");//use your ip address
        TheOne master = new TheOne();
        master.start();
    }


    public void start() throws ChannelException {
        channel = new JChannel("udp.xml");
        adapter = new VotingAdapter(channel);
        adapter.addMembershipListener(this);
        lockManager = new DistributedLockManager(adapter, "DistributedLockManager");
        channel.connect("ClassPresidentCluster");
    }


    public void viewAccepted(final View new_view) {
        new Thread() {
            public void run() {
                System.out.println("new view " + new_view);
                getLock();
            }
        }.start();
    }


    private void getLock() {
        if (becomeMaster) {
            System.out.println("-- STILL I am the ONE !!");
            return;
        }
        try {
            System.out.println("looking for lock");
            System.out.println(lockManager);
            System.out.println(adapter);
            lockManager.lock("writer", channel.getAddress(), 0);
            becomeMaster = true;
            System.out.println(" -- I AM THE ONE !!!");
        } catch (ChannelException e) {
            e.printStackTrace();
        } catch (LockNotGrantedException e) {
            System.out.println("no lock for me :(");
        }
    }
}


1)First competitor comes to area a becomes a master very easily.

2)Second competitor comes to area but lock is owned by the first one.

3)Third competitor comes to area but lock is owned by the first one still.


4)After killing the master second one becomes a MASTER.



Example for java.util.concurrent.locks.ReentrantReadWriteLock usage


Let's try the latest sample with another beauty of the java.util.concurrent package :ReentrantReadWriteLock



import java.util.concurrent.locks.*;

public class ReentrantReadWriteLockExample {
    int i = 0;

    public static void main (String[] args) {
        final ReentrantReadWriteLockExample example = new ReentrantReadWriteLockExample();
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

        final Runnable r = new Runnable () {
            public void run () {
                while (true) {
                    try {
                        lock.writeLock().lock();
                        //Here is the critical section jobs are being done securely..
                        example.printSomething ();
                        Thread.sleep (1000);
                        lock.writeLock().unlock();
                    } catch (Exception ex) {
                        System.out.println (" -- Interrupted...");
                        ex.printStackTrace ();
                    }
                }
            }
        };

        new Thread (r).start ();
        new Thread (r).start ();
        new Thread (r).start ();
    }

    public void printSomething (){
        i++;
        System.out.println (" -- current value of the i :"+ i);
    }
}

Thursday, October 7, 2010

Example for java.util.concurrent.Semaphore usage

Let's try to increment a simple integer variable without any synchronization.
See what happens if we don't care about critical sections :)


Yes even we are not able to increment an integer without using any synchronization primitives.So we see the values of "i" 10-11-10 which are highlighted in the messages pane of my ide.

And...Here is the succesfull result in which we are using a Semaphore from java 1.5 (Tiger).

import java.util.concurrent.locks.*;
import java.util.concurrent.Semaphore;

public class SemaphoreExample {
    int i = 0;

    public static void main (String[] args) {
        final SemaphoreExample example = new SemaphoreExample();
        final Semaphore semaphore = new Semaphore (1);

        final Runnable r = new Runnable () {
            public void run () {
                while (true) {
                    try {
                        semaphore.acquire();
                        example.printSomething ();
                        Thread.sleep (1000);
                        semaphore.release();
                    } catch (Exception ex) {
                        System.out.println (" -- Interrupted...");
                        ex.printStackTrace ();
                    }
                }
            }
        };

        new Thread (r).start ();
        new Thread (r).start ();
        new Thread (r).start ();
    }

    public void printSomething (){
        i++;
        System.out.println (" -- current value of the i :"+ i);
    }
}
 

Monday, September 27, 2010

Take a trip into Java Monitor with your source code






                      line 1:  synchronized(obj){//Entry Set
                      line 2:       System.out.println ("I am the master of monitor");//Owner
                      line 3:       obj.wait();//Wait Set
                                 }
                      line 4:           //Out of the monitor

More than one thread can be blocked/found on line 1 , line 3 and line 4 at the same time, but there can only be one active thread at the same time at line 2 as a monitor owner.

You may want to check this sample also : http://javabender.blogspot.com/2010/09/example-for-understanding-javas-wait.html

Thursday, September 23, 2010

Example for Understanding Java's Wait / Notify Mechanism and Synchronization

 public class WaitNotifyExample {

    public static void main (String[] args) {
        final Object obj = new Object();

        Runnable r = new Runnable () {
            public void run () {
                while (true) {
                    try {
                        System.out.println ("-- this lines are being printed in another thread.." + Thread.currentThread ().getName ());
                        synchronized(obj){// The current thread must own this object's monitor see javadoc guys..Object.wait ();
                            obj.wait();// just wait for somebody else to awake you here.
                        }
                    } catch (Exception ex) {
                        System.out.println (" -- Interrupted...");
                        ex.printStackTrace ();
                    }
                }
            }
        };

        new Thread (r).start ();//we crated and started a lazy guy here
        new Thread (r).start ();//another lazy guy
        new Thread (r).start ();//this can be me :)

        while (true) {
            System.out.println (" -- this lines are bing printed in main thread.." + Thread.currentThread ().getName ());
            try {

                Thread.currentThread ().sleep (5000);//Our master will sleep for 5 seconds
                synchronized(obj){//same reason with above synchronized
                    obj.notifyAll();// Then it will say others wake up !
                }
                Thread.currentThread ().sleep (500);//If we don'T stop the main thread here it will be the owner of the monitor of obj object always.
                                                    //It is in a while loop.Others will be blocked on synchronized statement.

            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}



---| Lets look at to the system out , I have took a thread dump for u also  |-----
-- this lines are being printed in another thread..Thread-0
-- this lines are being printed in another thread..Thread-1
 -- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-2
-- this lines are being printed in another thread..Thread-0
-- this lines are being printed in another thread..Thread-1
 -- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-2
-- this lines are being printed in another thread..Thread-0
-- this lines are being printed in another thread..Thread-1
Full thread dump Java HotSpot(TM) Server VM (1.5.0_08-b03 mixed mode):

"Thread-2" prio=1 tid=0x09dcaa88 nid=0xfa6 in Object.wait() [0x6e8cb000..0x6e8cc080]
    at java.lang.Object.wait(Native Method)
    - waiting on <0xaa00e258> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:474)
    at sil.WaitNotifyExample$1.run(WaitNotifyExample.java:17)//
obj.wait()
    - locked <0xaa00e258> (a java.lang.Object)
    at java.lang.Thread.run(Thread.java:595)

"Thread-1" prio=1 tid=0x09dc9218 nid=0xfa5 in Object.wait() [0x6e94c000..0x6e94d100]
    at java.lang.Object.wait(Native Method)
    - waiting on <0xaa00e258> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:474)
    at sil.WaitNotifyExample$1.run(WaitNotifyExample.java:17)//
obj.wait()
    - locked <0xaa00e258> (a java.lang.Object)
    at java.lang.Thread.run(Thread.java:595)

"Thread-0" prio=1 tid=0x09dcb550 nid=0xfa4 in Object.wait() [0x6e9cd000..0x6e9cdd80]
    at java.lang.Object.wait(Native Method)
    - waiting on <0xaa00e258> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:474)
    at sil.WaitNotifyExample$1.run(WaitNotifyExample.java:17)//
obj.wait()
    - locked <0xaa00e258> (a java.lang.Object)
    at java.lang.Thread.run(Thread.java:595)

"main" prio=1 tid=0x09cecee0 nid=0xf94 waiting on condition [0xbfe77000..0xbfe776f8]
    at java.lang.Thread.sleep(Native Method)
    at sil.WaitNotifyExample.main(WaitNotifyExample.java:37)//
Thread.currentThread ().sleep (500)

Monday, September 6, 2010

How to debug our code on the remote machine : remote debugging with jbuilder and intellij idea..

We write our code in our pc then we put it to a remote server. Sometimes it fails :)
Then what do you do to understand what is going on ?
Thread dump does not give what you need.
You need to connect your java process and debug.


To debug a process which is running a remote server you need to start it with the following JVM_ARGs.


-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3999,suspend=n


Sample usage : java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3999,suspend=n com.mycompany.test.WriteYourOwnCode


By the way your process will be waiting your connection from your machine whenever you want. Port number [3999] will be reserved for this debugging connections. Try to reserve a port number which is not used any other process/system.Otherwise your process will not start.You will see an error in this case something like this.


[serkans@serkansunellnx voicemail] java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3999,suspend=n com.mycompany.test.WriteYourOwnCode
ERROR: transport error 202: bind failed: Address already in use ["transport.c",L41]
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510) ["debugInit.c",L500]
JDWP exit error JVMTI_ERROR_INTERNAL(113): No transports initializedFATAL ERROR in native method: JDWP No transports initialized, jvmtiError=JVMTI_ERROR_INTERNAL(113)


In the jvm_args you see an option : suspend=n . This means your remote code will not wait anybody to connect it before start. If you set this option to YES [suspend=y] process will wait before starting automatically. This suspend options must be set to YES [suspend=y] when you are unable to catch an error which is  happens in the beginnging of the process.


After starting our process with this options it is running and listenning for your connections...


Let me put a few picture to show how to connect to a remote process.
I'll show for both JBuilder and IntellijIdea.


1) Remote debugging with Jbuilder.


Firstly we start our code on a remote server with the remote debugging options.


[serkans@serkansunellnx voicemail] java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3999,suspend=n socketLogger.LogServer
Listening for transport dt_socket at address: 3999
 >> Server waiting for log messages...

It started and said that waiting on port 3999.It is not hanged.It is running, cool down :)



Then we turn to our ide.In the Jbuilder Right Click on the project and select project properties.
Click to the Run on the left pane of the project properties window.You will see RunTime Configurations. Click to New . A new window will appear called "New Runtime Configuration".Then click to the Remote in the left pane of this window.
1) Select "Enable Remote Debugging" by clicking the checkbox.
2) Select "Attach".
3) Write the IP of remote server on which your code is running as hostname.
4) Change the port number if you selected something different from 3999.


That is all.Click OK , OK  and close these windows.


It will looks like that :




After doing these right click on your Project .Select Debug..Ohh yes now you see the newly defined Runtime Configuration called "Your connection to remote process". Yes ...Click on this.




Now you are connected to your code.Go there and put your breakpoints. If your process hit your breakpoints you will notice it :-)


I got tired... I will expain it for Intellij Idea later.

Simple Log server with java SocketHandler and centralization of log records

A common requirement for all enterprise systems is the centralization of log records.

This systems simply use the SocketHandlers to store/display ongoing event logs.

SocketHandler is a subclasses of StreamHandler. Others classes are consoleHandler, FileHandler and SocketHandler.

In a distributed system nobody can perform a healty monitoring activity on the whole system without a centraliazed log system.

Let's have look at SocketHandler.

The following class will produce logs and sent to LogServer.

package socketLogger;

import java.io.*;
import java.util.logging.*;

public class LogTest {
    private static Logger logger = Logger.getAnonymousLogger ();
   
    public static void main (String argv[]) throws IOException {
        Handler handler = new SocketHandler ("localhost", 5000);
        //set handler log level
        handler.setLevel (Level.FINEST);
        //set logger log level
        logger.setLevel(Level.FINEST);
        //add socket handler to sent our logs to log server..
        logger.addHandler (handler);

        //lets make some noice
        logger.log (Level.SEVERE, "Hello, World");
        logger.log (Level.INFO, "Welcome Home");
        logger.log (Level.CONFIG, "Config ....");
        logger.log (Level.FINE, "Fine ....");
        logger.log (Level.FINEST, "Finest ....");
        logger.log (Level.WARNING, "Warning ....");
    }
}


And our log server which listens incoming logs and prints them to console.You can build your own log server with a nice gui and filtering/searching capabilities by enhancing this.

package socketLogger;

import javax.net.ssl.*;
import javax.net.*;
import java.io.*;
import java.net.*;

public class LogServer {
    private static final int PORT_NUM = 5000;
    private static ServerSocket serverSocket;
   
    public static void main (String args[]) {
        serverSocket = createServerSocket ();
        listenAndPrintLogMessages ();
    }

    private static void listenAndPrintLogMessages () {
        while (true) {
            Socket socket = null;
            try {
                System.out.println (" >> Server waiting for log messages...");
                socket = serverSocket.accept ();

                InputStream is = socket.getInputStream ();
                BufferedReader br = new BufferedReader (new InputStreamReader (is, "US-ASCII"));
                String line = null;
                while ((line = br.readLine ()) != null) {
                    System.out.println (line);
                }
            } catch (Exception e) {
                e.printStackTrace ();
            } finally {
                if (socket != null) {
                    try {
                        socket.close ();
                    } catch (IOException ignored) {
                    }
                }
            }
        }
    }

    private static ServerSocket createServerSocket () {
        ServerSocketFactory serverSocketFactory = ServerSocketFactory.getDefault ();
        try {
            serverSocket = serverSocketFactory.createServerSocket (PORT_NUM);
        } catch (IOException ioEx) {
            System.err.println ("Unable to create server");
            ioEx.printStackTrace();
            System.exit ( -1);
        }
        return serverSocket;
    }
}

see in action


And here is our logs which is captured from ServerSocket and printed to console in. By default the XMLFormatter class is used for formatting in the socket handler.So we have very nice log records now :)

 >> Server waiting for log messages...
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2010-09-06T23:01:02</date>
  <millis>1283803262382</millis>
  <sequence>0</sequence>
  <level>SEVERE</level>
  <class>socketLogger.LogTest</class>
  <method>main</method>
  <thread>10</thread>
  <message>Hello, World</message>
</record>
<record>
  <date>2010-09-06T23:01:03</date>
  <millis>1283803263161</millis>
  <sequence>1</sequence>
  <level>INFO</level>
  <class>socketLogger.LogTest</class>
  <method>main</method>
  <thread>10</thread>
  <message>Welcome Home</message>
</record>
<record>
  <date>2010-09-06T23:01:03</date>
  <millis>1283803263162</millis>
  <sequence>2</sequence>
  <level>CONFIG</level>
  <class>socketLogger.LogTest</class>
  <method>main</method>
  <thread>10</thread>
  <message>Config ....</message>
</record>
<record>
  <date>2010-09-06T23:01:03</date>
  <millis>1283803263162</millis>
  <sequence>3</sequence>
  <level>FINE</level>
  <class>socketLogger.LogTest</class>
  <method>main</method>
  <thread>10</thread>
  <message>Fine ....</message>
</record>
<record>
  <date>2010-09-06T23:01:03</date>
  <millis>1283803263172</millis>
  <sequence>4</sequence>
  <level>FINEST</level>
  <class>socketLogger.LogTest</class>
  <method>main</method>
  <thread>10</thread>
  <message>Finest ....</message>
</record>
<record>
  <date>2010-09-06T23:01:03</date>
  <millis>1283803263172</millis>
  <sequence>5</sequence>
  <level>WARNING</level>
  <class>socketLogger.LogTest</class>
  <method>main</method>
  <thread>10</thread>
  <message>Warning ....</message>
</record>
 >> Server waiting for log messages...

About JVM Options

  • Options that begin with -X are non-standard (not guaranteed to be supported on all VM implementations), and are subject to change without notice in subsequent releases of the JDK.
  • Options that are specified with -XX are not stable and are not recommended for casual use. These options are subject to change without notice. 
Don't expect to see abnormal tps increasement by using an option something like [-XX:+StringCache] this is why i am telling this here.

-XX:+StringCache : Enables caching of commonly allocated strings.

But it would be GREET :) if works huh ?

    Tuesday, August 24, 2010

    How to get tcp dump & snoop

    Snoop to file
    # snoop -xv -o snoopfilename port portNo


    Read from file with Snoop
    # snoop -xv -i snoopfilename port portNo

    TcpDump
    # tcpdump -i eth0 -s 0 -w snoopfilename port portNo

    Wednesday, August 18, 2010

    Vector vs ArrayList vs HashSet

    List syncList = Collections.synchronizedList(new ArrayList());

    Set syncSet = Collections.synchronizedSet(new HashSet());

    Vector is naturally synchronized.

    Performance Test Result
    #1 :ArrayList
    #2 :Vector
    #3 : HashSet

    Read more: Java Code Geeks: Java Best Practices – Vector vs ArrayList vs HashSet

    http://www.javacodegeeks.com/2010/08/java-best-practices-vector-arraylist.html#ixzz0wxSNUeuS

    Saturday, August 14, 2010

    Scalability hints

    Avoid lock on static variables or static methods:
    It locks your class. Even worse than locking your object!

    private static Object myVar = null;
    private foo{
        synchronized (myVar){// do not do that
           ....
       }
    }
    private static synchronized myMethod(){
         .....
    }

    Use lock free data structures
    Use java.util.concurrent.atomic package.It uses lock-free algorithms under the help with hardware synchronization primitives without using native code.

    Reduce lock granularity:

    Try to prefer  "block locks" instead of "method locks" if possible.synchronized methods locks the Object instance as you know.
    public class SchemaManager {
    private HashMap schema;
    private HashMap treeNodes;
    //Bad way!
    public boolean synchronized updateSchema(HashMap nodeTree) {
              String nodeName = (String)nodeTree.get("nodeName");
              String nodeAttributes = (List)nodeTree.get("attributes");
              if (nodeName == null)
                      return false;
              else
                      return schema.update(nodeName,nodeAttributes);
    }
    //Good Way
    public boolean updateSchema(HashMap nodeTree) {
    String nodeName = (String)nodeTree.get("nodeName");
    String nodeAttributes = (List)nodeTree.get("attributes");
    synchronized (schema) {
              if (nodeName == null)
                       return false;
              else
                       return schema.update(nodeName,nodeAttributes); }
    }
    public boolean synchronized updateTreeNodes() { ...... } }


    Make synchronized blocks as short as possible
    Move the thread safe code outside of the synchronized block.
     
    public boolean updateSchema(HashMap nodeTree) {
                   synchronized (schema) {
                              String nodeName = (String)nodeTree.get("nodeName");
                              String nodeAttributes = (List)nodeTree.get("attributes");
                              if (nodeName == null)
                                     return false;
                              else
                                     return schema.update(nodeName,nodeAttributes);
                    }
    }
     
    public boolean updateSchema(HashMap nodeTree) {
               String nodeName = (String)nodeTree.get("nodeName");
               String nodeAttributes = (List)nodeTree.get("attributes");
               synchronized (schema) {
                         if (nodeName == null)
                                return false;
                         else
                                return schema.update(nodeName,nodeAttributes);
              }
    }




    Try to use ThreadLocal variables if possible.

    Friday, August 13, 2010

    Java 6 allows classpath wildcards

    All we hate to complete our $CLASSPATH  !

    We always try to setup a CLASSPATH enviroment variable while dealing with all & everytime & anywhere.

    Fortunately Sun noticed our tears at the end :)

    Now we can specify lib/*, and all of the JAR files listed in that directory (not recursively), in the classpath.

    We collect all dependencies under a directory and it is enough to say everthing is out there...

    Sunday, August 1, 2010

    Reliable Multicasting with JGroups

    Append the jgroups jar to your classpath.

    Then execute the following code twice in different consoles like this.These are our chat clients.

    java -Djava.net.preferIPv4Stack=true SimpleChat


    import java.io.*;
    import java.net.InetAddress;
    import org.jgroups.*;

    public class SimpleChat implements Receiver {
        JChannel channel;

        public static void main (String[] args) throws Exception {
            new SimpleChat ().start ();
        }

        private void start () throws Exception {
            channel = new JChannel ();
            channel.connect ("ChatCluster");
            channel.setReceiver (this);
            eventLoop ();
            channel.close ();
        }

        private void eventLoop () {
            BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
            while ( true ) {
                try {
                    System.out.print ("> ");
                    System.out.flush ();
                    String line = in.readLine ().toLowerCase ();
                    if (line.startsWith ("quit") || line.startsWith ("exit")) {
                        break;
                    }
                    line = "[" + this + "] " + line;
                    Message msg = new Message (null, null, line);
                    channel.send (msg);
                } catch (Exception e) {
                    e.printStackTrace ();
                }
            }
        }

        public void receive (Message message) {
            System.out.println ("message = " + new String (message.getBuffer ()));
        }

        public byte[] getState () {
            return new byte[0]; 
        }

        public void setState (byte[] bytes) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public void viewAccepted (View view) {
            System.out.println ("view = " + view.toString ());
        }

        public void suspect (Address address) {
            System.out.println ("address = " + address);
        }

        public void block () {

        }
    }

    There are more fancy examples in there. Like org.jgroups.demos.Draw !

    Sunday, July 18, 2010

    Three ways to make an object thread-safe


    There are basically three approaches you can take to make an object such as RGBThread thread-safe:

    1. Synchronize critical sections
    2. Make it immutable
    3. Use a thread-safe wrapper 
    design techniques and thread safety

      Why worry about thread safety?

      Given the structure of the JVM, local variables, method parameters, and return values are inherently "thread-safe." But instance variables and class variables will only be thread-safe if you design your class appropriately

      -- Bill Venners

      Preventing Memery Locks on the Multi CPU machine

      Problem
         Multithreaded apps create new objects at the same time
         New objects are always created in the EDEN space
         During object creation, memory is locked
         On a multi CPU machine (threads run concurrently) there can be contention

      Solution
          Allow each thread to have a private piece of the EDEN
          space
      Thread Local Allocation Buffer
          -XX:+UseTLAB
          -XX:TLABSize=
          -XX:+ResizeTLAB
          (On by default on multi CPU machines and newer JDK)
      Analyse TLAB usage
          -XX:+PrintTLAB
      JDK 1.5 and higher (GC ergonomics)
          Dynamic sizing algorithm, tuned to each thread


      INSIDE THE JAVA VIRTUAL MACHINE / Filip Hanik

      Sun recommended JVM Settings

      GC Settings
         -XX:+UseConcMarkSweepGC
         -XX:+CMSIncrementalMode
         -XX:+CMSIncrementalPacing
         -XX:CMSIncrementalDutyCycleMin=0
         -XX:+CMSIncrementalDutyCycle=10
         -XX:+UseParNewGC
         -XX:+CMSPermGenSweepingEnabled
      To analyze what is going on
         -XX:+PrintGCDetails
         -XX:+PrintGCTimeStamps
         -XX:-TraceClassUnloading
                                         
      Minor Notes
      -XX:+UseParallelGC <> -XX:+UseParNewGC
      -XX:ParallelGCThreads=
          Use with ParallelGC setting
      If you have 4 cpus and 1 JVM
          Set value to 4
      If you have 4 cpus and 2 JVM
          Set value to 2
      If you have 4 cpus and 6 JVM
          Set value to 2


      INSIDE THE JAVA VIRTUAL MACHINE / Filip Hanik

      Thread Safety Rules

      Safety Rules

      Sometimes, a set of data is interdependent. For example, we might have two fields corresponding to a street address and a zip code. Changing an address might involve changing both of these fields. If the zip code is changed without a corresponding change to the street address, the data may be inconsistent or incoherent. Such a set of operations, which must be done as a unit -- i.e., either all of the operations are executed or none are -- in order to ensure consistency of the data, is called a transaction. The property of "doing all or none" is called atomicity. A system in which all transactions are atomic is transaction-safe.
      The following rule suffices to ensure that your system is transaction-safe:
        All (potentially changeable) shared data is accessed only through the synchronized methods of a single object; no interdependent piece can be accessed independently.
      Note that this means that shared data cannot be returned by these methods for access by other methods. If shared data is to be returned, a (non-shared) copy must be made. Further, if interdependent values are to be returned (i.e., a portion of the shared data is to be used by other methods), all of the relevant values must be returned in a single transaction.
      For example, the address and zip code of the previous example should not be returned by two separate method calls if they are to be assumed consistent.
      public class AddressData {
      private String streetAddress;
      private String zipCode;
      public AddressData( String streetAddress, String zipCode) {
          this.setAddress( streetAddress, zipCode );
          ....
      }
      public synchronized void setAddress( String streetAddress,
                                           String zipCode) {
          // validity checks
          ....
          // set fields
          ....
      }
      public synchronized String getStreetAddress() { // problematic!
          return this.streetAddress;
      }
      public synchronized String getZipCode() { // problematic!
          return this.zipCode;
      }
      }
      If this class definition were used, e.g. for
      printMailingLabel( address.getStreetAddress(),
                         address.getZipCode() );
      it would in principle be possible to get an inconsistent address. For example, between the calls to address.getStreetAddress() and address.getZipCode(), it is possible that a call to address.setAddress could occur. In this case, getStreetAddress would return the old street address, while getZipCode() would return the new zip code.
      Instead, getStreetAddress() and getZipCode() should be replaced by a single synchronized method which returns a copy of the fields of the AddressData object:
      public synchronized SimpleAddressData getAddress() {
          return new SimpleAddressData( this.streetAddress,
                                        this.zipCode );
      }
      The SimpleAddressData class can contain just public streetAddress and zipCode fields, without accessors. It is being used solely to return the two objects at the same time.

      Introduction to Interactive Programming by Lynn Andrea Stein

      Reentrant Synchronization

      Recall that a thread cannot acquire a lock owned by another thread. But a thread can acquire a lock that it already owns. Allowing a thread to acquire the same lock more than once enables reentrant synchronization. This describes a situation where synchronized code, directly or indirectly, invokes a method that also contains synchronized code, and both sets of code use the same lock. Without reentrant synchronization, synchronized code would have to take many additional precautions to avoid having a thread cause itself to block.

      Intrinsic Locks and Synchronization

      Cooperation

      Java's monitor supports two kinds of thread synchronization: mutual exclusion and cooperation . Mutual exclusion, which is supported in the Java virtual machine via object locks, enables multiple threads to independently work on shared data without interfering with each other. Cooperation, which is supported in the Java virtual machine via the wait and notify methods of class Object, enables threads to work together towards a common goal. 

      Cooperation is important when one thread needs some data to be in a particular state and another thread is responsible for getting the data into that state. For example, one thread, a "read thread," may be reading data from a buffer that another thread, a "write thread," is filling. The read thread needs the buffer to be in a "not empty" state before it can read any data out of the buffer. If the read thread discovers that the buffer is empty, it must wait. The write thread is responsible for filling the buffer with data. Once the write thread has done some more writing, the read thread can do some more reading. 



      Thread Synchronization by Bill Venner





      package monitor;
      import java.util.Vector;



      class Producer extends Thread {
          static final int MAXQUEUE = 5;
          private Vector messages = new Vector(); 
          public void run() {

              try {
               while ( true ) {
                      putMessage();
                      sleep( 1000 );
                  }
              }
              catch( InterruptedException e ) { }
          }

          private synchronized void putMessage() throws InterruptedException {
              while ( messages.size() == MAXQUEUE )
                  wait();
              messages.addElement( new java.util.Date().toString() );
              notify();
          }

          // Called by Consumer
          public synchronized String getMessage() throws InterruptedException {
             notify();
             while ( messages.size() == 0 )
                  wait();
              String message = (String)messages.firstElement();
              messages.removeElement( message );
              return message;
          }

      }



      class Consumer extends Thread {
          Producer producer;

          Consumer(Producer p) {
              producer = p;
          }

          public void run() {
              try {
                  while ( true ) {
                      String message = producer.getMessage();
                      System.out.println("Got message: " + message);
                      sleep( 2000 );
                  }
              }
             catch( InterruptedException e ) { }
          } 
       
          public static void main(String args[]) {
              Producer producer = new Producer();
              producer.start();
              new Consumer( producer ).start();
          }

      }
       


      Wednesday, July 14, 2010

      How to detect JVM Shutdown

      Runtime.getRuntime().addShutdownHook(new ShutdownHook());

      boolean jvmShuttingDown = false;


      private class ShutdownHook extends Thread {
              public void run() {
                     jvmShuttingDown = true;
              }
      }

      Friday, July 9, 2010

      How to lock a table in Mysql

      in a session
      use following
      --> lock tables mySchema.myTable write;
      In another session try to get
      -->select count(*) from mySchema.myTable;

      It will be locked till u say

      --> unlock tables

      How to get OpenJpa Sql Traces

      Put the following item in your persistence.xml

      <properties>
                  <property name="openjpa.Log" value="SQL=TRACE"/>
      </properties>

      Monday, July 5, 2010

      Lets do some XStream test

      Aim: Keep a finite state machine configuration in a xml file.

      A sample synthetic scenario for this ;
          Get messageBoxes of the users. Put the messageBoxes into a blockingQueue , let some threads process the mailboxes and get new and old messeges from the boxes and seperate them to different queues .
          Then process new messages (exm:delete if it is keeped longer than expected) and put some data for notification sms to the owner of expired message.Some threads will process this queue and will send smses and they will put some data to the edr queue.
          If the message is old (read message) threads on the oldMessageQueue put data to the edr queue directly without sending sms.

      Maybe we want to use this states and conditions in a state manager base implementation/code generator which is not written yet ;)

      public class StateList {
          public StateList () {
          }

          public List getFsm () {
              return fsm;
          }

          public void setFsm (List fsm) {
              this.fsm = fsm;
          }

          private Listfsm = new ArrayList();
      }


      public class State {
          public State () {
          }

          private String queueName;
          private int threadCount;
          private int queueSize;
          private String executorClassName;
          private List transitions = new java.util.ArrayList();

          public String getExecutorClassName () {
              return executorClassName;
          }

          public String getQueueName () {
              return queueName;
          }

          public int getThreadCount () {
              return threadCount;
          }

          public int getQueueSize () {
              return queueSize;
          }

          public List getTransitions () {
              return transitions;
          }

          public void setExecutorClassName (String executorClassName) {
              this.executorClassName = executorClassName;
          }

          public void setQueueName (String queueName) {
              this.queueName = queueName;
          }

          public void setThreadCount (int threadCount) {
              this.threadCount = threadCount;
          }

          public void setQueueSize (int queueSize) {
              this.queueSize = queueSize;
          }

          public void setTransitions (List transitions) {
              this.transitions = transitions;
          }
      }


      public class Transition {
          public Transition () {
          }

          public String getCondition () {
              return condition;
          }

          public String getName () {
              return name;
          }

          public void setCondition (String condition) {
              this.condition = condition;
          }

          public void setName (String name) {
              this.name = name;
          }

          private String name ;
          private String condition;
      }


      package XstreamTester;

      import java.util.*;
      import com.thoughtworks.xstream.*;
      import java.io.*;


      public class XStreamTester {
          public XStreamTester () {
       
          /* messageboxGetter->//newMsgProcessor ->SmsSender->EdrWrite
                                             ->//oldMsgProcessor ->EdrWrite
           */

          }

          public static void main (String[] args) {
              XStreamTester xstreamtester = new XStreamTester ();

              XStream stateConfiguration = new XStream ();
              stateConfiguration.alias ("state", State.class);
              stateConfiguration.alias ("transition", Transition.class);
              stateConfiguration.alias ("states", StateList.class);

              StateList stateConfig = new StateList();
              List stateList = new ArrayList();
              // messagebox state
              State mboxState = new State ();
              mboxState.setExecutorClassName ("MboxGetter");

              List mboxTransitions = new ArrayList();
              Transition newMessageTransition = new Transition ();
              newMessageTransition.setName ("newMessageQueue");
              newMessageTransition.setCondition ("new");
              mboxTransitions.add (newMessageTransition);
              Transition oldMessageTransition = new Transition ();
              oldMessageTransition.setName ("oldMessageQueue");
              oldMessageTransition.setCondition ("old");
              mboxTransitions.add (oldMessageTransition);
              mboxState.setTransitions (mboxTransitions);

              mboxState.setQueueName ("messageBoxQueue");
              mboxState.setThreadCount (10);
              mboxState.setQueueSize (1000);
              stateList.add (mboxState);


              // new message state
              State newMsgState = new State ();
              newMsgState.setExecutorClassName ("newMessageProcessor");

              List newMsgTransitionList = new ArrayList();
              Transition smsTransition = new Transition ();
              smsTransition.setName ("smsQueue");
              smsTransition.setCondition ("deleted");
              newMsgTransitionList.add (smsTransition);
              newMsgState.setTransitions(newMsgTransitionList);

              newMsgState.setQueueName ("newMessageQueue");
              newMsgState.setThreadCount (10);
              newMsgState.setQueueSize (1000);
              stateList.add (newMsgState);

              // old message state
              State oldMsgState = new State ();
              oldMsgState.setExecutorClassName ("oldMessageProcessor");

              List oldMsgTransitionList = new ArrayList();
              Transition edrTransition = new Transition ();
              edrTransition.setName ("edrQueue");
              edrTransition.setCondition ("deleted");
              oldMsgTransitionList.add (edrTransition);
              oldMsgState.setTransitions(oldMsgTransitionList);

              oldMsgState.setQueueName ("oldMessageQueue");
              oldMsgState.setThreadCount (10);
              oldMsgState.setQueueSize (1000);
              stateList.add (oldMsgState);


              //sms state
              State smsState = new State ();
              smsState.setExecutorClassName ("smsQueueProcessor");

              List smsTransitionList = new ArrayList();
              Transition sms2EdrTransition = new Transition ();
              sms2EdrTransition.setName ("edr");
              sms2EdrTransition.setCondition ("smsSent");
              smsTransitionList.add (sms2EdrTransition);

              smsState.setTransitions(smsTransitionList);
              smsState.setQueueName ("smsQueue");
              smsState.setThreadCount (10);
              smsState.setQueueSize (1000);
              stateList.add (smsState);


              //edr state
              State edrState = new State ();
              smsState.setExecutorClassName ("edrQueueProcessor");

              edrState.setQueueName ("edrQueue");
              edrState.setThreadCount (10);
              edrState.setQueueSize (1000);
              stateList.add (edrState);


              stateConfig.setFsm(stateList);
              String xml = stateConfiguration.toXML (stateConfig);
              try {
                  BufferedWriter out = new BufferedWriter (new FileWriter (
                      "/home/serkans/jbproject/UM_Test/xml/stateConfiguration.xml"));
                  out.write (xml);
                  out.close ();
              } catch (IOException e) {
                  e.printStackTrace ();
              }
              System.exit (0);
          }
      }


      Generated xml will be something like this.
      <states>
        <fsm>
          <state>
            <queueName>messageBoxQueue</queueName>
            <threadCount>10</threadCount>
            <queueSize>1000</queueSize>
            <executorClassName>MboxGetter</executorClassName>
            <transitions>
              <transition>
                <name>newMessage</name>
                <condition>new</condition>
              </transition>
              <transition>
                <name>oldMessage</name>
                <condition>old</condition>
              </transition>
            </transitions>
          </state>
          <state>
            <queueName>newMessageQueue</queueName>
            <threadCount>10</threadCount>
            <queueSize>1000</queueSize>
            <executorClassName>newMessageProcessor</executorClassName>
            <transitions>
              <transition>
                <name>sms</name>
                <condition>deleted</condition>
              </transition>
            </transitions>
          </state>
          <state>
            <queueName>oldMessageQueue</queueName>
            <threadCount>10</threadCount>
            <queueSize>1000</queueSize>
            <executorClassName>oldMessageProcessor</executorClassName>
            <transitions>
              <transition>
                <name>edr</name>
                <condition>deleted</condition>
              </transition>
            </transitions>
          </state>
          <state>
            <queueName>smsQueue</queueName>
            <threadCount>10</threadCount>
            <queueSize>1000</queueSize>
            <executorClassName>edrQueueProcessor</executorClassName>
            <transitions>
              <transition>
                <name>edr</name>
                <condition>smsSent</condition>
              </transition>
            </transitions>
          </state>
          <state>
            <queueName>edrQueue</queueName>
            <threadCount>10</threadCount>
            <queueSize>1000</queueSize>
            <transitions/>
          </state>
        </fsm>
      </states>

      Wednesday, June 30, 2010

      Are u sure about Oracle ROWNUM ?

      http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html

      A simple implementation with JPA  on oracle dbms

          /*
           I had to make some improvements in query with oracle bec.of performance of the default jpa implementation.
           */
          public User[] getUsersInPage (int startIndex, int stopIndex) throws Exception {

              EntityManager em = createEntityManager ();
              List ret = new ArrayList();
              try {
                  if (isOracle()){
                      Query selectQuery = em.createNativeQuery ("SELECT * FROM (select a.*,rownum rnum from (select * from provisioning_subsys.VIP_USERS) a where ROWNUM <= " +
                          stopIndex + ") where rnum >=" + startIndex + "", User.class);
                      ret = (List) selectQuery.getResultList ();
                  }else{
                      String qry = "select u from User u";
                      Query selectQuery = em.createQuery (qry);
                      selectQuery.setMaxResults (stopIndex - startIndex);
                      selectQuery.setFirstResult (startIndex);
                      ret = (List) selectQuery.getResultList ();
                  }
              } finally {
                  em.close ();
              }

              return getUserArrayFromList (ret);
          }

          private boolean isOracle () {
              if (emf instanceof EntityManagerFactoryImpl) {
                  Properties jpaProperties = ((EntityManagerFactoryImpl) emf).getProperties ();
                  String dbms = (String) jpaProperties.get ("Platform");
                  if (dbms.indexOf ("Oracle") != -1) {
                      return true;
                  } else return false;
              }
              return false;
          }