-
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.
- Loading branch information
1 parent
98f280e
commit 37c6345
Showing
8 changed files
with
665 additions
and
6 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
dotCMS/src/main/java/com/dotcms/jobs/business/api/JobProcessorScanner.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,54 @@ | ||
package com.dotcms.jobs.business.api; | ||
|
||
import com.dotcms.jobs.business.processor.JobProcessor; | ||
import com.dotmarketing.util.Logger; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.Index; | ||
import org.jboss.jandex.IndexReader; | ||
|
||
@ApplicationScoped | ||
public class JobProcessorScanner { | ||
|
||
|
||
public List<Class<? extends JobProcessor>> discoverJobProcessors() { | ||
List<Class<? extends JobProcessor>> jobProcessors = new ArrayList<>(); | ||
try { | ||
|
||
Index index = getJandexIndex(); | ||
DotName jobProcessorInterface = DotName.createSimple(JobProcessor.class.getName()); | ||
|
||
Collection<ClassInfo> implementors = index.getAllKnownImplementors(jobProcessorInterface); | ||
|
||
for (ClassInfo classInfo : implementors) { | ||
String className = classInfo.name().toString(); | ||
|
||
Class<?> clazz = Class.forName(className); | ||
if (JobProcessor.class.isAssignableFrom(clazz)) { | ||
jobProcessors.add((Class<? extends JobProcessor>) clazz); | ||
} | ||
} | ||
|
||
} catch (IOException | ClassNotFoundException e) { | ||
Logger.error(JobProcessorScanner.class, "Error discovering JobProcessors", e); | ||
|
||
} | ||
return jobProcessors; | ||
} | ||
|
||
private Index getJandexIndex() throws IOException { | ||
InputStream input = getClass().getClassLoader().getResourceAsStream("META-INF/jandex.idx"); | ||
if (input == null) { | ||
throw new IOException("Jandex index not found"); | ||
} | ||
IndexReader reader = new IndexReader(input); | ||
return reader.read(); | ||
} | ||
|
||
} |
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
14 changes: 14 additions & 0 deletions
14
dotCMS/src/main/java/com/dotcms/jobs/business/processor/Queue.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,14 @@ | ||
package com.dotcms.jobs.business.processor; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
public @interface Queue { | ||
String value(); | ||
} |
76 changes: 76 additions & 0 deletions
76
dotCMS/src/main/java/com/dotcms/jobs/business/processor/impl/FileReaderJob.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,76 @@ | ||
package com.dotcms.jobs.business.processor.impl; | ||
|
||
import com.dotcms.jobs.business.error.JobCancellationException; | ||
import com.dotcms.jobs.business.job.Job; | ||
import com.dotcms.jobs.business.processor.Cancellable; | ||
import com.dotcms.jobs.business.processor.JobProcessor; | ||
import com.dotcms.jobs.business.processor.Queue; | ||
import com.dotmarketing.util.Logger; | ||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
@Queue("FileReader") | ||
public class FileReaderJob implements JobProcessor, Cancellable { | ||
|
||
boolean working = true; | ||
|
||
@Override | ||
public void process(Job job) { | ||
// Retrieve job parameters | ||
Logger.info(this.getClass(), "Processing job: " + job.id()); | ||
Map<String, Object> params = job.parameters(); | ||
String filePath = (String) params.get("filePath"); | ||
final Object nLinesRaw = params.get("nLines"); | ||
if(!(nLinesRaw instanceof String)) { | ||
Logger.error(this.getClass(), "Parameter 'nLines' is required."); | ||
return; | ||
} | ||
int nLines = Integer.parseInt((String) nLinesRaw); | ||
// Validate required parameters | ||
if (filePath == null || nLines <= 0) { | ||
Logger.error(this.getClass(), "Parameters 'filePath' and 'nLines' (greater than zero) are required."); | ||
return; | ||
} | ||
|
||
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { | ||
String line; | ||
int lineCount = 0; | ||
int totalLines = 0; | ||
|
||
Logger.info(this.getClass(), "Starting to read the file: " + filePath); | ||
|
||
while (working && (line = reader.readLine()) != null) { | ||
lineCount++; | ||
totalLines++; | ||
|
||
// Print the line when the counter reaches nLines | ||
if (lineCount == nLines) { | ||
Logger.info(this.getClass(), "Line " + totalLines + ": " + line); | ||
lineCount = 0; // Reset the counter | ||
} | ||
Thread.sleep(1000); // Simulate processing time | ||
} | ||
|
||
Logger.info(this.getClass(), "Reading completed. Total lines read: " + totalLines); | ||
|
||
} catch (IOException e) { | ||
Logger.error(this.getClass(), "Error reading the file: " + e.getMessage()); | ||
} catch (Exception e) { | ||
Logger.error(this.getClass(), "Unexpected error during processing: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getResultMetadata(Job job) { | ||
return Map.of(); | ||
} | ||
|
||
|
||
@Override | ||
public void cancel(Job job) throws JobCancellationException { | ||
Logger.info(this.getClass(), "Job cancelled: " + job.id()); | ||
working = false; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
dotCMS/src/main/java/com/dotcms/rest/api/v1/job/JobParams.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,58 @@ | ||
package com.dotcms.rest.api.v1.job; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
import org.glassfish.jersey.media.multipart.FormDataContentDisposition; | ||
import org.glassfish.jersey.media.multipart.FormDataParam; | ||
|
||
public class JobParams { | ||
@FormDataParam("file") | ||
private InputStream fileInputStream; | ||
|
||
@FormDataParam("file") | ||
private FormDataContentDisposition contentDisposition; | ||
|
||
@FormDataParam("params") | ||
private String jsonParams; | ||
|
||
@FormDataParam("params") | ||
private Map params; | ||
|
||
public InputStream getFileInputStream() { | ||
return fileInputStream; | ||
} | ||
|
||
public void setFileInputStream(InputStream fileInputStream) { | ||
this.fileInputStream = fileInputStream; | ||
} | ||
|
||
public FormDataContentDisposition getContentDisposition() { | ||
return contentDisposition; | ||
} | ||
|
||
public void setContentDisposition(FormDataContentDisposition contentDisposition) { | ||
this.contentDisposition = contentDisposition; | ||
} | ||
|
||
public void setJsonParams(String jsonParams) { | ||
this.jsonParams = jsonParams; | ||
} | ||
|
||
public Map getParams() throws JsonProcessingException { | ||
if (null == params) { | ||
params = new ObjectMapper().readValue(jsonParams, Map.class); | ||
} | ||
return params; | ||
} | ||
|
||
public void setParams(Map<Object, Object> params) { | ||
this.params = params; | ||
} | ||
|
||
public String getJsonParams() { | ||
return jsonParams; | ||
} | ||
|
||
} |
Oops, something went wrong.