-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#30669 fix extract jobQueueManagerAPI init and destroy
- Loading branch information
1 parent
40311d9
commit c6e620f
Showing
3 changed files
with
94 additions
and
113 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
dotCMS/src/main/java/com/dotcms/rest/api/v1/JobQueueManagerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.dotcms.rest.api.v1; | ||
|
||
import com.dotcms.jobs.business.api.JobProcessorScanner; | ||
import com.dotcms.jobs.business.api.JobQueueManagerAPI; | ||
import com.dotcms.jobs.business.processor.JobProcessor; | ||
import com.dotcms.jobs.business.processor.Queue; | ||
import com.dotmarketing.util.Logger; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import java.lang.reflect.Constructor; | ||
import java.util.List; | ||
|
||
@ApplicationScoped | ||
public class JobQueueManagerHelper { | ||
|
||
private JobProcessorScanner scanner; | ||
|
||
@Inject | ||
public JobQueueManagerHelper(JobProcessorScanner scanner) { | ||
this.scanner = scanner; | ||
} | ||
|
||
public JobQueueManagerHelper() { | ||
} | ||
|
||
public void registerProcessors(JobQueueManagerAPI jobQueueManagerAPI) { | ||
if (!jobQueueManagerAPI.isStarted()) { | ||
jobQueueManagerAPI.start(); | ||
Logger.info(this.getClass(), "JobQueueManagerAPI started"); | ||
} | ||
|
||
List<Class<? extends JobProcessor>> processors = scanner.discoverJobProcessors(); | ||
processors.forEach(processor -> { | ||
try { | ||
if (!testInstantiation(processor)) { | ||
return; | ||
} | ||
registerProcessor(jobQueueManagerAPI, processor); | ||
} catch (Exception e) { | ||
Logger.error(this.getClass(), "Unable to register JobProcessor ", e); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Test if a processor can be instantiated | ||
* @param processor The processor to tested | ||
* @return true if the processor can be instantiated, false otherwise | ||
*/ | ||
private boolean testInstantiation(Class<? extends JobProcessor> processor) { | ||
try { | ||
Constructor<? extends JobProcessor> declaredConstructor = processor.getDeclaredConstructor(); | ||
declaredConstructor.newInstance(); | ||
return true; | ||
} catch (Exception e) { | ||
Logger.error(this.getClass(), String.format(" JobProcessor [%s] cannot be instantiated and will be ignored.", processor.getName()), e); | ||
} | ||
return false; | ||
} | ||
|
||
private void registerProcessor(JobQueueManagerAPI jobQueueManagerAPI, Class<? extends JobProcessor> processor) { | ||
if (processor.isAnnotationPresent(Queue.class)) { | ||
Queue queue = processor.getAnnotation(Queue.class); | ||
jobQueueManagerAPI.registerProcessor(queue.value(), processor); | ||
} else { | ||
jobQueueManagerAPI.registerProcessor(processor.getName(), processor); | ||
} | ||
} | ||
|
||
public void shutdown(JobQueueManagerAPI jobQueueManagerAPI) { | ||
if (jobQueueManagerAPI.isStarted()) { | ||
try { | ||
jobQueueManagerAPI.close(); | ||
Logger.info(this.getClass(), "JobQueueManagerAPI successfully closed"); | ||
} catch (Exception e) { | ||
Logger.error(this.getClass(), e.getMessage(), e); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters