All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.caelum.vraptor.tasks.TaskHandler Maven / Gradle / Ivy

package br.com.caelum.vraptor.tasks;

import java.text.ParseException;
import java.util.List;

import org.quartz.Trigger;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.tasks.helpers.TriggerBuilder;
import br.com.caelum.vraptor.tasks.scheduler.Scheduled;
import br.com.caelum.vraptor.tasks.scheduler.TaskScheduler;

@Component
@ApplicationScoped
public class TaskHandler {

	private final TaskScheduler scheduler;
	private final TriggerBuilder builder;

	public TaskHandler(TaskScheduler scheduler, TriggerBuilder builder, List tasks) {
		this.scheduler = scheduler;
		this.builder = builder;
		for (Task task : tasks) {
			if(task.getClass().isAnnotationPresent(Scheduled.class))
				scheduleTask(task.getClass());
		}
	}
	
	private void scheduleTask(Class task) {
		try {
			Trigger trigger = builder.triggerFor(task);
			String id = getTaskId(task);
			scheduler.schedule(task, trigger, id);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	private String getTaskId(Class task) {
		Scheduled params = task.getAnnotation(Scheduled.class);
		return !params.id().isEmpty() ? params.id() : task.getSimpleName();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy