View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * JobSchedulerService
51   */
52  public final class JobSchedulerService
53  {
54      private static volatile JobSchedulerService _singleton;
55      private static Scheduler _scheduler;
56  
57      /** Creates a new instance of JobSchedulerService */
58      private JobSchedulerService(  )
59      {
60      }
61  
62      /**
63       * Gets the unique instance of the service
64       * @return The service's instance
65       */
66      public static JobSchedulerService getInstance(  )
67      {
68          if ( _singleton == null )
69          {
70              synchronized ( JobSchedulerService.class )
71              {
72                  JobSchedulerService service = new JobSchedulerService(  );
73                  service.init(  );
74                  _singleton = service;
75              }
76          }
77  
78          return _singleton;
79      }
80  
81      /**
82       * Initialize the service.
83       */
84      private void init(  )
85      {
86          SchedulerFactory factory = new StdSchedulerFactory(  );
87  
88          try
89          {
90              _scheduler = factory.getScheduler(  );
91              _scheduler.start(  );
92              AppLogService.info( "Lutece job scheduler started." );
93          }
94          catch ( SchedulerException e )
95          {
96              AppLogService.error( "Error starting the Lutece job scheduler ", e );
97          }
98      }
99  
100     /**
101      * Schedule a job according cron information
102      * @param job The Job to schedule
103      * @param trigger The Cron trigger
104      * @return Date
105      */
106     public Date scheduleJob( JobDetail job, CronTrigger trigger )
107     {
108         Date date = null;
109 
110         if ( _scheduler != null )
111         {
112             try
113             {
114                 date = _scheduler.scheduleJob( job, trigger );
115                 AppLogService.info( "New job scheduled : " + job.getName(  ) );
116             }
117             catch ( SchedulerException e )
118             {
119                 AppLogService.error( "Error scheduling job " + job.getName(  ), e );
120             }
121         }
122 
123         return date;
124     }
125 
126     /**
127      * Shutdown the service (Called by the core while the webapp is destroyed)
128      */
129     public static void shutdown(  )
130     {
131         if ( _scheduler != null )
132         {
133             try
134             {
135                 _scheduler.shutdown(  );
136                 AppLogService.info( "Lutece job scheduler stopped." );
137             }
138             catch ( SchedulerException e )
139             {
140                 AppLogService.error( "Error shuting down the Lutece job scheduler ", e );
141             }
142         }
143     }
144 }