How to write a scheduling job in java

September 8th, 2009No Comments  |  

1. Quartz

Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application – from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

http://www.opensymphony.com/quartz/

2. java batch process

If you are looking for writing java batch process you can find nice sample program at below link

http://www.java-tips.org/other-api-tips/jdbc/how-to-exceute-a-batch-process-from-preparedstat-2.html

3. java job triggering

If you need triggering point from java then you might want to take look at Timer class in java.
http://java.sun.com/j2se/1.4.2/docs/api/index.html

How to remove white-space from a String

September 2nd, 2009No Comments  |  

/*** replace multiple whitespaces between words with single blank */

String before=" hi how are you ";
String value = before.trim().replaceAll("[ ][ ]*", " ");

or

String value = before.replaceAll("\\b\\s{2,}\\b", " ");

//before : hi how are you

//after: hi how are you

/* remove leading whitespace */

String value= before.replaceAll("^\\s+", "");

//before : hi how are you

//after: hi how are you

/* remove trailing whitespace */

String value= before.replaceAll("\\s+$", "");

//before : hi how are you

//after: hi how are you

/** Trims all white spaces from an input string. */

public String trimSpace(String s){
String[] trimString = s.trim().split(" ");
String trimmedString = "";
for (int i = 0; i < trimString.length; i++){
trimmedString = trimmedString + trimString[i];
}
return trimmedString;
}

If you want to display substring of text and don’t want to truncate word in a substring, then you can use below method to fulfill your requirement.

public static String textSubString(String text, int start, int end){

String str="";
int limit=0;
limit=end;

if(text.length()!=0 && text!=""){

String substr=text.substring(end,end+1);

if(substr!=""){

while(!substr.equals("")){
substr=text.substring(limit,++limit);
substr=substr.trim();
}
}
str=text.substring(start,limit);
}
return str;
}