-
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.
#30545 Changed job manager initialization to the InitServlet, also, c…
…hanged the processors discovery from Jandex to CDI.
- Loading branch information
1 parent
8c6f422
commit 085eb37
Showing
15 changed files
with
183 additions
and
271 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
dotCMS/src/main/java/com/dotcms/jobs/business/api/JobProcessorDiscovery.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,88 @@ | ||
package com.dotcms.jobs.business.api; | ||
|
||
import com.dotcms.jobs.business.processor.JobProcessor; | ||
import com.dotmarketing.exception.DotRuntimeException; | ||
import com.dotmarketing.util.Logger; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.inject.Any; | ||
import javax.enterprise.inject.spi.Bean; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.inject.Inject; | ||
|
||
/** | ||
* Discovers all classes that implement the JobProcessor interface using CDI. | ||
*/ | ||
@ApplicationScoped | ||
public class JobProcessorDiscovery { | ||
|
||
private final BeanManager beanManager; | ||
|
||
@Inject | ||
public JobProcessorDiscovery(BeanManager beanManager) { | ||
this.beanManager = beanManager; | ||
} | ||
|
||
/** | ||
* Default constructor required by CDI. | ||
*/ | ||
public JobProcessorDiscovery() { | ||
this.beanManager = null; | ||
} | ||
|
||
/** | ||
* Discovers all classes that implement the JobProcessor interface using CDI. Does not create | ||
* instances, only finds the classes. | ||
* | ||
* @return A list of classes that implement the JobProcessor interface. | ||
*/ | ||
public List<Class<? extends JobProcessor>> discoverJobProcessors() { | ||
|
||
List<Class<? extends JobProcessor>> processors = new ArrayList<>(); | ||
|
||
try { | ||
Set<Bean<?>> beans = beanManager.getBeans(JobProcessor.class, Any.Literal.INSTANCE); | ||
|
||
for (Bean<?> bean : beans) { | ||
Class<?> beanClass = bean.getBeanClass(); | ||
|
||
if (JobProcessor.class.isAssignableFrom(beanClass)) { | ||
|
||
// Validate that the bean is in the correct scope | ||
validateScope(bean); | ||
|
||
processors.add((Class<? extends JobProcessor>) beanClass); | ||
Logger.debug(this, "Discovered JobProcessor: " + beanClass.getName()); | ||
} | ||
} | ||
} catch (Exception e) { | ||
var errorMessage = "Error discovering JobProcessors"; | ||
Logger.error(this, errorMessage, e); | ||
throw new DotRuntimeException(errorMessage, e); | ||
} | ||
|
||
if (processors.isEmpty()) { | ||
Logger.warn(this, "No JobProcessors were discovered"); | ||
} | ||
|
||
return processors; | ||
} | ||
|
||
/** | ||
* Validates that the scope of the bean is correct for a JobProcessor. | ||
* | ||
* @param bean The bean to validate. | ||
*/ | ||
private void validateScope(Bean<?> bean) { | ||
Class<?> scope = bean.getScope(); | ||
if (scope != Dependent.class) { | ||
throw new DotRuntimeException( | ||
"JobProcessor " + bean.getBeanClass().getName() + | ||
" must use @Dependent scope, found: " + scope.getName()); | ||
} | ||
} | ||
|
||
} |
34 changes: 23 additions & 11 deletions
34
dotCMS/src/main/java/com/dotcms/jobs/business/api/JobProcessorFactory.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 |
---|---|---|
@@ -1,32 +1,44 @@ | ||
package com.dotcms.jobs.business.api; | ||
|
||
import com.dotcms.cdi.CDIUtils; | ||
import com.dotcms.jobs.business.error.JobProcessorInstantiationException; | ||
import com.dotcms.jobs.business.processor.JobProcessor; | ||
import com.dotmarketing.util.Logger; | ||
import java.util.Optional; | ||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
@ApplicationScoped | ||
public class JobProcessorFactory { | ||
|
||
public JobProcessorFactory() { | ||
// Default constructor for CDI | ||
} | ||
|
||
/** | ||
* Creates a new instance of the specified job processor class. | ||
* First attempts to get the processor from CDI context, falls back to direct instantiation if that fails. | ||
* | ||
* @param processorClass The class of the job processor to create. | ||
* @return An optional containing the new job processor instance, or an empty optional if the | ||
* processor could not be created. | ||
* @return A new job processor instance | ||
* @throws JobProcessorInstantiationException if creation fails through both methods | ||
*/ | ||
JobProcessor newInstance( | ||
Class<? extends JobProcessor> processorClass) { | ||
JobProcessor newInstance(Class<? extends JobProcessor> processorClass) { | ||
|
||
Optional<? extends JobProcessor> cdiInstance = CDIUtils.getBean(processorClass); | ||
|
||
if (cdiInstance.isPresent()) { | ||
return cdiInstance.get(); | ||
} | ||
|
||
// If CDI fails, try direct instantiation | ||
return createInstance(processorClass); | ||
} | ||
|
||
/** | ||
* Creates a new instance using reflection. | ||
*/ | ||
private JobProcessor createInstance(Class<? extends JobProcessor> processorClass) { | ||
try { | ||
return processorClass.getDeclaredConstructor().newInstance(); | ||
} catch (Exception e) { | ||
Logger.error(this, "Error creating job processor", e); | ||
Logger.error(this, "Error creating job processor via reflection", e); | ||
throw new JobProcessorInstantiationException(processorClass, e); | ||
} | ||
} | ||
|
||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
dotCMS/src/main/java/com/dotcms/jobs/business/api/JobProcessorScanner.java
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.