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 package fr.paris.lutece.portal.service.scheduler;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37
38 import org.quartz.CronTrigger;
39 import org.quartz.JobDetail;
40 import org.quartz.Scheduler;
41 import org.quartz.SchedulerException;
42 import org.quartz.SchedulerFactory;
43
44 import org.quartz.impl.StdSchedulerFactory;
45
46 import java.util.Date;
47
48
49
50
51 public final class JobSchedulerService
52 {
53 private static JobSchedulerService _singleton;
54 private static Scheduler _scheduler;
55
56
57 private JobSchedulerService( )
58 {
59 init( );
60 }
61
62
63
64
65
66
67 public static synchronized JobSchedulerService getInstance( )
68 {
69 if ( _singleton == null )
70 {
71 _singleton = new JobSchedulerService( );
72 }
73
74 return _singleton;
75 }
76
77
78
79
80 private void init( )
81 {
82 SchedulerFactory factory = new StdSchedulerFactory( );
83
84 try
85 {
86 _scheduler = factory.getScheduler( );
87 _scheduler.start( );
88 AppLogService.info( "Lutece job scheduler started." );
89 }
90 catch( SchedulerException e )
91 {
92 AppLogService.error( "Error starting the Lutece job scheduler ", e );
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105 public Date scheduleJob( JobDetail job, CronTrigger trigger )
106 {
107 Date date = null;
108
109 if ( _scheduler != null )
110 {
111 try
112 {
113 date = _scheduler.scheduleJob( job, trigger );
114 AppLogService.info( "New job scheduled : {}", job.getKey( ).getName( ) );
115 }
116 catch( SchedulerException e )
117 {
118 AppLogService.error( "Error scheduling job {}", job.getKey( ).getName( ), e );
119 }
120 }
121
122 return date;
123 }
124
125
126
127
128 public static void shutdown( )
129 {
130 if ( _scheduler != null )
131 {
132 try
133 {
134 _scheduler.shutdown( );
135 AppLogService.info( "Lutece job scheduler stopped." );
136 }
137 catch( SchedulerException e )
138 {
139 AppLogService.error( "Error shuting down the Lutece job scheduler ", e );
140 }
141 }
142 }
143 }