org.apache.sling.event.impl.jobs
Class JobManagerImpl

java.lang.Object
  extended by org.apache.sling.event.impl.jobs.stats.StatisticsImpl
      extended by org.apache.sling.event.impl.jobs.JobManagerImpl
All Implemented Interfaces:
Runnable, org.apache.sling.discovery.TopologyEventListener, JobManager, Statistics, org.osgi.service.event.EventHandler

@Service(value={JobManager.class,org.osgi.service.event.EventHandler.class,org.apache.sling.discovery.TopologyEventListener.class,java.lang.Runnable.class})
@Properties(value={@Property(name="repository.path",value="/var/eventing/jobs"),@Property(name="scheduler.period",longValue=60L),@Property(name="scheduler.concurrent",boolValue=false),@Property(name="event.topics",value={"org/apache/sling/api/resource/Resource/ADDED","org/apache/sling/event/notification/job/*","org/osgi/framework/BundleEvent/STARTED","org/osgi/framework/BundleEvent/UPDATED"})})
public class JobManagerImpl
extends StatisticsImpl
implements JobManager, org.osgi.service.event.EventHandler, org.apache.sling.discovery.TopologyEventListener, Runnable

Implementation of the job manager.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.sling.event.jobs.JobManager
JobManager.QueryType
 
Constructor Summary
JobManagerImpl()
           
 
Method Summary
protected  void activate(Map<String,Object> props)
          Activate this component.
 Job addJob(String topic, Map<String,Object> properties)
          Add a new job If the topic is null or illegal, no job is created and null is returned.
 Job addJob(String topic, String name, Map<String,Object> properties)
          Add a new job If the topic is null or illegal, no job is created and null is returned.
protected  void deactivate()
          Deactivate this component.
 org.osgi.service.event.Event findJob(String topic, Map<String,Object> template)
          Find a job - either scheduled or active.
 Collection<Job> findJobs(JobManager.QueryType type, String topic, long limit, Map<String,Object>... templates)
          Return all jobs either running or scheduled.
 void finished(JobHandler info)
           
 void forceRemoveJob(String jobId)
          Cancel this job.
 Job getJob(String topic, Map<String,Object> template)
          Find a job - either scheduled or active.
 Job getJobById(String id)
           
 Job getJobByName(String name)
           
 Queue getQueue(String name)
          Return a queue with a specific name (if running)
 Iterable<Queue> getQueues()
          Return an iterator for all available queues.
 Statistics getStatistics()
          Return our internal statistics object.
 Iterable<TopicStatistics> getTopicStatistics()
          Return statistics information about job topics.
 TopologyCapabilities getTopologyCapabilities()
           
 void handleEvent(org.osgi.service.event.Event event)
           
 void handleTopologyEvent(org.apache.sling.discovery.TopologyEvent event)
           
 boolean isJobProcessingEnabled()
          Is job processing enabled?
 JobsIterator queryJobs(JobManager.QueryType type, String topic, long limit, Map<String,Object>... templates)
          Return all jobs either running or scheduled.
 JobsIterator queryJobs(JobManager.QueryType type, String topic, Map<String,Object>... templates)
          Return all jobs either running or scheduled.
 void reassign(JobHandler handler)
           
 boolean remove(JobImpl job)
          Remove the job.
 boolean removeJob(String jobId)
          Cancel this job.
 boolean removeJobById(String jobId)
          Removes the job even if it is currently in processing.
 boolean reschedule(JobHandler info)
          Reschedule a job.
 void reset()
          Clear all collected statistics and set the starting time to the current time.
 void restart()
          Restart the job manager.
 void run()
          This method is invoked periodically by the scheduler.
 boolean start(JobHandler info)
          Try to start the job
protected  void update(Map<String,Object> props)
          Configure this component.
 
Methods inherited from class org.apache.sling.event.impl.jobs.stats.StatisticsImpl
add, addActive, cancelledJob, clearQueued, copyFrom, decQueued, failedJob, finishedJob, getAverageProcessingTime, getAverageWaitingTime, getLastActivatedJobTime, getLastFinishedJobTime, getNumberOfActiveJobs, getNumberOfCancelledJobs, getNumberOfFailedJobs, getNumberOfFinishedJobs, getNumberOfJobs, getNumberOfProcessedJobs, getNumberOfQueuedJobs, getStartTime, incQueued
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JobManagerImpl

public JobManagerImpl()
Method Detail

activate

@Activate
protected void activate(Map<String,Object> props)
                 throws org.apache.sling.api.resource.LoginException
Activate this component.

Parameters:
props - Configuration properties
Throws:
org.apache.sling.api.resource.LoginException

update

@Modified
protected void update(Map<String,Object> props)
Configure this component.

Parameters:
props - Configuration properties

deactivate

@Deactivate
protected void deactivate()
Deactivate this component.


run

public void run()
This method is invoked periodically by the scheduler. In the default configuration every minute

Specified by:
run in interface Runnable
See Also:
Runnable.run()

reset

public void reset()
Description copied from interface: Statistics
Clear all collected statistics and set the starting time to the current time. Note that not all fields are cleared, last waiting time or number of active and queued jobs is not cleared as these are currently used.

Specified by:
reset in interface Statistics
Overrides:
reset in class StatisticsImpl
See Also:
Reset this statistics and all queues.

restart

public void restart()
Description copied from interface: JobManager
Restart the job manager. This method restarts the job manager and all queues - currently processed jobs will be finished. The job manager should only be restarted if really necessary!

Specified by:
restart in interface JobManager
See Also:
JobManager.restart()

isJobProcessingEnabled

public boolean isJobProcessingEnabled()
Description copied from interface: JobManager
Is job processing enabled? It is possible to completely turn off job processing.

Specified by:
isJobProcessingEnabled in interface JobManager
See Also:
JobManager.isJobProcessingEnabled()

handleEvent

public void handleEvent(org.osgi.service.event.Event event)
Specified by:
handleEvent in interface org.osgi.service.event.EventHandler
See Also:
EventHandler.handleEvent(org.osgi.service.event.Event)

handleTopologyEvent

public void handleTopologyEvent(org.apache.sling.discovery.TopologyEvent event)
Specified by:
handleTopologyEvent in interface org.apache.sling.discovery.TopologyEventListener
See Also:
TopologyEventListener.handleTopologyEvent(org.apache.sling.discovery.TopologyEvent)

getStatistics

public Statistics getStatistics()
Return our internal statistics object.

Specified by:
getStatistics in interface JobManager
See Also:
JobManager.getStatistics()

getTopicStatistics

public Iterable<TopicStatistics> getTopicStatistics()
Description copied from interface: JobManager
Return statistics information about job topics.

Specified by:
getTopicStatistics in interface JobManager
See Also:
JobManager.getTopicStatistics()

getQueue

public Queue getQueue(String name)
Description copied from interface: JobManager
Return a queue with a specific name (if running)

Specified by:
getQueue in interface JobManager
Parameters:
name - The queue name
Returns:
The queue or null
See Also:
JobManager.getQueue(java.lang.String)

getQueues

public Iterable<Queue> getQueues()
Description copied from interface: JobManager
Return an iterator for all available queues.

Specified by:
getQueues in interface JobManager
See Also:
JobManager.getQueues()

queryJobs

public JobsIterator queryJobs(JobManager.QueryType type,
                              String topic,
                              Map<String,Object>... templates)
Description copied from interface: JobManager
Return all jobs either running or scheduled.

Specified by:
queryJobs in interface JobManager
Parameters:
type - Required parameter for the type: either all jobs, only queued or only started can be returned.
topic - Topic can be used as a filter, if it is non-null, only jobs with this topic will be returned.
templates - A list of filter property maps. Each map acts like a template. The searched job must match the template (AND query). By providing several maps, different filters are possible (OR query).
Returns:
A non null collection.

queryJobs

public JobsIterator queryJobs(JobManager.QueryType type,
                              String topic,
                              long limit,
                              Map<String,Object>... templates)
Description copied from interface: JobManager
Return all jobs either running or scheduled.

Specified by:
queryJobs in interface JobManager
Parameters:
type - Required parameter for the type: either all jobs, only queued or only started can be returned.
topic - Topic can be used as a filter, if it is non-null, only jobs with this topic will be returned.
limit - A positive number indicating the maximum number of jobs returned by the iterator.
templates - A list of filter property maps. Each map acts like a template. The searched job must match the template (AND query). By providing several maps, different filters are possible (OR query).
Returns:
A non null collection.

findJob

public org.osgi.service.event.Event findJob(String topic,
                                            Map<String,Object> template)
Description copied from interface: JobManager
Find a job - either scheduled or active. This method searches for an event with the given topic and filter properties. If more than one job matches, the first one found is returned which could be any of the matching jobs.

Specified by:
findJob in interface JobManager
Parameters:
topic - Topic is required.
template - The map acts like a template. The searched job must match the template (AND query).
Returns:
An event or null

removeJob

public boolean removeJob(String jobId)
Description copied from interface: JobManager
Cancel this job. Canceling a job might fail if the job is currently in processing.

Specified by:
removeJob in interface JobManager
Parameters:
jobId - The unique identifier as found in the property JobUtil.JOB_ID.
Returns:
true if the job could be cancelled or does not exist anymore. false otherwise.
See Also:
JobManager.removeJob(java.lang.String)

forceRemoveJob

public void forceRemoveJob(String jobId)
Description copied from interface: JobManager
Cancel this job. This method acts like JobManager.removeJob(String) with the exception that it waits for a job to finish. The job will be removed when this method returns - however this method blocks until the job is finished!

Specified by:
forceRemoveJob in interface JobManager
Parameters:
jobId - The unique identifier as found in the property JobUtil.JOB_ID.
See Also:
JobManager.forceRemoveJob(java.lang.String)

addJob

public Job addJob(String topic,
                  Map<String,Object> properties)
Description copied from interface: JobManager
Add a new job If the topic is null or illegal, no job is created and null is returned. A job topic is a hierarchical name separated by dashes, each part has to start with a letter, allowed characters are letters, numbers and the underscore.

Specified by:
addJob in interface JobManager
Parameters:
topic - The required job topic.
properties - Optional job properties
Returns:
The new job - or null if the job could not be created.
See Also:
JobManager.addJob(java.lang.String, java.util.Map)

addJob

public Job addJob(String topic,
                  String name,
                  Map<String,Object> properties)
Description copied from interface: JobManager
Add a new job If the topic is null or illegal, no job is created and null is returned. A job topic is a hierarchical name separated by dashes, each part has to start with a letter, allowed characters are letters, numbers and the underscore. This method allows to specify a job name which should uniquely identify this job. If a job with the same name is started on different instances, the job is still processed only once. However, the topology api in combination with the leader selection provides a better way for dealing with this situation and as jobs with name come with a heavy processing overhead these should be avoided.

Specified by:
addJob in interface JobManager
Parameters:
topic - The required job topic.
name - Optional unique job name
properties - Optional job properties
Returns:
The new job - or null if the job could not be created.
See Also:
JobManager.addJob(java.lang.String, java.lang.String, java.util.Map)

getJobByName

public Job getJobByName(String name)
Specified by:
getJobByName in interface JobManager
Returns:
A job or null
See Also:
JobManager.getJobByName(java.lang.String)

getJobById

public Job getJobById(String id)
Specified by:
getJobById in interface JobManager
Parameters:
id - The unique identifier from Job.getId()
Returns:
A job or null
See Also:
JobManager.getJobById(java.lang.String)

getJob

public Job getJob(String topic,
                  Map<String,Object> template)
Description copied from interface: JobManager
Find a job - either scheduled or active. This method searches for an event with the given topic and filter properties. If more than one job matches, the first one found is returned which could be any of the matching jobs.

Specified by:
getJob in interface JobManager
Parameters:
topic - Topic is required.
template - The map acts like a template. The searched job must match the template (AND query).
Returns:
A job or null
See Also:
JobManager.getJob(java.lang.String, java.util.Map)

removeJobById

public boolean removeJobById(String jobId)
Description copied from interface: JobManager
Removes the job even if it is currently in processing. If the job exists and is not in processing, it gets removed from the processing queue. If the job exists and is in processing, it is removed from the persistence layer, however processing is not stopped.

Specified by:
removeJobById in interface JobManager
Parameters:
jobId - The unique identifier from Job.getId()
Returns:
true if the job could be removed or does not exist anymore. false otherwise.
See Also:
JobManager.removeJobById(java.lang.String)

findJobs

public Collection<Job> findJobs(JobManager.QueryType type,
                                String topic,
                                long limit,
                                Map<String,Object>... templates)
Description copied from interface: JobManager
Return all jobs either running or scheduled.

Specified by:
findJobs in interface JobManager
Parameters:
type - Required parameter for the type: either all jobs, only queued or only started can be returned.
topic - Topic can be used as a filter, if it is non-null, only jobs with this topic will be returned.
limit - A positive number indicating the maximum number of jobs returned by the iterator. A value of zero or less indicates that all jobs should be returned.
templates - A list of filter property maps. Each map acts like a template. The searched job must match the template (AND query). By providing several maps, different filters are possible (OR query).
Returns:
A collection of jobs - the collection might be empty.
See Also:
org.apache.sling.event.jobs.JobManager#findJobs(org.apache.sling.event.jobs.JobManager.QueryType, java.lang.String, long, java.util.Map[])

finished

public void finished(JobHandler info)

reschedule

public boolean reschedule(JobHandler info)
Reschedule a job. Update the retry count and remove the started time.

Parameters:
info - The job info
Returns:
true if the job could be updated.

remove

public boolean remove(JobImpl job)
Remove the job.

Parameters:
info -
Returns:

start

public boolean start(JobHandler info)
Try to start the job


reassign

public void reassign(JobHandler handler)

getTopologyCapabilities

public TopologyCapabilities getTopologyCapabilities()


Copyright © 2007-2013 The Apache Software Foundation. All Rights Reserved.