diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/JobQueueManagerHelper.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/JobQueueManagerHelper.java new file mode 100644 index 000000000000..ee791720941c --- /dev/null +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/JobQueueManagerHelper.java @@ -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> 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 processor) { + try { + Constructor 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 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); + } + } + } +} + diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportHelper.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportHelper.java index 52411427eddb..a7d1afb50e15 100644 --- a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportHelper.java +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportHelper.java @@ -4,6 +4,7 @@ import com.dotcms.jobs.business.api.JobQueueManagerAPI; import com.dotcms.jobs.business.processor.JobProcessor; import com.dotcms.jobs.business.processor.Queue; +import com.dotcms.rest.api.v1.JobQueueManagerHelper; import com.dotcms.rest.api.v1.temp.DotTempFile; import com.dotmarketing.business.APILocator; import com.dotmarketing.business.web.WebAPILocator; @@ -29,15 +30,13 @@ public class ContentImportHelper { private static final String CMD_PREVIEW = "preview"; private static final String CMD_PUBLISH = "publish"; - JobQueueManagerAPI jobQueueManagerAPI; - JobProcessorScanner scanner; + private JobQueueManagerAPI jobQueueManagerAPI; + private JobQueueManagerHelper jobQueueManagerHelper; @Inject - public ContentImportHelper( - JobQueueManagerAPI jobQueueManagerAPI, - JobProcessorScanner scanner) { + public ContentImportHelper(JobQueueManagerAPI jobQueueManagerAPI, JobQueueManagerHelper jobQueueManagerHelper) { this.jobQueueManagerAPI = jobQueueManagerAPI; - this.scanner = scanner; + this.jobQueueManagerHelper = jobQueueManagerHelper; } public ContentImportHelper() { @@ -45,57 +44,12 @@ public ContentImportHelper() { @PostConstruct public void onInit() { - - if(!jobQueueManagerAPI.isStarted()){ - jobQueueManagerAPI.start(); - Logger.info(this.getClass(), "JobQueueManagerAPI started"); - } - final List> processors = scanner.discoverJobProcessors(); - processors.forEach(processor -> { - try { - if(!testInstantiation(processor)){ - return; - } - //registering the processor with the jobQueueManagerAPI - // lower case it to avoid case - if(processor.isAnnotationPresent(Queue.class)){ - final Queue queue = processor.getAnnotation(Queue.class); - jobQueueManagerAPI.registerProcessor(queue.value(), processor); - } else { - jobQueueManagerAPI.registerProcessor(processor.getName(), 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 processor) { - try { - final Constructor declaredConstructor = processor.getDeclaredConstructor(); - declaredConstructor.newInstance(); - return true; - } catch (Exception e) { - Logger.error(this.getClass(), String.format(" JobProcessor [%s] can not be instantiated and will be ignored.",processor.getName()), e); - } - return false; + jobQueueManagerHelper.registerProcessors(jobQueueManagerAPI); } @PreDestroy public void onDestroy() { - if(jobQueueManagerAPI.isStarted()){ - try { - jobQueueManagerAPI.close(); - Logger.info(this.getClass(), "JobQueueManagerAPI successfully closed"); - } catch (Exception e) { - Logger.error(this.getClass(), e.getMessage(), e); - } - } + jobQueueManagerHelper.shutdown(jobQueueManagerAPI); } /** diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/job/JobQueueHelper.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/job/JobQueueHelper.java index 534986fb7b37..50e2c0c158d7 100644 --- a/dotCMS/src/main/java/com/dotcms/rest/api/v1/job/JobQueueHelper.java +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/job/JobQueueHelper.java @@ -10,6 +10,7 @@ import com.dotcms.jobs.business.job.JobState; import com.dotcms.jobs.business.processor.JobProcessor; import com.dotcms.jobs.business.processor.Queue; +import com.dotcms.rest.api.v1.JobQueueManagerHelper; import com.dotcms.rest.api.v1.temp.DotTempFile; import com.dotcms.rest.api.v1.temp.TempFileAPI; import com.dotmarketing.business.APILocator; @@ -45,73 +46,17 @@ @ApplicationScoped public class JobQueueHelper { - JobQueueManagerAPI jobQueueManagerAPI; - - JobProcessorScanner scanner; + private JobQueueManagerAPI jobQueueManagerAPI; + private JobQueueManagerHelper jobQueueManagerHelper; public JobQueueHelper() { //default constructor Mandatory for CDI } - @PostConstruct - public void onInit() { - - if(!jobQueueManagerAPI.isStarted()){ - jobQueueManagerAPI.start(); - Logger.info(this.getClass(), "JobQueueManagerAPI started"); - } - final List> processors = scanner.discoverJobProcessors(); - processors.forEach(processor -> { - try { - if(!testInstantiation(processor)){ - return; - } - //registering the processor with the jobQueueManagerAPI - // lower case it to avoid case - if(processor.isAnnotationPresent(Queue.class)){ - final Queue queue = processor.getAnnotation(Queue.class); - jobQueueManagerAPI.registerProcessor(queue.value(), processor); - } else { - jobQueueManagerAPI.registerProcessor(processor.getName(), 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 processor) { - try { - final Constructor declaredConstructor = processor.getDeclaredConstructor(); - declaredConstructor.newInstance(); - return true; - } catch (Exception e) { - Logger.error(this.getClass(), String.format(" JobProcessor [%s] can not be instantiated and will be ignored.",processor.getName()), e); - } - return false; - } - - @PreDestroy - public void onDestroy() { - if(jobQueueManagerAPI.isStarted()){ - try { - jobQueueManagerAPI.close(); - Logger.info(this.getClass(), "JobQueueManagerAPI successfully closed"); - } catch (Exception e) { - Logger.error(this.getClass(), e.getMessage(), e); - } - } - } - @Inject - public JobQueueHelper(JobQueueManagerAPI jobQueueManagerAPI, JobProcessorScanner scanner) { + public JobQueueHelper(JobQueueManagerAPI jobQueueManagerAPI, JobQueueManagerHelper jobQueueManagerHelper) { this.jobQueueManagerAPI = jobQueueManagerAPI; - this.scanner = scanner; + this.jobQueueManagerHelper = jobQueueManagerHelper; } /**