Jun 11

Running a java program from your shell script is commonplace for Batch operations.  And the wrapper shell script doesnt know about the success/failure of the wrapped Java program unless we return an exit code. 

In most cases, we have the Java source on hand. For others who use a different mechanism to achieve the functionality of the batch operation, I am not sure how far this will be useful to you.

The only possible mechanism (at least that’s what i know) is to give out your exit code using the System.exit (int) method call.  I would suggest you to keep off status codes 0-255 because that is already reserved in Unix platforms (Solaris inclusive).

For the DOS, you use the ERRORLEVEL to catch your exit status.

echo %ERRORLEVEL% should give you a number indicating the exit code of your last executed program.

For bash and ksh, it is $?  as in “echo $?”

For cshell, it is $status as in “echo $status” which returns.  I dont see $? working on cshell (at least on my Solaris box).

When you are calling a Java program from inside a Java program (using Runtime.exec()), the code will look something like this (more elegant, of course) :

Assume the following program is the one which is called

public class WaitAndTerminate {
public static void main(String[] args) {
System.out.println(”Program begin”);
System.exit(1000);
System.out.println(”Program exit”);
}
}

and this is your caller program

 
public class Caller {
public static void main(String[] args) {
BufferedReader inStream=null;
try {
System.out.println(”Caller start”);
Process process=Runtime.getRuntime().exec(”java WaitAndTerminate”);
inStream = new BufferedReader(new InputStreamReader( process.getInputStream() ));
//the waitFor waits for the called program to exit
process.waitFor();
System.out.println(process.exitValue());
} catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException ie){
ie.printStackTrace();
}
}
}

The getInputStream() method of the Process, as you can see, grabs the output of the called program so that it could be logged.

One other main utility of this mechanism is that we can get to know whether the Java program succeeds completely or not.  In which case, make the last line of your main program return a System.exit(1000) (which indicates that the program is terminated successfully). Any termination in the middle (assuming a system restart or assuming that the called program is a webservice client making a call to a service hosted on a application server which is restarting - My God) will not return a status code of 1000. Ideally, it would return 2 or something like that.

Lets code that into Java now with a bit of modification into our previously written code.


public class WaitAndTerminate {
public static void main(String[] args) {
System.out.println(”Program begin”);
Object object=new Object();
synchronized (object){
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.exit(1000);
System.out.println(”Program exit”);
}
}

The Caller Program is this :
 The destroy() method forcefully terminates the application, giving out a status code of 1.

public class Caller {
public static void main(String[] args) {
BufferedReader inStream=null;
try {
System.out.println(”Caller start”);
Process process=Runtime.getRuntime().exec(”java WaitAndTerminate”);
inStream = new BufferedReader(new InputStreamReader( process.getInputStream() ));
System.out.println(inStream.readLine());
//forceful termination of the called program
process.destroy();
process.waitFor();
System.out.println(process.exitValue());
} catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException ie){
ie.printStackTrace();
}
}
}

Output :
Caller start
Program begin
1

Tags: , ,

Jan 24

There are a few other things i wanted to share. I actually use the java.util.concurrent.ThreadPoolExecutor to copy files. For those using Java 1.4, here is your gateway to concurrency :  http://backport-jsr166.sourceforge.net/

Here goes my ThreadPool code. You surely could write better code than this.

LinkedBlockingQueue archiverTaskQueue = new LinkedBlockingQueue();
ThreadPoolExecutor archiverExecutor = new ThreadPoolExecutor(50, MAXTHREADS, 50000L, TimeUnit.MILLISECONDS,archiverTaskQueue);
File tempCurrentFile=new File(tempDestinationDir, archiveFileName);
archiver = new Archiver(tempCurrentFile, new File(actualDestinationDir, fileName));
archiverExecutor.execute(archiver);
 

Forgot to mention, I use NIO to copy files. I understand that there are people for and against NIO. My reason for using NIO is just it sounded exciting.

Source code : Archiver.java

Tags: , , , , , , , , ,

Sep 14

The number of MINUTES after which a web app times out is defined in the web.xml this way.

<web-app>
….
<session-config>
<session-timeout>60</session-timeout>
</session-config>
….
</web-app>

The WebLogic Server waits TimeoutSecs (in SECONDS) before timing out a session as shown below:
The default value, if not specified, is 3600 secs.

<weblogic-web-app>
….
<session-descriptor>
<session-param>
<param-name>TimeoutSecs</param-name>
<param-value>7200</param-value>
</session-param>
</session-descriptor>
….
</weblogic-web-app>

When timeout is set in both the deployment descriptors, the web.xml entry OVERRIDES the one defined in the weblogic.xml. This preference can be changed by a special value.

<session-config>
<session-timeout>-2</session-timeout>
</session-config>

This will let the app use the TimeoutSecs defined in the weblogic.xml for session time out.
There is yet another special value -1, in which case the session never times out. Of course, the weblogic entry is overriden in this case too.

Tags:

Sep 11

Old code but thought this would be useful

package com.ml;

import java.io.File;
import java.io.FilenameFilter;

public class FileFilterTest {

public static void main(String[] args) {

FilenameFilter filter=new FilenameFilter(){
public boolean accept(File dir, String fileName) {
return fileName.endsWith("java");
}
};

File f=new File("D:/Programs/Code");
String [] fileList=f.list(filter);
for (int i=0;i<fileList.length;i++){
System.out.println(fileList[i]);
}
}

}

Tags: , , ,

Aug 23

The same error comes when the regex is “+”

Here is the solution.

Enclose your * and + within square brackets.

Here is my code.

public static final String [] wildCards={”[*]“,”%”,”@”,”&”,”[+]“};

public static void main(String[] args) {
String s=”!@#$%^&*()”;
s=stripWild(s, wildCards);
System.out.println(s);
}

Tags: ,

Give your best to the world.