发表于: 2018-01-13 21:01:29
1 701
今天完成的事
做了一个定时任务
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
@MapperScan(value = "com.ptteng.dao")
@SpringBootApplication
public class QuartzApplication {
public static void main(String[] args) {
SpringApplication.run(QuartzApplication.class, args);
}
}
package com.ptteng;
import com.ptteng.pojo.MybatisDemo;
import com.ptteng.service.InserUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by ${MIND-ZR} on 2018/1/13.
*/
//@Component
//@Configurable
//@EnableScheduling
public class ScheduledTasks {
@Autowired
InserUserService insertUserService;
@Scheduled(fixedRate = 1000 * 30)
public void reportCurrentTime(){
MybatisDemo mybatisDemo=new MybatisDemo();
mybatisDemo.setmAge(11);
mybatisDemo.setmName("小三");
insertUserService.insertUser(mybatisDemo);
System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date()));
}
//每1分钟执行一次
@Scheduled(cron = "0 */1 * * * * ")
public void reportCurrentByCron(){
System.out.println ("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date ()));
}
private SimpleDateFormat dateFormat(){
return new SimpleDateFormat ("HH:mm:ss");
}
}
package com.ptteng.conf;
import com.ptteng.pojo.CScheduleTrigger;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by ${MIND-ZR} on 2018/1/13.
*/
public interface CScheduleTriggerRepository extends JpaRepository {
List<CScheduleTrigger> queryAll();
}
package com.ptteng.conf;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import java.io.IOException;
import java.util.Properties;
/**
* Created by ${MIND-ZR} on 2018/1/13.
*/
public class QuartzConfigration {
@Autowired
private MyJobFactory myJobFactory; //自定义的factory
//获取工厂bean
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
try {
schedulerFactoryBean.setQuartzProperties(quartzProperties());
schedulerFactoryBean.setJobFactory(myJobFactory);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return schedulerFactoryBean;
}
//指定quartz.properties
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
//创建schedule
@Bean(name = "scheduler")
public Scheduler scheduler() {
return schedulerFactoryBean().getScheduler();
}
}
没搞成功,明天接着搞。
遇到的问题
quartz的数据源设置总是出问题
明天的计划
方案修改
评论