1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.job.internal

File DefaultJobManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

6
37
11
1
231
134
19
0.51
3.36
11
1.73

Classes

Class Line # Actions
DefaultJobManager 59 37 0% 19 35
0.3518518535.2%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.job.internal;
21   
22    import java.util.Arrays;
23    import java.util.List;
24    import java.util.Objects;
25    import java.util.concurrent.BlockingQueue;
26    import java.util.concurrent.LinkedBlockingQueue;
27   
28    import javax.inject.Inject;
29    import javax.inject.Named;
30    import javax.inject.Provider;
31    import javax.inject.Singleton;
32   
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.component.manager.ComponentLookupException;
35    import org.xwiki.component.manager.ComponentManager;
36    import org.xwiki.component.phase.Initializable;
37    import org.xwiki.component.phase.InitializationException;
38    import org.xwiki.context.Execution;
39    import org.xwiki.context.ExecutionContext;
40    import org.xwiki.context.ExecutionContextException;
41    import org.xwiki.context.ExecutionContextManager;
42    import org.xwiki.job.Job;
43    import org.xwiki.job.JobException;
44    import org.xwiki.job.JobManager;
45    import org.xwiki.job.JobStatusStore;
46    import org.xwiki.job.Request;
47    import org.xwiki.job.event.status.JobStatus;
48   
49    /**
50    * Default implementation of {@link JobManager}.
51    *
52    * @version $Id: 6442a2c684a5538b9ca5307daebd4aca2bae3fca $
53    * @since 4.0M1
54    * @deprecated since 6.1M2, use {@link DefaultJobExecutor} instead
55    */
56    @Component
57    @Singleton
58    @Deprecated
 
59    public class DefaultJobManager implements JobManager, Runnable, Initializable
60    {
61    /**
62    * Used to lookup {@link Job} implementations.
63    */
64    @Inject
65    @Named("context")
66    private Provider<ComponentManager> componentManager;
67   
68    /**
69    * Used to store the results of the jobs execution.
70    */
71    @Inject
72    private JobStatusStore store;
73   
74    /**
75    * Used to get the Execution Context.
76    */
77    @Inject
78    private Execution execution;
79   
80    /**
81    * Used to create a new Execution Context from scratch.
82    */
83    @Inject
84    private ExecutionContextManager executionContextManager;
85   
86    /**
87    * @see #getCurrentJob()
88    */
89    private volatile Job currentJob;
90   
91    /**
92    * The queue of jobs to execute.
93    */
94    private BlockingQueue<Job> jobQueue = new LinkedBlockingQueue<Job>();
95   
96    /**
97    * The thread on which the job manager is running.
98    */
99    private Thread thread;
100   
 
101  1 toggle @Override
102    public void initialize() throws InitializationException
103    {
104  1 initialize("Job Manager daemon thread");
105    }
106   
 
107  1 toggle protected void initialize(String threadName) throws InitializationException
108    {
109  1 this.thread = new Thread(this);
110  1 this.thread.setDaemon(true);
111  1 this.thread.start();
112  1 this.thread.setName(threadName);
113    }
114   
115    // Runnable
116   
 
117  1 toggle @Override
118    public void run()
119    {
120  1 while (!this.thread.isInterrupted()) {
121  1 runJob();
122    }
123    }
124   
125    /**
126    * Execute one job.
127    */
 
128  1 toggle public void runJob()
129    {
130  1 try {
131  1 this.currentJob = this.jobQueue.take();
132   
133    // Create a clean Execution Context
134  0 ExecutionContext context = new ExecutionContext();
135   
136  0 try {
137  0 this.executionContextManager.initialize(context);
138    } catch (ExecutionContextException e) {
139  0 throw new RuntimeException("Failed to initialize Job " + this.currentJob + " execution context", e);
140    }
141   
142  0 this.currentJob.run();
143    } catch (InterruptedException e) {
144    // Thread has been stopped
145    } finally {
146  0 this.execution.removeContext();
147    }
148    }
149   
150    // JobManager
151   
 
152  0 toggle @Override
153    public Job getCurrentJob()
154    {
155  0 return this.currentJob;
156    }
157   
158    /**
159    * @param jobType the job id
160    * @return a new job
161    * @throws JobException failed to create a job for the provided type
162    */
 
163  0 toggle private Job createJob(String jobType) throws JobException
164    {
165  0 Job job;
166  0 try {
167  0 job = this.componentManager.get().getInstance(Job.class, jobType);
168    } catch (ComponentLookupException e) {
169  0 throw new JobException("Failed to lookup any Job for role hint [" + jobType + "]", e);
170    }
171   
172  0 return job;
173    }
174   
 
175  0 toggle @Override
176    public Job executeJob(String jobType, Request request) throws JobException
177    {
178  0 Job job = addJob(jobType, request);
179   
180  0 try {
181  0 job.join();
182    } catch (InterruptedException e) {
183    // Ignore
184    }
185   
186  0 return job;
187    }
188   
 
189  0 toggle @Override
190    public Job addJob(String jobType, Request request) throws JobException
191    {
192  0 Job job = createJob(jobType);
193   
194  0 job.initialize(request);
195   
196  0 this.jobQueue.offer(job);
197   
198  0 return job;
199    }
200   
 
201  0 toggle @Override
202    public void addJob(Job job)
203    {
204  0 this.jobQueue.offer(job);
205    }
206   
 
207  0 toggle @Override
208    public JobStatus getJobStatus(String id)
209    {
210  0 return getJobStatus(Arrays.asList(id));
211    }
212   
 
213  1 toggle @Override
214    public JobStatus getJobStatus(List<String> id)
215    {
216    // Is it the current job
217  1 if (this.currentJob != null && Objects.equals(id, this.currentJob.getRequest().getId())) {
218  0 return this.currentJob.getStatus();
219    }
220   
221    // Is it in queue
222  1 for (Job job : this.jobQueue) {
223  0 if (Objects.equals(id, job.getRequest().getId())) {
224  0 return job.getStatus();
225    }
226    }
227   
228    // Is it stored
229  1 return this.store.getJobStatus(id);
230    }
231    }