This repository was archived by the owner on Mar 3, 2026. It is now read-only.
Commit df83fc3
committed
quartz module fix jooby-project#43
A job scheduler from [Quartz](http://quartz-scheduler.org/).
```xml
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-quartz</artifactId>
<version>{{version}}</version>
</dependency>
```
```java
import org.jooby.quartz.Quartz;
{
use(new Quartz().with(MyJob.class));
}
```
Previous example will startup Quartz and schedule MyJob.
A job can implement the ```Job``` interface as described in the [Quartz documentation]
(http://quartz-scheduler.org/documentation)
If you prefer to not implement the ```Job``` interface, all you have to do is to annotated a method with the ```Scheduled``` annotation.
By default, job name is set the class name or to the method name. Default group is set to the package name of the job class.
A job method must follow this rules:
* It must be a public method
* Without a return value
* Have ZERO arguments
* or just ONE argument of type ```JobExecutionContext```
The next section will you show how to add a trigger to a job and some examples too.
Trigger are defined by the ```Scheduled``` annotation. The annotation defined a single and required attributes, which is basically a trigger expression or a reference to it.
Example 1: run every 10s
```java
public class MyJob implements Job {
@scheduled("10s")
public void execute(JobExecutionContext ctx) throws JobExecutionException {
...
}
}
```
Example 2: run every 10s (no ```Job```)
```java
public class MyJob {
@scheduled("10s")
public void doWork() {
...
}
}
```
The ```Scheduled``` define a ```Quartz Trigger```. There you can put expressions
like: ```5s```, ```15minutes```, ```2hours```, etc... or a CRON expression: ```0/3 * * * * ?```.
It is also possible to put the name of property:
```java
public class MyJob {
@scheduled("job.expr")
public void doWork() {
...
}
}
```
And again the property: ```job.expr``` must be one of the previously described expressions.
If you have two or more jobs doing something similar, it is possible to group all them into one single class:
```java
public class MyJobs {
@scheduled("5minutes")
public void job1() {
...
}
@scheduled("1h")
public void job2() {
...
}
}
```
Not much to add here, just let you know jobs are created by Guice.
```java
public class MyJob {
private A a;
@Inject
public MyJob(A a) {
this.a = a;
}
@scheduled("5minutes")
public void doWork() {
this.a.doWork();
}
}
```
Injecting a ```Scheduler```
```java
public class MyJobManager {
private Scheduler scheduler;
@Inject
public MyJobManager(Scheduler scheduler) {
this.scheduler = scheduler;
}
}
```
Example: Setting max number of threads
```properties
org.quartz.threadPool.threadCount = 1 # default is number of available processors
```
Configuration follows the [Quartz
documentation](http://quartz-scheduler.org/documentation). The only difference is that you need to put add the properties on your ```*.conf``` file, NOT in a custom ```quartz.properties``` file.
Jdbc Store is fully supported but it depends on the <code>jooby-jdbc</code> module. So, in order to use the Jdbc Store you need to follow these steps:
1st. Install the Jdbc module:
```java
{
use(new Jdbc());
use(new Quartz(MyJob.class));
}
```
2nd. Set the quartz properties:
```properties
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource = db
```
When ```Scheduled``` isn't not enough and/or if you prefer to build jobs manually, you can try
one of the available alternatives.
Example 1: build the trigger and use default job naming
```java
{
use(new Quartz()
.with(MyJob.class, trigger {@literal ->} {
trigger
.withSchedule(withIntervalInDays(3))
.startAt(futureDate(10, MINUTES));
})
);
}
```
Example 2: build the job, the trigger and use default job naming
```java
{
use(new Quartz()
.with(MyJob.class, (job, trigger) {@literal ->} {
job.withDescription("etc...");
trigger
.withSchedule(withIntervalInDays(3))
.startAt(futureDate(10, MINUTES));
})
);
}
```
Example 3: build and set everything from scratch
```java
{
use(new Quartz()
.with(
newJob(MyJob.class).withDescription("etc...")
.build(),
newTrigger()
.withSchedule(withIntervalInDays(3))
.startAt(futureDate(10, MINUTES))
.build()
})
);
}
```
That's all folks! Enjoy it!!1 parent d760a83 commit df83fc3
File tree
48 files changed
+2644
-262
lines changed- coverage-report
- src/test/java/org/jooby/quartz
- jooby-jdbc/src
- main/java/org/jooby/jdbc
- test/java/org/jooby/jdbc
- jooby-netty/src/main/java/org/jooby/internal/netty
- jooby-quartz
- src
- main
- java/org/jooby
- internal/quartz
- quartz
- resources/org/jooby/quartz
- test/java/org/jooby
- internal/quartz
- quartz
- jooby-servlet/src/main/java/org/jooby/servlet
- jooby-undertow/src/main/java/org/jooby/internal/undertow
- jooby/src
- main/java/org/jooby
- internal
- mvc
- util
- test/java/org/jooby
- fn
- internal
- md/doc/jdbc-quartz
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
48 files changed
+2644
-262
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| 66 | + | |
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
| |||
179 | 181 | | |
180 | 182 | | |
181 | 183 | | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
182 | 190 | | |
183 | 191 | | |
184 | 192 | | |
| |||
Lines changed: 43 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
70 | 74 | | |
71 | 75 | | |
72 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
99 | | - | |
| 100 | + | |
100 | 101 | | |
| 102 | + | |
101 | 103 | | |
102 | 104 | | |
103 | 105 | | |
| |||
133 | 135 | | |
134 | 136 | | |
135 | 137 | | |
136 | | - | |
| 138 | + | |
137 | 139 | | |
| 140 | + | |
138 | 141 | | |
139 | 142 | | |
140 | 143 | | |
| |||
171 | 174 | | |
172 | 175 | | |
173 | 176 | | |
174 | | - | |
| 177 | + | |
175 | 178 | | |
| 179 | + | |
176 | 180 | | |
177 | 181 | | |
178 | 182 | | |
| |||
208 | 212 | | |
209 | 213 | | |
210 | 214 | | |
211 | | - | |
| 215 | + | |
212 | 216 | | |
| 217 | + | |
213 | 218 | | |
214 | 219 | | |
215 | 220 | | |
| |||
249 | 254 | | |
250 | 255 | | |
251 | 256 | | |
252 | | - | |
| 257 | + | |
253 | 258 | | |
| 259 | + | |
254 | 260 | | |
255 | 261 | | |
256 | 262 | | |
| |||
287 | 293 | | |
288 | 294 | | |
289 | 295 | | |
290 | | - | |
| 296 | + | |
291 | 297 | | |
| 298 | + | |
292 | 299 | | |
293 | 300 | | |
294 | 301 | | |
| |||
325 | 332 | | |
326 | 333 | | |
327 | 334 | | |
328 | | - | |
| 335 | + | |
329 | 336 | | |
| 337 | + | |
330 | 338 | | |
331 | 339 | | |
332 | 340 | | |
| |||
367 | 375 | | |
368 | 376 | | |
369 | 377 | | |
370 | | - | |
| 378 | + | |
371 | 379 | | |
| 380 | + | |
372 | 381 | | |
373 | 382 | | |
374 | 383 | | |
| |||
421 | 430 | | |
422 | 431 | | |
423 | 432 | | |
424 | | - | |
| 433 | + | |
425 | 434 | | |
| 435 | + | |
426 | 436 | | |
427 | 437 | | |
428 | 438 | | |
| |||
466 | 476 | | |
467 | 477 | | |
468 | 478 | | |
469 | | - | |
| 479 | + | |
470 | 480 | | |
| 481 | + | |
471 | 482 | | |
472 | 483 | | |
473 | 484 | | |
| |||
523 | 534 | | |
524 | 535 | | |
525 | 536 | | |
526 | | - | |
| 537 | + | |
527 | 538 | | |
| 539 | + | |
528 | 540 | | |
529 | 541 | | |
530 | 542 | | |
| |||
573 | 585 | | |
574 | 586 | | |
575 | 587 | | |
576 | | - | |
| 588 | + | |
577 | 589 | | |
| 590 | + | |
578 | 591 | | |
579 | 592 | | |
580 | 593 | | |
| |||
653 | 666 | | |
654 | 667 | | |
655 | 668 | | |
656 | | - | |
| 669 | + | |
657 | 670 | | |
| 671 | + | |
658 | 672 | | |
659 | 673 | | |
660 | 674 | | |
| |||
694 | 708 | | |
695 | 709 | | |
696 | 710 | | |
697 | | - | |
| 711 | + | |
698 | 712 | | |
| 713 | + | |
| 714 | + | |
699 | 715 | | |
700 | 716 | | |
701 | 717 | | |
| |||
735 | 751 | | |
736 | 752 | | |
737 | 753 | | |
738 | | - | |
| 754 | + | |
739 | 755 | | |
| 756 | + | |
740 | 757 | | |
741 | 758 | | |
742 | 759 | | |
| |||
776 | 793 | | |
777 | 794 | | |
778 | 795 | | |
779 | | - | |
| 796 | + | |
780 | 797 | | |
| 798 | + | |
781 | 799 | | |
782 | 800 | | |
783 | 801 | | |
| |||
817 | 835 | | |
818 | 836 | | |
819 | 837 | | |
820 | | - | |
| 838 | + | |
821 | 839 | | |
| 840 | + | |
822 | 841 | | |
823 | 842 | | |
824 | 843 | | |
| |||
858 | 877 | | |
859 | 878 | | |
860 | 879 | | |
861 | | - | |
| 880 | + | |
862 | 881 | | |
| 882 | + | |
863 | 883 | | |
864 | 884 | | |
865 | 885 | | |
| |||
899 | 919 | | |
900 | 920 | | |
901 | 921 | | |
902 | | - | |
| 922 | + | |
903 | 923 | | |
| 924 | + | |
904 | 925 | | |
905 | 926 | | |
906 | 927 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
49 | 48 | | |
50 | 49 | | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
48 | 56 | | |
49 | 57 | | |
50 | 58 | | |
51 | 59 | | |
52 | 60 | | |
53 | | - | |
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
| |||
0 commit comments