我正在使用spring boot,我有一个服务,它可以从DB中存储检索一些数据。我想使用一个使用我的服务的quartz作业。我尝试了许多互联网上的建议来整合石英和spring boot,但它没有工作。你能帮帮我吗?这是我的代码。1) 我在pom.xml中添加了org.quartz-scheduler作为依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
</dependencies>
2) 这是我的application.properties,配置为使用mysql作为服务的DB。
server.port=8281 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:/localhost:3306mydb spring.datasource.username spring.datasource.password=mypassword。
3)这是我的服务。
@Service public class ReportService { JdbcTemplate jdbcTemplate; public ReportService(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public List getPendingReports() { String sql = "SELECT * FROM report WHERE status = '" + ReportStatus.PENDING.name() +"'"; ReportMapper reportsMapper = new ReportMapper(); List reports = jdbcTemplate.query(sql, reportsMapper); return reports; } }。 }
4)调度器类。
import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz. SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class ReportScheduler { public void scanAndUpdateReports() { SchedulerFactory sf = new StdSchedulerFactory(); try { Scheduler scheduler = sf. getScheduler(); scheduler.start(); JobDetail job = JobBuilder.newJob(ReportsJob.class) .withIdentity("reportsJob") .build(); SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity("reportsTrigger") .startNow() . withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(60).repeatForever()) .build(); scheduler.scheduleJob(job, trigger); } catch (SchedulerException e) { / TODO 自动生成的 catch block e.printStackTrace(); } }。 } }
5) 还有我的job类,我想自动生成ReportService,但它是空的。
public class ReportsJob implements Job { @Autowired ReportService reportService; /this doesn't work (it is null) @Override public void execute(JobExecutionContext context) throws JobExecutionException { List reports = reportService.getPendingReports(); System.out.println("Reports:\n "+reports); } } }
6)现在我从main方法调用调度器。
@SpringBootApplication public class ReportAppBeApplication { public static void main(String[]args) { SpringApplication.run(ReportAppBeApplication.class, args); ReportScheduler rs = new ReportScheduler(); rs.scanAndUpdateReports(); }。 }
它是空的,因为 @Autowired
只适用于 @Component
, @Repository
或 @Service
annotated class.Annotate你的 ReportJob
类,并确保你存储该类的地图包含在 component scan
启动时。您可以将扫描用的地图包括在内。
@ComponentScan(basePackages={"folderOfReportJob"})
这个注释应该被添加到 @Configuration
或 @SpringBootApplication
注释类。
EDIT:
如果你不想让你的工作成为一个组件,你也可以通过手动获得bean ApplicationContext
类。它是这样的。
private static ApplicationContext ac;
ReportService reportService = ac.getBean(ReportService.class);