Hot questions for Using GlassFish in multithreading

Top Java Programmings / GlassFish / multithreading

Question:

I'm indexing pdf files using Apache lucene with threads. Some threads take time more than 15 minutes. After 15 minutes of thread execution it will throw Thread interrupted Exception.Is there a way to increase the time limit to avoid this issue.

I got this exception when there is a single thread running and it indexed nearly 76% of its pdf files. application server is Glassfish


    List<Thread> threads = new ArrayList<Thread>();
    Thread worker;
    for (int a = 1;a <= count; a++) {

        IndexManualRunnable indexManualRunnable =
                new IndexManualRunnable(indexfileLocation, collections, manual, processId);
        worker = new Thread(indexManualRunnable);

        worker.setName(manual.getName());
        worker.setPriority(Thread.MAX_PRIORITY);
        worker.start();

        threads.add(worker);
    }

        for (Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException interruptedException) {

                saveReport("", "", "Interrupted Exception", 1, processId, Category.INDEXING, thread.getName());
                interruptedException.printStackTrace();
            }
        }

Answer:

UPDATE:

I see that you are using Glassfish and are saying this interrupt is occurring every time at 15 minutes. It appears Glassfish is set to timeout at around 900 seconds which is 15 minutes by default - this throws an InterruptException.

Since your application viably needs to process for longer than 15 minutes, update the following server config to a time limit you see fit.

http.request-timeout-seconds

Here is an example asadmin command to update the property but I have not tested it:

 # asadmin set server-config.network-config.protocols.protocol.<listener-name>.http.request-timeout-seconds=-1

- NOTE: <listener-name> is the name of the listener they wish to disable the  timeout on.

 - (-1) means unlimited

To deal with an interrupted thread you can catch and handle the exception yourself. If you want the thread to continue executing regardless you would theoretically do nothing - but to keep the code clean and proper I would implement it as below:

boolean interrupted = false;
try {
    while (true) {
        try {
            return queue.take();
        } catch (InterruptedException e) {
            interrupted = true;
            // fall through and retry
        }
    }
} finally {
    if (interrupted)
        Thread.currentThread().interrupt();
}

I like this example because it does not just leave an empty catch clause, it instead preserves the fact that it was called in a boolean. It does this so that when it finally does complete you can check if it was interrupted and if so respect it and interrupt the current thread.

For more information and where the example came from look into the following article: http://www.ibm.com/developerworks/library/j-jtp05236/