Tuesday, February 23, 2010

Command line Reader Sample

import java.io.*;
import java.lang.*;

public class DecimalToOctal {
public static void main(String[] args) throws IOException{
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the decimal number:");
String deci = buff.readLine();
int value = Integer.parseInt(deci);
String str = Integer.toString(value,8);
System.out.println("octal:=" + str);
}
}

Monday, February 22, 2010

Blocking Queue Sample

import java.util.*;
import java.util.concurrent.*;
import java.io.*;


public class BlockingQueue {
    private static List waitingCustomerList = null;
    private static final String filePath = "/home/serkans/phoneNumbersOnly.txt";
    private static ArrayBlockingQueue waitingCustomersQueue = null;


    public static void main (String[] args)  {


        loadNumbers();


        initializeBlockingQueue();


        Runnable callcenterGirl = new Runnable (){
            public void run () {
                while (true) {


                 String phoneNumber = null;
                 try {
                     phoneNumber = waitingCustomersQueue.poll (10000L, TimeUnit.MILLISECONDS);
                     if (null != phoneNumber){
                         System.out.println (" -- Callcenter girl is helping to : " + phoneNumber);
                     }else{
                         System.out.println ( " -- Callcenter girl is waiting  : " + phoneNumber);
                     }
                 } catch (InterruptedException ex) {
                     System.out.println ("  -- Callcenter girl is interrupted ex:");
                     ex.printStackTrace();
                 }
             }


         }
        };


        Thread t = new Thread (callcenterGirl);
        t.start();




    }


    private static ArrayBlockingQueue initializeBlockingQueue () {
        try {
            waitingCustomersQueue = new ArrayBlockingQueue(waitingCustomerList.size (), true, waitingCustomerList);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }
        return waitingCustomersQueue;
    }


    private static void loadNumbers () {
        Properties props = new Properties();
        try {
            props.load (new FileInputStream (new File (filePath)));
            System.out.println (" -- item count in file is :" + props.size());
            waitingCustomerList = new ArrayList (props.keySet());
        } catch (IOException ex) {
            System.out.println (" -- Unable to find file : "+filePath);
        }
    }
}

Wednesday, February 17, 2010

Are you sickened with QUARTZ database ??

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

public class WorkHourChecker {

private final static long MILLSECS_PER_HOUR = 1L * 60L * 60L * 1000L;

public static void main (String[] args) throws IOException, InterruptedException {
String workHours = "2-5,19-23,14-15";
checkTimeAndWork (workHours);
System.exit(0);
}

private static void checkTimeAndWork (String workHours) throws InterruptedException,
NumberFormatException {
Date now = new Date();
boolean isWorkhour = false;
StringTokenizer hoursTokenizer = new StringTokenizer(workHours,",");

while (hoursTokenizer.hasMoreTokens()) {
String duration = (String) hoursTokenizer.nextElement();
StringTokenizer durationTokenizer = new StringTokenizer(duration,"-");
int startHour = -1;
int endHour = -1;
while (durationTokenizer.hasMoreElements()) {
startHour = Integer.parseInt((String)durationTokenizer.nextElement()) ;
endHour = Integer.parseInt((String)durationTokenizer.nextElement()) ;
}
if (now.getHours() >= startHour && now.getHours() <= endHour){
isWorkhour = true;
break;
}else{
isWorkhour = false;
}

}

if (isWorkhour){
System.out.println ("-- In the work hours ..");
doSomeBusiness ();
System.out.println ("-- I am very tired i will get some rest!!");
Thread.currentThread().sleep (MILLSECS_PER_HOUR);
checkTimeAndWork (workHours);
}else{
System.out.println(" -- Not in the work hours yet, sleep 1 hour");
Thread.currentThread().sleep (MILLSECS_PER_HOUR);
checkTimeAndWork (workHours);
}
}

private static void doSomeBusiness (){
System.out.println (" -- I have started to work..");
}

}

Tuesday, February 16, 2010

Expose your Java classes as a MBean

link : Mbean sample

Getting SQL Statistics between given dates

ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

EXECUTE DBMS_LOGMNR.START_LOGMNR( STARTTIME => '16-FEB-2010 14:30:00', ENDTIME => '16-FEB-2010 15:00:00',options=> DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG +DBMS_LOGMNR.CONTINUOUS_MINE);

set pause on;

set linesize 2000;

SELECT TIMESTAMP,SQL_REDO,SESSION# FROM V$LOGMNR_CONTENTS WHERE seg_owner='CSVM' and TABLE_NAME = 'UMMESSAGE' and sql_redo like '%1647293%'