Thursday, 27 December 2012

SGA (System Global Area) - Oracle

SGA is shared location for oracle database instance. This memory will be shared or used by multiple processes running in that instance. This shared location is divided into multiple areas and each area will be used for different purpose.
Here are those areas,

1. Large pool:-
This holds information about UGA (User Global Area). This holds information related with user sessions.

2. dictionary cache:-
It holds information about data dictionary.

3. Java pool:-
It will be used for parsing java statements.

4. Shared pool:-
It holds information about commonly used SQL statements.

5. redo buffer:-
It holds info about committed transactions but not yes written to online log.

6. buffer cache:-
Holds data blocks from tables.

Method injection in spring framework - JAVA

Spring framework provides one of the good mechanism of method injection. Method injection means changing methods from class. Means on call of that particular method we can execute another method which is not in that class. We generally use this mechanism when we have some licensed code and we are not running with authority to change code but we are in need to change just one particular method.

Here I am giving short demo with my eclipse IDE. You can download this IDE from http://downloads.myeclipseide.com/downloads/products/eworkbench/indigo/installers/myeclipse-10.7-offline-installer-windows.exe here.


Now start the IDE and keep all default settings.
1. Create simple java project.


2. Now right click on project go to "my eclipse" option and in that select "add Spring capabilities".
Select 2.0 version of spring framework and finish it.


3. Now open your project and you will see applicationContext.xml file under "src" folder.


4. Open that file and past following code into that,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id = "emp" class = "testpkg.Employee">
        <replaced-method name = "empSal" replacer="employeeReplacer"/>
    </bean>
   
    <bean id= "employeeReplacer" class = "testpkg.EmployeeReplacer"/>
</beans>

5. Now create new package called "testpkg".

6. In that package create class called "Employee" and past following code,

package testpkg;
public class Employee {
    public String empSal() {
        return "bonus";
    }
}

7. Now create class "EmployeeReplacer" and past following code in it,

package testpkg;
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class EmployeeReplacer implements MethodReplacer{
    @Override
    public Object reimplement(Object arg0, Method arg1, Object[] arg2)
            throws Throwable {
        // TODO Auto-generated method stub
        return "extra bonus";
    }
}

8. Now create "Main" class in the same package and  past following code in it,

package testpkg;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Employee ms = (Employee) ctx.getBean("emp");
        String str = ms.empSal();
        System.out.println("This is return : " + str);
    }
}

Now if you are executing this code then in normal situation it must print "This is return : bonus".
But because of spring method injection now it will print "This is return : extra bonus". In this way we are executing some other method on behalf of class method.

Daemon Thread - Java

The major difference between normal thread and daemon thread is that JVM always waits for normal thread to finish, but JVM will never wait for daemon thread to terminate. Means ones normal threads in program are finishing execution the JVM will terminate without caring how many daemon threads are executing.
Here I am giving one simple example proving above mechanism,


class Main implements Runnable
{
    Thread normal, daemon;
    public static void main(String args[])
    {
        Main m = new Main();
        m.checkOut();
    }
   
    public void checkOut()
    {
        try
        {
            normal = new Thread(this);
           
            daemon = new Thread(this);
            daemon.setDaemon(true);
           
            normal.start();
            daemon.start();
        }
        catch(Exception e)
        {
            System.out.print("\nException : " + e);
        }
    }
   
    public void run()
    {
        try
        {
            synchronized (this) {
                System.out.print("\nThis is thread : " + 1);
                Thread.sleep(2000);
                System.out.print("\nThis is thread : " + 2);
                Thread.sleep(2000);
                System.out.print("\nThis is thread : " + 3);
                Thread.sleep(2000);
                System.out.print("\nThis is thread : " + 4);
            }
        }
        catch(Exception e)
        {
            System.out.print("\nException : " + e);
        }
    }
}


In output you can see that as soon as normal thread is finishing execution the JVM is exiting without waiting for daemon thread to complete.

In java by default thread is not daemon thread. We can use "setDaemon()" method to make our normal thread daemon. Here we have to remember that setDaemon() method must be called before call to start() method otherwise it will throw an exception IllegalThreadStateException.


Generally we can use this daemon thread for background processing purpose. Marking thread as daemon means it will be killed safely when JVM will exit. Practically we can use this daemon thread for background timers, keeping watch on something throughout our program execution etc.

Apart from this behavior daemon thread works similar to normal thread. We can use all normal thread methods with daemon thread.

Tuesday, 11 December 2012

Variable arguments - Java

This is one of the new feature in JDK5. varargs allows you to pass variable number of arguments to function. You can no argument, one arguments or any number of arguments to function using this feature. Remember one thing that vararg must be that last argument to function and we can use only one vararg for  one function. We can use varargs anywhere except last argument. You can even pass array to varargs.
Here I am giving one simple example,

class Test
{
    private void printThis(int... a)
    {
        for(int i = 0; i < a.length; i++)
            System.out.print("\nArgs : " + a[i]);
        System.out.print("\n\n=======================");
    }
   
    public static void main(String args[])
    {
        int[] ary = new int[10];
        for(int i = 0; i < 10; i++)
            ary[i] = i;
       
       
        Test t = new Test();
        t.printThis();
        t.printThis(1, 2, 3);
        t.printThis(1, 2, 3, 4, 5, 6, 7, 8);
        t.printThis(ary);
    }
}


We can use this when we don't know exact number of arguments.

Generics - Java

Generics are mainly used for creating type safe collections and for avoiding code repetition. It also helps us to make our code more readable. We can use generic methods as well as generic classes. This is one of the new feature in JDK5.
Here I am giving few examples of generics in java..

Avoiding code repetition:
public class Test
{                   
    public static <E extends Comparable<E>> E max( E x, E y, E z)
    {
        E max = x;
        if(max.compareTo(y) < 0)
        {
            max = y;
        }
        if(max.compareTo(z) < 0)
        {
            max = z;
        }
        return max;
    }
    public static void main( String args[])
    {
        System.out.print("\nMax : " + max(1, 2, 3));
        System.out.print("\nMax : " + max(1.0, 2.3, 3.6));
        System.out.print("\nMax : " + max("Abc", "Pqr", "Xyz"));
    }
}


Here you can see that we are using same method to compare integers, floats and strings. In this way generics avoid code repetition an this is making our code more redable.


Type safe collections:
 import java.util.ArrayList;
public class Test
{                   
    ArrayList<String> al;
    public Test()
    {
        al = new ArrayList<String>();
        al.add("Test 1");
        //al.add(1); This will create compile time error.
        al.add("Test2");
        System.out.print("Array list is : " + al);
    }
    public static void main(String args[])
    {
        new Test();      
    }
}


Here in the above code you can see that we are using ArrayList and we are making it type safe. Here our ArrayList will be able to store only strings. If you are trying to add integer then it will create compile time error.


Generic classes:
class Test<E>
{
    private <E extends Comparable<E>>void maxTest(E x, E y)
    {
        E max = x;
        if(max.compareTo(y) < 0)
            max = y;
        System.out.print("\nMax is : " + max);
    }
    public static void main(String args[])
    {
        Test<String> t1 = new Test<String>();
        t1.maxTest(1, 2);
      
        Test<String> t2 = new Test<String>();
        t2.maxTest(1.2, 2.7);
      
        Test<String> t3 = new Test<String>();
        t3.maxTest("A", "B");
    }
}


In this way we can write generic classes.

Here we can say that generics is mainly used for type safe collections, avoiding code repetition and making code more readable.

Wednesday, 5 December 2012

Accessing Machine Info : JAVA

We can access much machine information in java. Here I am giving some of those.
1. Getting OS current user:
System.getProperty("user.name");

2. Getting OS name:
System.getProperty("os.name");

3. Getting OS arch:
System.getProperty("os.arch");

4. Getting OS version:
System.getProperty("os.version");

5. Getting host name and IP address of machine:
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
localMachine.getHostName();
localMachine.getHostAddress();

6. Getting processor of machine:
System.getenv("PROCESSOR_IDENTIFIER");

7. Getting architecture of processor:
System.getenv("PROCESSOR_ARCHITECTURE");

8. Number of processors:
System.getenv("NUMBER_OF_PROCESSORS");

9. Getting screen dimentions:
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
dim.width  &  dim.height

10. Available drives:
File roots[] = File.listRoots();
for(int i = 0; i < roots.length; i++)
{
   System.out.print("Drive  :  " + roots[i].toString() + "  ::  Total space : " + roots[i].getTotalSpace() / 1024  / 1024 / 1024 + " GB  ::  Free space : " + roots[i].getFreeSpace() / 1024 / 1024 / 1024 + " GB");
 }//for i

I have tried these things on JDK6.0