
net.scattersphere.util.thread.JobManagerCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scattersphere-core Show documentation
Show all versions of scattersphere-core Show documentation
Scattersphere Core library, used to run the main server.
The newest version!
/*
* Scattersphere
* Copyright 2014-2015, Scattersphere Project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.scattersphere.util.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* This is a cache class that is used to store a list of running, completed, and failed jobs.
*
* Created by kenji on 12/28/14.
*/
public class JobManagerCache {
private static final JobManagerCache instance = new JobManagerCache();
/** Map of running jobs: jobId, context */
private final Map runningJobs;
/** List of completed jobs. */
private final List completedJobs;
/** List of failed jobs. */
private final List failedJobs;
private JobManagerCache() {
this.runningJobs = new ConcurrentHashMap<>();
this.completedJobs = new ArrayList<>();
this.failedJobs = new ArrayList<>();
}
/**
* Retrieves the default instance of this class.
*
* @return {@link JobManagerCache} instance.
*/
public static JobManagerCache instance() {
return instance;
}
/**
* Adds a running job to the list.
*
* @param job The {@link JobExecutionContext} object.
*/
public void addRunningJob(JobExecutionContext job) {
runningJobs.put(job.getJobContext().getJobId(), job);
}
/**
* Removes a running job from the list.
*
* @param job The {@link net.scattersphere.util.thread.JobExecutionContext} to remove.
*/
public void removeRunningJob(JobExecutionContext job) {
runningJobs.remove(job.getJobContext().getJobId());
}
/**
* Adds a completed job to the list.
*
* @param job {@link net.scattersphere.util.thread.JobExecutionContext} object to add.
*/
public void addCompletedJob(JobExecutionContext job) {
completedJobs.add(job);
}
/**
* Adds a failed job to the list.
*
* @param job {@link net.scattersphere.util.thread.JobExecutionContext} object to add.
*/
public void addFailedJob(JobExecutionContext job) {
failedJobs.add(job);
}
/**
* Returns the {@link JobExecutionContext} for the job by ID.
*
* @param jobId The job ID to search for.
* @return {@link net.scattersphere.util.thread.JobExecutionContext} if found, {@code null} otherwise.
*/
public JobExecutionContext findJobStatus(String jobId) {
JobExecutionContext foundJob = null;
if ((foundJob = runningJobs.get(jobId)) != null) {
return foundJob;
}
// TODO
// Reduce these functions down to a lambda or set of lamdbas
for (JobExecutionContext completedJob : completedJobs) {
if (completedJob.getJobContext().getJobId().equals(jobId)) {
return completedJob;
}
}
for (JobExecutionContext failedJob : failedJobs) {
if (failedJob.getJobContext().getJobId().equals(jobId)) {
return failedJob;
}
}
return null;
}
/**
* Retrieves the list of running jobs. Order is not guaranteed.
*
* @return {@link List} of {@link JobExecutionContext} objects.
*/
public List getRunningJobs() {
return new ArrayList<>(runningJobs.values());
}
/**
* Retrieves a list of completed jobs.
*
* @return {@link List} of {@link JobExecutionContext} objects.
*/
public List getCompletedJobs() {
return completedJobs;
}
/**
* Retrieves a list of failed or interrupted jobs.
*
* @return {@link List} of {@link JobExecutionContext} objects.
*/
public List getFailedJobs() {
return failedJobs;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy