DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Making Spring and Quartz Work Together Smoothly

Making Spring and Quartz Work Together Smoothly

Bozhidar Bozhanov user avatar by
Bozhidar Bozhanov
·
Aug. 26, 11 · Interview
Like (0)
Save
Tweet
Share
10.69K Views

Join the DZone community and get the full member experience.

Join For Free

Spring has quartz support. But as you can see it requires a lot of old-style xml mappings for each of the jobs you want to run. And it looks more complex than needed. To an extent that it is a common opinion that if you have to use quartz and spring, you should use quartz separately (and not utilize the spring quartz support).

On the other hand spring scheduling has become pretty nice: @Scheduled + task:annotation-driven is what you need for most scenarios. But it is not as feature-rich as quartz – you don’t have an interface for controlling the jobs execution – stop, pause, resume. So quartz can be considered superior. So what do we have as requirements:

  • use quartz, as a feature-rich scheduling technology
  • use spring to provide dependency-injection in quartz jobs
  • make it as simple as possible to write and configure a new job

It appears that you can’t do this. So I had to create something new, on top of existing spring classes, that would make it easier to work with quartz and still have every job instance as spring bean – with dependency injection, AOP, etc. My goals:

  • Schedule the job with annotation – I started with @Scheduled, but it is based on the idea of annotating methods, rather than classes. And it doesn’t support the richer quartz meta-data (job name, group, durability, etc)
  • Minimum xml – the whole setup should be enabled through one or two bean definitions, but adding new jobs should not require changes
  • Ability to control the quartz jobs

I asked a question on stackoverflow, to check if there’s an existing solution. It appears there isn’t, so I implemented the above, and here is the end result (as answer to the stackoverflow question – I won’t copy/paste it here, it looks better there anyway). An example job would look like this:

@ScheduledJob(cronExpression="${some.cron.property}")
public class SomeJob implements Job { .. }

As you see you don’t even need @Service / @Component, because @ScheduledJob makes use of spring stereotype support (using @Component as meta-annotation). You can also use properties and SpEL in the cronExpression property – you can externalize it, instead of hard-coding it.

There is some room for simplifying it even further. Perhaps I can get rid of the need to implement the Job interface, and instead annotate methods with @ScheduledJob, and then wrapping them in an adapter for the Job interface, but that would make it more complex in the wiring part, without giving that much benefit.

I have already suggested it as improvement to spring. If they like the approach I believe they will fix some issues that I may have overlooked. If they don’t like it, I can make it a separate project.

 

From http://techblog.bozho.net/?p=452

Spring Framework Quartz (scheduler)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Host Hack Attempt Detection Using ELK
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • How to Use Buildpacks to Build Java Containers
  • Detecting Network Anomalies Using Apache Spark

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: