Search code examples
javaquartz-schedulerjob-scheduling

Quartz Job won't execute method containing an exception (try/catch or throws)


As the title mention, I'm trying to use Quartz to schedule a periodic job to send emails. A basic job test works perfectly (some hello world prints, etc.), but once I call a method having a try/catch clause or throwing an exeption, the job won't call it. You'll find below the different pieces of my code responsible of the job handling.

BAPJob.java : The job I want to execute

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.echallenge.util.MailService;

public class BAPJob implements Job {

    public void execute(JobExecutionContext context) throws JobExecutionException {

        System.out.println("EXECUTION STARTING ...");

        MailService.test();

        System.out.println("EXECUTION ENDING");
    }

}

MailService.Java : The class containing the method I want to call

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class MailService {

    public static void test(){
        System.out.println("TESTING I ...");

        try {
            InternetAddress.parse("[email protected]");
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

JobInitializer.java : The servlet that launch the job (has a 2 value in in web.xml)

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Servlet implementation class JobInitializer
 */
public class JobInitializer extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {

        try {
            JobDetail job = JobBuilder.newJob(BAPJob.class).withIdentity("bapjob", "group1").build();

            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("bapjobtrigger", "group1")
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever())
                    .build();

            String key = "org.quartz.impl.StdSchedulerFactory.KEY";
            ServletContext servletContext = config.getServletContext();
            StdSchedulerFactory factory = (StdSchedulerFactory) servletContext.getAttribute(key);
            Scheduler scheduler = factory.getScheduler("MyQuartzScheduler");

            scheduler.start();
            scheduler.scheduleJob(job, trigger);

        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

After starting the application on the Tomcat server, I get the following output:

EXECUTION STARTING ...
EXECUTION STARTING ...
EXECUTION STARTING ...

Thank you guys !


Solution

  • I found the solution and I'll leave it here just in case.

    Since the try catch only catches exceptions, I needed to catch "errors" as well, so I catched Throwable and it showed me a message error that wasn't visible before.

    So the I changed my Job to be as the following:

    public class BAPJob implements Job {
    
        public void execute(JobExecutionContext context) throws JobExecutionException {
    
            System.out.println("EXECUTION STARTING ...");
    
            try {
                InternetAddress.parse("[email protected]");
            } catch (Throwable t) {
                t.printStackTrace();
            }
    
            System.out.println("EXECUTION ENDING");
        }
    
    }