File tree Expand file tree Collapse file tree
spring-boot/bean-lifecycle/src
main/java/io/reflectoring/beanlifecycle/quartz
test/java/io/reflectoring/beanlifecycle/quartz Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55import org .springframework .context .annotation .Bean ;
66import org .springframework .context .annotation .Configuration ;
77import org .springframework .scheduling .quartz .SchedulerFactoryBean ;
8+ import org .springframework .scheduling .quartz .SpringBeanJobFactory ;
89
910@ Configuration
1011public class QuartzConfig {
1112
1213 @ Autowired
1314 ApplicationContext applicationContext ;
1415
16+ @ Bean
17+ public SpringBeanJobFactory springBeanJobFactory () {
18+ AutowireCapableJobFactory jobFactory = new AutowireCapableJobFactory ();
19+ jobFactory .setApplicationContext (applicationContext );
20+ return jobFactory ;
21+ }
22+
1523 /**
1624 * Create custom {@link SchedulerFactoryBean} for Quartz
1725 */
1826 @ Bean
1927 public SchedulerFactoryBean schedulerFactoryBean (){
20- AutowireCapableJobFactory jobFactory = new AutowireCapableJobFactory ();
21- jobFactory .setApplicationContext (applicationContext );
22-
2328 SchedulerFactoryBean factoryBean = new SchedulerFactoryBean ();
24- factoryBean .setJobFactory (jobFactory );
25-
29+ factoryBean .setJobFactory (springBeanJobFactory ());
2630 return factoryBean ;
2731 }
2832
Original file line number Diff line number Diff line change 1+ package io .reflectoring .beanlifecycle .quartz ;
2+
3+ import org .quartz .Job ;
4+ import org .quartz .JobExecutionContext ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+
7+ public class SampleJob implements Job {
8+
9+ @ Autowired
10+ private SampleServiceBean service ;
11+
12+ @ Override
13+ public void execute (JobExecutionContext jobExecutionContext ) {
14+ service .hello ();
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package io .reflectoring .beanlifecycle .quartz ;
2+
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+ import org .springframework .stereotype .Service ;
6+
7+ @ Service
8+ public class SampleServiceBean {
9+
10+ Logger logger = LoggerFactory .getLogger (SampleServiceBean .class );
11+
12+ public void hello () {
13+ logger .info ("hello from Quartz Job!" );
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1+ package io .reflectoring .beanlifecycle .quartz ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .quartz .*;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .boot .test .context .SpringBootTest ;
7+
8+ @ SpringBootTest
9+ public class SpringQuartzTests {
10+
11+ @ Autowired
12+ Scheduler quartzScheduler ;
13+
14+ @ Test
15+ public void testQuartzJobHasCapabaleOfAutowireSpringBeans () throws SchedulerException , InterruptedException {
16+
17+ JobDetail jobDetail = JobBuilder .newJob (SampleJob .class ).build ();
18+ Trigger trigger = TriggerBuilder .newTrigger ()
19+ .withSchedule (SimpleScheduleBuilder .repeatSecondlyForever ())
20+ .startNow ().build ();
21+ quartzScheduler .scheduleJob (jobDetail , trigger );
22+
23+ Thread .sleep (5000 );
24+
25+ }
26+
27+ }
You can’t perform that action at this time.
0 commit comments