Meu teste do Quartz não funciona. Por quê?

3 respostas
Fox_McCloud

Por que esse teste (abaixo) simplesmente não roda? O que está faltando?

private void testScheduling() {
    try{
            JobDetail jd = new JobDetail("nome", "grupo", TestJob.class);
    
            // Get the cron Expression as an Init parameter
            CronTrigger cronTrigger = new CronTrigger("nome", "grupo");
            cronTrigger.setCronExpression("0/10 * * * * ?");
    
            // Agendando a Tarefa
            Scheduler sched = new StdSchedulerFactory().getScheduler();
            
            sched.start();
            
            sched.scheduleJob(jd, cronTrigger);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

private class TestJob implements Job {
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("teste");
    }
}

3 Respostas

C

Amigo acredito que vc ja tenha resolvido pelo tempo que a msg está no ar mas pelo que vi está faltando o construtor em branco da classe TestJob. Sem ele o Quartz não consegue instanciar a Classe.

Ficando asim

private class TestJob implements Job {  
    public TestJob (){
    }
 
    public void execute(JobExecutionContext context) throws JobExecutionException {   
        System.out.println("teste");   
    }   
}

Falou

Fox_McCloud

clausas:
Amigo acredito que vc ja tenha resolvido pelo tempo que a msg está no ar mas pelo que vi está faltando o construtor em branco da classe TestJob. Sem ele o Quartz não consegue instanciar a Classe.

Ficando asim

private class TestJob implements Job {  
    public TestJob (){
    }
 
    public void execute(JobExecutionContext context) throws JobExecutionException {   
        System.out.println("teste");   
    }   
}

Falou


Thank you very much!

:wink:

Fox_McCloud
clausas:
Amigo acredito que vc ja tenha resolvido pelo tempo que a msg está no ar mas pelo que vi está faltando o construtor em branco da classe TestJob. Sem ele o Quartz não consegue instanciar a Classe.

Ficando asim

private class TestJob implements Job {  
    public TestJob (){
    }
 
    public void execute(JobExecutionContext context) throws JobExecutionException {   
        System.out.println("teste");   
    }   
}

Falou


O Quartz também tem dificuldades com classes internas ou com visibilidade modificada... definindo o TestJob como classe interna pública estática deu certo!

Assim funcionou:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;


public class SchedulingTest {

	// 'G' - ERA
	// 'y' - YEAR
	// 'M' - MONTH
	// 'k' - HOUR_OF_DAY: 1-based.  eg, 23:59 + 1 hour =>> 24:59
	// 'E' - DAY_OF_WEEK
	// 'a' - AM_PM
	// 'h' - HOUR:1-based.  eg, 11PM + 1 hour =>> 12 AM
	// 'z' - ZONE_OFFSET
	// 'Z' - ZONE_OFFSET
    // 'd' - DATE
    // 'H' - HOUR_OF_DAY:0-based.  eg, 23:59 + 1 hour =>> 00:59
    // 'm' - MINUTE
    // 's' - SECOND
    // 'S' - MILLISECOND
    // 'D' - DAY_OF_YEAR
    // 'F' - DAY_OF_WEEK_IN_MONTH
    // 'w' - WEEK_OF_YEAR
    // 'W' - WEEK_OF_MONTH
    // 'K' - HOUR: 0-based.  eg, 11PM + 1 hour =>> 0 AM
	private static final DateFormat dateFormat = new SimpleDateFormat("DD/MM/yyyy - HH:mm:ss.SSS", new Locale("pt", "BR"));
	
	private void test() {  
	    try{  
	            JobDetail jd = new JobDetail("nome", "grupo", TestJob.class);  
	      
	            // Get the cron Expression as an Init parameter  
	            CronTrigger cronTrigger = new CronTrigger("nome", "grupo");  
	            cronTrigger.setCronExpression("/2 * * * * ?");  
	      
	            // Agendando a Tarefa  
	            Scheduler sched = new StdSchedulerFactory().getScheduler();  
	              
	            sched.start();
	            
	            sched.scheduleJob(jd, cronTrigger);  
	    } catch(Exception e) {  
	        e.printStackTrace();  
	    }  
	}

	public static class TestJob implements Job {
		
		public TestJob() {
			super();
		}
		
	    public void execute(JobExecutionContext context) throws JobExecutionException {
	        System.err.println("Current Time: "+SchedulingTest.dateFormat.format(new Date(System.currentTimeMillis())));  
	    }  
	}
	
	public static void main(String[] args) {
		new SchedulingTest().test();
	}
	
}
Criado 8 de agosto de 2008
Ultima resposta 30 de mar. de 2009
Respostas 3
Participantes 2