or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

caching.mdindex.mdmail.mdscheduling.mdtemplates.md

scheduling.mddocs/

0

# Quartz Scheduling

1

2

Spring Context Support provides enterprise-grade job scheduling integration with Quartz Scheduler. It supports cron-based triggers, method-invoking jobs, Spring dependency injection for scheduled tasks, and comprehensive lifecycle management.

3

4

## Capabilities

5

6

### Scheduler Factory Bean

7

8

Primary entry point for Quartz scheduler integration with full Spring lifecycle support and configuration management.

9

10

```java { .api }

11

/**

12

* FactoryBean for creating and configuring a Quartz Scheduler

13

*/

14

public class SchedulerFactoryBean implements FactoryBean<Scheduler>, BeanNameAware,

15

ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle {

16

17

/**

18

* Set the SchedulerFactory to use

19

* @param schedulerFactory the SchedulerFactory instance

20

*/

21

public void setSchedulerFactory(SchedulerFactory schedulerFactory);

22

23

/**

24

* Set the SchedulerFactory class to instantiate

25

* @param schedulerFactoryClass the SchedulerFactory class

26

*/

27

public void setSchedulerFactoryClass(Class<? extends SchedulerFactory> schedulerFactoryClass);

28

29

/**

30

* Set the scheduler name

31

* @param schedulerName the scheduler name

32

*/

33

public void setSchedulerName(String schedulerName);

34

35

/**

36

* Set the location of the Quartz properties file

37

* @param configLocation the properties file location

38

*/

39

public void setConfigLocation(Resource configLocation);

40

41

/**

42

* Set Quartz properties directly

43

* @param quartzProperties the Quartz properties

44

*/

45

public void setQuartzProperties(Properties quartzProperties);

46

47

/**

48

* Set the TaskExecutor for background task execution

49

* @param taskExecutor the TaskExecutor to use

50

*/

51

public void setTaskExecutor(Executor taskExecutor);

52

53

/**

54

* Set the DataSource for persistent job storage

55

* @param dataSource the DataSource to use

56

*/

57

public void setDataSource(DataSource dataSource);

58

59

/**

60

* Set the non-transactional DataSource for job storage

61

* @param nonTransactionalDataSource the non-transactional DataSource

62

*/

63

public void setNonTransactionalDataSource(DataSource nonTransactionalDataSource);

64

65

/**

66

* Set scheduler context parameters

67

* @param schedulerContextMap map of context parameters

68

*/

69

public void setSchedulerContextAsMap(Map<String, ?> schedulerContextMap);

70

71

/**

72

* Set the key for storing ApplicationContext in scheduler context

73

* @param applicationContextSchedulerContextKey the context key

74

*/

75

public void setApplicationContextSchedulerContextKey(String applicationContextSchedulerContextKey);

76

77

/**

78

* Set job factory for creating job instances

79

* @param jobFactory the job factory

80

*/

81

public void setJobFactory(JobFactory jobFactory);

82

83

/**

84

* Set whether to auto-start the scheduler after initialization

85

* @param autoStartup whether to auto-start

86

*/

87

public void setAutoStartup(boolean autoStartup);

88

89

/**

90

* Set the startup/shutdown phase

91

* @param phase the phase value

92

*/

93

public void setPhase(int phase);

94

95

/**

96

* Get the startup/shutdown phase

97

* @return the phase value

98

*/

99

public int getPhase();

100

101

/**

102

* Set the startup delay in seconds

103

* @param startupDelay delay in seconds

104

*/

105

public void setStartupDelay(int startupDelay);

106

107

/**

108

* Set whether to expose scheduler in repository

109

* @param exposeSchedulerInRepository whether to expose in repository

110

*/

111

public void setExposeSchedulerInRepository(boolean exposeSchedulerInRepository);

112

113

/**

114

* Set whether to wait for jobs to complete on shutdown

115

* @param waitForJobsToCompleteOnShutdown whether to wait for jobs

116

*/

117

public void setWaitForJobsToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown);

118

119

/**

120

* Set triggers to be registered with the scheduler

121

* @param triggers array of triggers

122

*/

123

public void setTriggers(Trigger... triggers);

124

125

/**

126

* Set job details to be registered with the scheduler

127

* @param jobDetails array of job details

128

*/

129

public void setJobDetails(JobDetail... jobDetails);

130

131

/**

132

* Set calendars to be registered with the scheduler

133

* @param calendars map of calendar names to Calendar objects

134

*/

135

public void setCalendars(Map<String, Calendar> calendars);

136

137

/**

138

* Get the Scheduler instance managed by this factory

139

* @return the Scheduler instance

140

*/

141

public Scheduler getScheduler();

142

143

/**

144

* Get the created Scheduler instance

145

* @return the Scheduler instance

146

*/

147

@Override

148

public Scheduler getObject();

149

}

150

```

151

152

**Usage Examples:**

153

154

```java

155

@Configuration

156

@EnableScheduling

157

public class QuartzConfig {

158

159

@Bean

160

public SchedulerFactoryBean schedulerFactoryBean() {

161

SchedulerFactoryBean factory = new SchedulerFactoryBean();

162

factory.setConfigLocation(new ClassPathResource("quartz.properties"));

163

factory.setJobFactory(springBeanJobFactory());

164

factory.setAutoStartup(true);

165

factory.setStartupDelay(30);

166

factory.setOverwriteExistingJobs(true);

167

factory.setWaitForJobsToCompleteOnShutdown(true);

168

return factory;

169

}

170

171

@Bean

172

public SpringBeanJobFactory springBeanJobFactory() {

173

SpringBeanJobFactory jobFactory = new SpringBeanJobFactory();

174

jobFactory.setApplicationContext(applicationContext);

175

return jobFactory;

176

}

177

178

// Complete scheduler configuration with jobs and triggers

179

@Bean

180

public SchedulerFactoryBean completeScheduler() {

181

SchedulerFactoryBean factory = new SchedulerFactoryBean();

182

183

// Set jobs and triggers

184

factory.setJobDetails(

185

emailReportJobDetail().getObject(),

186

dataCleanupJobDetail().getObject()

187

);

188

189

factory.setTriggers(

190

emailReportTrigger().getObject(),

191

dataCleanupTrigger().getObject()

192

);

193

194

return factory;

195

}

196

}

197

```

198

199

### Job Creation and Configuration

200

201

Factory beans for creating and configuring Quartz job details with Spring integration.

202

203

```java { .api }

204

/**

205

* FactoryBean for creating Quartz JobDetail objects

206

*/

207

public class JobDetailFactoryBean implements FactoryBean<JobDetail>, BeanNameAware, InitializingBean {

208

209

/**

210

* Set the job class to execute

211

* @param jobClass the job class

212

*/

213

public void setJobClass(Class<? extends Job> jobClass);

214

215

/**

216

* Set the job name

217

* @param name the job name

218

*/

219

public void setName(String name);

220

221

/**

222

* Set the job group

223

* @param group the job group

224

*/

225

public void setGroup(String group);

226

227

/**

228

* Set job description

229

* @param description the job description

230

*/

231

public void setDescription(String description);

232

233

/**

234

* Set job data as a Map

235

* @param jobDataAsMap map of job data

236

*/

237

public void setJobDataAsMap(Map<String, ?> jobDataAsMap);

238

239

/**

240

* Set job durability (whether job persists without triggers)

241

* @param durability whether the job should be durable

242

*/

243

public void setDurability(boolean durability);

244

245

/**

246

* Set whether the job should request recovery after scheduler failure

247

* @param requestsRecovery whether to request recovery

248

*/

249

public void setRequestsRecovery(boolean requestsRecovery);

250

251

/**

252

* Get the created JobDetail

253

* @return the JobDetail instance

254

*/

255

@Override

256

public JobDetail getObject();

257

}

258

259

/**

260

* FactoryBean for method-invoking job details

261

*/

262

public class MethodInvokingJobDetailFactoryBean extends JobDetailFactoryBean

263

implements BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, InitializingBean {

264

265

/**

266

* Set the target object to invoke methods on

267

* @param targetObject the target object

268

*/

269

public void setTargetObject(Object targetObject);

270

271

/**

272

* Set the target class for static method invocation

273

* @param targetClass the target class

274

*/

275

public void setTargetClass(Class<?> targetClass);

276

277

/**

278

* Set the target bean name from the application context

279

* @param targetBeanName the target bean name

280

*/

281

public void setTargetBeanName(String targetBeanName);

282

283

/**

284

* Set the method name to invoke

285

* @param targetMethod the method name

286

*/

287

public void setTargetMethod(String targetMethod);

288

289

/**

290

* Set arguments for the method invocation

291

* @param arguments the method arguments

292

*/

293

public void setArguments(Object... arguments);

294

295

/**

296

* Set whether the job is concurrent (default: false)

297

* @param concurrent whether to allow concurrent execution

298

*/

299

public void setConcurrent(boolean concurrent);

300

}

301

```

302

303

### Trigger Configuration

304

305

Factory beans for creating different types of Quartz triggers.

306

307

```java { .api }

308

/**

309

* FactoryBean for creating cron-based triggers

310

*/

311

public class CronTriggerFactoryBean implements FactoryBean<CronTrigger>, BeanNameAware, InitializingBean {

312

313

/**

314

* Set the cron expression

315

* @param cronExpression the cron expression

316

*/

317

public void setCronExpression(String cronExpression);

318

319

/**

320

* Set the time zone for the cron expression

321

* @param timeZone the time zone

322

*/

323

public void setTimeZone(TimeZone timeZone);

324

325

/**

326

* Set the JobDetail this trigger should fire

327

* @param jobDetail the job detail

328

*/

329

public void setJobDetail(JobDetail jobDetail);

330

331

/**

332

* Set the trigger name

333

* @param name the trigger name

334

*/

335

public void setName(String name);

336

337

/**

338

* Set the trigger group

339

* @param group the trigger group

340

*/

341

public void setGroup(String group);

342

343

/**

344

* Set the trigger description

345

* @param description the trigger description

346

*/

347

public void setDescription(String description);

348

349

/**

350

* Set the start time

351

* @param startTime the start time

352

*/

353

public void setStartTime(Date startTime);

354

355

/**

356

* Set the start delay in milliseconds

357

* @param startDelay delay in milliseconds

358

*/

359

public void setStartDelay(long startDelay);

360

361

/**

362

* Set trigger priority

363

* @param priority the priority (default: 5)

364

*/

365

public void setPriority(int priority);

366

367

/**

368

* Set misfire instruction

369

* @param misfireInstruction the misfire instruction

370

*/

371

public void setMisfireInstruction(int misfireInstruction);

372

373

/**

374

* Set job data map

375

* @param jobDataMap the job data map

376

*/

377

public void setJobDataMap(JobDataMap jobDataMap);

378

379

/**

380

* Get the created CronTrigger

381

* @return the CronTrigger instance

382

*/

383

@Override

384

public CronTrigger getObject();

385

}

386

387

/**

388

* FactoryBean for creating simple triggers

389

*/

390

public class SimpleTriggerFactoryBean implements FactoryBean<SimpleTrigger>, BeanNameAware, InitializingBean {

391

392

/**

393

* Set the JobDetail this trigger should fire

394

* @param jobDetail the job detail

395

*/

396

public void setJobDetail(JobDetail jobDetail);

397

398

/**

399

* Set the start time

400

* @param startTime the start time

401

*/

402

public void setStartTime(Date startTime);

403

404

/**

405

* Set the start delay in milliseconds

406

* @param startDelay delay in milliseconds

407

*/

408

public void setStartDelay(long startDelay);

409

410

/**

411

* Set the repeat interval in milliseconds

412

* @param repeatInterval interval in milliseconds

413

*/

414

public void setRepeatInterval(long repeatInterval);

415

416

/**

417

* Set the repeat count (-1 for infinite)

418

* @param repeatCount the repeat count

419

*/

420

public void setRepeatCount(int repeatCount);

421

422

/**

423

* Set misfire instruction

424

* @param misfireInstruction the misfire instruction

425

*/

426

public void setMisfireInstruction(int misfireInstruction);

427

428

/**

429

* Get the created SimpleTrigger

430

* @return the SimpleTrigger instance

431

*/

432

@Override

433

public SimpleTrigger getObject();

434

}

435

```

436

437

**Usage Examples:**

438

439

```java

440

@Configuration

441

public class JobConfig {

442

443

@Bean

444

public JobDetailFactoryBean emailReportJobDetail() {

445

JobDetailFactoryBean factory = new JobDetailFactoryBean();

446

factory.setJobClass(EmailReportJob.class);

447

factory.setName("emailReportJob");

448

factory.setGroup("reports");

449

factory.setDescription("Daily email report job");

450

factory.setDurability(true);

451

452

Map<String, Object> jobData = new HashMap<>();

453

jobData.put("reportType", "daily");

454

jobData.put("recipients", Arrays.asList("admin@example.com"));

455

factory.setJobDataAsMap(jobData);

456

457

return factory;

458

}

459

460

@Bean

461

public CronTriggerFactoryBean emailReportTrigger() {

462

CronTriggerFactoryBean factory = new CronTriggerFactoryBean();

463

factory.setJobDetail(emailReportJobDetail().getObject());

464

factory.setCronExpression("0 0 8 * * ?"); // Daily at 8 AM

465

factory.setName("emailReportTrigger");

466

factory.setGroup("reports");

467

return factory;

468

}

469

470

@Bean

471

public MethodInvokingJobDetailFactoryBean methodInvokingJob() {

472

MethodInvokingJobDetailFactoryBean factory = new MethodInvokingJobDetailFactoryBean();

473

factory.setTargetBeanName("dataService");

474

factory.setTargetMethod("cleanupExpiredData");

475

factory.setName("dataCleanupJob");

476

factory.setConcurrent(false);

477

return factory;

478

}

479

}

480

```

481

482

### Job Implementation Support

483

484

Base classes and factories for implementing Spring-aware Quartz jobs.

485

486

```java { .api }

487

/**

488

* Base class for Spring-aware Quartz jobs with automatic property injection

489

*/

490

public abstract class QuartzJobBean implements Job {

491

492

/**

493

* Execute the job. Subclasses should implement this method.

494

* @param context the job execution context

495

* @throws JobExecutionException if job execution fails

496

*/

497

protected abstract void executeInternal(JobExecutionContext context) throws JobExecutionException;

498

499

/**

500

* Set a property value from the JobDataMap

501

* @param name the property name

502

* @param value the property value

503

*/

504

public final void setBeanName(String name);

505

506

/**

507

* Template method that delegates to executeInternal

508

* @param context the job execution context

509

* @throws JobExecutionException if job execution fails

510

*/

511

@Override

512

public final void execute(JobExecutionContext context) throws JobExecutionException;

513

}

514

515

/**

516

* JobFactory that enables dependency injection for Quartz jobs

517

*/

518

public class SpringBeanJobFactory extends AdaptableJobFactory

519

implements ApplicationContextAware, SchedulerContextAware {

520

521

@Override

522

protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception;

523

524

/**

525

* Set the ApplicationContext for dependency injection

526

* @param applicationContext the application context

527

*/

528

@Override

529

public void setApplicationContext(ApplicationContext applicationContext);

530

531

/**

532

* Set the SchedulerContext

533

* @param schedulerContext the scheduler context

534

*/

535

@Override

536

public void setSchedulerContext(SchedulerContext schedulerContext);

537

}

538

539

/**

540

* Base class for adaptable job factories

541

*/

542

public class AdaptableJobFactory implements JobFactory {

543

544

/**

545

* Create a job instance for the given bundle

546

* @param bundle the trigger fired bundle

547

* @return the job instance

548

* @throws Exception if job creation fails

549

*/

550

protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception;

551

552

/**

553

* Create a new job instance

554

* @param bundle the trigger fired bundle

555

* @return the job instance

556

* @throws SchedulerException if job creation fails

557

*/

558

@Override

559

public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException;

560

}

561

562

/**

563

* Job that delegates execution to a target object and method

564

*/

565

public class DelegatingJob implements Job {

566

567

/**

568

* Execute the delegated job

569

* @param context the job execution context

570

* @throws JobExecutionException if execution fails

571

*/

572

@Override

573

public void execute(JobExecutionContext context) throws JobExecutionException;

574

}

575

```

576

577

**Usage Examples:**

578

579

```java

580

@Component

581

public class EmailReportJob extends QuartzJobBean {

582

583

@Autowired

584

private EmailService emailService;

585

586

@Autowired

587

private ReportService reportService;

588

589

// Properties automatically injected from JobDataMap

590

private String reportType;

591

private List<String> recipients;

592

593

@Override

594

protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

595

try {

596

String report = reportService.generateReport(reportType);

597

598

for (String recipient : recipients) {

599

emailService.sendReport(recipient, "Daily Report", report);

600

}

601

602

log.info("Email report job completed successfully");

603

} catch (Exception e) {

604

throw new JobExecutionException("Failed to execute email report job", e);

605

}

606

}

607

608

// Setters for JobDataMap injection

609

public void setReportType(String reportType) {

610

this.reportType = reportType;

611

}

612

613

public void setRecipients(List<String> recipients) {

614

this.recipients = recipients;

615

}

616

}

617

```

618

619

### Integration Support Classes

620

621

Classes for enhanced Spring integration and custom behavior.

622

623

```java { .api }

624

/**

625

* Interface for beans that need access to the SchedulerContext

626

*/

627

public interface SchedulerContextAware {

628

/**

629

* Set the SchedulerContext that this object runs in

630

* @param schedulerContext the SchedulerContext

631

*/

632

void setSchedulerContext(SchedulerContext schedulerContext);

633

}

634

635

/**

636

* JobStore implementation that uses a local DataSource

637

*/

638

public class LocalDataSourceJobStore extends JobStoreTX {

639

640

/**

641

* Set the DataSource to use for the JobStore

642

* @param dataSource the DataSource

643

*/

644

public void setDataSource(DataSource dataSource);

645

646

/**

647

* Get the DataSource used by this JobStore

648

* @return the DataSource

649

*/

650

public DataSource getDataSource();

651

}

652

653

/**

654

* ThreadPool implementation that delegates to a Spring TaskExecutor

655

*/

656

public class LocalTaskExecutorThreadPool implements ThreadPool {

657

658

/**

659

* Set the TaskExecutor to delegate to

660

* @param taskExecutor the TaskExecutor

661

*/

662

public void setTaskExecutor(TaskExecutor taskExecutor);

663

664

/**

665

* Get the TaskExecutor being used

666

* @return the TaskExecutor

667

*/

668

public TaskExecutor getTaskExecutor();

669

670

@Override

671

public boolean runInThread(Runnable runnable);

672

673

@Override

674

public int blockForAvailableThreads();

675

676

@Override

677

public void initialize() throws SchedulerConfigException;

678

679

@Override

680

public void shutdown(boolean waitForJobsToComplete);

681

682

@Override

683

public int getPoolSize();

684

}

685

686

/**

687

* TaskExecutor implementation that uses Quartz SimpleThreadPool

688

*/

689

public class SimpleThreadPoolTaskExecutor extends CustomizableThreadFactory

690

implements TaskExecutor, InitializingBean, DisposableBean {

691

692

/**

693

* Set the thread count

694

* @param threadCount the number of threads

695

*/

696

public void setThreadCount(int threadCount);

697

698

/**

699

* Set the thread priority

700

* @param threadPriority the thread priority

701

*/

702

public void setThreadPriority(int threadPriority);

703

704

/**

705

* Set whether threads should be daemon threads

706

* @param daemon whether threads are daemon threads

707

*/

708

public void setDaemon(boolean daemon);

709

710

/**

711

* Execute a task

712

* @param task the task to execute

713

*/

714

@Override

715

public void execute(Runnable task);

716

}

717

```

718

719

### Exception Classes

720

721

Specific exceptions for job scheduling failures.

722

723

```java { .api }

724

/**

725

* Exception thrown when job method invocation fails

726

*/

727

public class JobMethodInvocationFailedException extends JobExecutionException {

728

729

/**

730

* Create exception for method invocation failure

731

* @param methodInvocation the failed method invocation

732

* @param cause the underlying cause

733

*/

734

public JobMethodInvocationFailedException(MethodInvocation methodInvocation, Throwable cause);

735

736

/**

737

* Get details about the failed method invocation

738

* @return the method invocation details

739

*/

740

public MethodInvocation getMethodInvocation();

741

}

742

```