Tomasz Nowak
Tomasz Nowak

Reputation: 251

ScheduledExecutorService stops working even when all exceptions are caught

I have ScheduledExecutorService set to execute every minute. My command catches all exceptions to prevent executor service to stop working. However, for some reason, it sometimes stops working (let's say once every few days, probably when application has more traffic).

My method starting executor service:

public void startExecutor(ScheduledExecutorService executor) {
   Runnable job = new Runnable() {
        @Override
        public void run() {
            doJob();
        }
    };
    executor.scheduleAtFixedRate(job, 0, 1, TimeUnit.MINUTES);
}

ScheduledExecutorService is initialized by Dropwizard this way:

@Override
public void run(NuvrProjectsAndRecordingsConfiguration configuration, Environment environment) throws Exception {
    ScheduledExecutorService executor = environment.lifecycle().scheduledExecutorService("job").build();
}

Dropwizard creates ScheduledThreadPoolExecutor implementation with corePoolSize equal to 1.

The doJob() method catches all exceptions:

private void doJob() {
    try {
        worker.process();
    } catch (Exception e) {
        LOGGER.error("Cannot perform job, will retry", e);
    }
}

And the process() method handles regular exceptions and has finally section:

public void process() {
    try {
        // some hard stuff
    } catch (IOException ex) {
        LOGGER.error("Processing of job failed", ex);
    } catch (RuntimeException e) {
        LOGGER.error("Unknown error during recording processor", e);
    } finally {
        // clean after processing
    }
}

What is interesting is that sometimes Exception is thrown from finally section.

Since all operations are protected by global catch-exception section - why do executor service stops working?

Upvotes: 1

Views: 806

Answers (1)

Igor Loskutov
Igor Loskutov

Reputation: 2325

You may need to catch all Throwables as the Exception class doesn't include subclass Error.

Upvotes: 1

Related Questions