package com.yonge.cooleshow.task.core;

import javax.sql.DataSource;

import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

@Component
public class LocalSchedulerFactoryBean extends SchedulerFactoryBean {

	@Autowired
	private DataSource dataSource;

	@Autowired
	private LocalJobFactory jobFactory;

	@Value("${task.configLocation}")
	private Resource configLocation;

	@Value("${task.autoStartup}")
	private boolean autoStartup;

	@Value("${task.startupDelay}")
	private int startupDelay;

	private boolean waitForJobsToCompleteOnShutdown = true;

	@Override
	public void afterPropertiesSet() throws Exception {
		setDataSource(dataSource);
		setJobFactory(jobFactory);
		setOverwriteExistingJobs(true);
		setConfigLocation(configLocation);
		setWaitForJobsToCompleteOnShutdown(waitForJobsToCompleteOnShutdown);
		setStartupDelay(startupDelay);
		setAutoStartup(autoStartup);
		
		super.afterPropertiesSet();
	}

	@Override
	public void setBeanName(String name) {
		// 重写该方法的目的是为了scheduler的实例名称遵循quartz配置文件的配置，除非在spring的配置文件中指定了实例名称
	}

	@Override
	public void setTriggers(Trigger... triggers) {
		throw new UnsupportedOperationException("Not allowed to add the triggers instantiation");
	}

}
