Scheduled Java agent leaving files open

I have a problem with a scheduled Java agent leaving files open, and am wondering if there’s any way to drop the file handles with code in another agent.

At the start of a run, the agent opens two file streams, like this:

inFS=new FileInputStream(filepath1);

outFS=new FileOutputStream(filepath2,false);

(Note that filepath1 and filepath2 were previously set to the full paths of files which already exist on the server.)

Then, if it terminates normally, the files are closed with these lines:

inFS.close();

outFS.close();

Trouble is, with the current server setup, the agent sometimes runs for too long and gets killed by the agent manager. This results in the files being left open, so no other processes can access them. The next time the agent runs, it dies with the exception “File is in use by another program” when attempting to open the first file.

Each time this happens, I can get someone with server admin access to fix the problem by restarting the amgr task, but this usually means waiting a while until an administrator is available.

Does anyone know of any code I can run (Java or LotusScript) to fix the problem without restarting amgr?

Subject: Scheduled Java agent leaving files open

Scott -

Try implementing a try/catch/finally block wherein an attempt is always (finally) made to close the streams regardless of outcome. So for example:

java.io.FileInputStream inFS = null;

java.io.FileOutputStream outFS = null;

try {

String strFilePathIn = “some valid filepath”;

String strFilePathOut = “some valid filepath”;

inFS = new java.io.FileInputStream(strFilePathIn);

outFS = new java.io.FileOutputStream(strFilePathOut, false);

// do stuff with the streams

} catch (Throwable th) { // a more specific exception may be better depending on program flow

th.printStackTrace();

// do whatever other exception handling you need to do

} finally {

System.out.println(“Closing streams . . .”);

closeStreamSilently(inFS);

closeStreamSilently(outFS);

}

/**

  • A static method for closing an instance of java.io.InputStream

  • @param pinStrm - java.io.InputStream, InputStream to be closed

*/

public static void closeStreamSilently(java.io.InputStream pinStrm) {

try {

if (pinStrm != null) {

pinStrm.close();

pinStrm = null;

}

} catch (java.io.IOException ioe) {

// ok to suppress these here

}

}

/**

  • A static method for closing an instance of java.io.OutputStream

  • @param poutStrm - java.io.OutputStream, OutputStream to be closed

*/

public static void closeStreamSilently(java.io.OutputStream poutStrm) {

try {

if (poutStrm != null) {

poutStrm.close();

poutStrm = null;

}

} catch (java.io.IOException ioe) {

// ok to suppress these here

}

}

And fyi, there is a utility class in the commons.io Jakarta project that does exactly this type of stuff.

dgg

Subject: RE: Scheduled Java agent leaving files open

Thanks, Dallas.That worked.