Closes #2229 load correct constructor to init TaskanaJob
instead of using always the first constructor, load the constructor with correct parameters
This commit is contained in:
parent
82b572b1ef
commit
0caff9d327
|
@ -1,12 +1,14 @@
|
|||
package acceptance.jobs;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -18,9 +20,11 @@ import pro.taskana.classification.internal.jobs.ClassificationChangedJob;
|
|||
import pro.taskana.common.api.JobService;
|
||||
import pro.taskana.common.api.ScheduledJob;
|
||||
import pro.taskana.common.api.TaskanaEngine;
|
||||
import pro.taskana.common.api.exceptions.TaskanaException;
|
||||
import pro.taskana.common.internal.JobMapper;
|
||||
import pro.taskana.common.internal.jobs.AbstractTaskanaJob;
|
||||
import pro.taskana.common.internal.jobs.JobRunner;
|
||||
import pro.taskana.common.internal.transaction.TaskanaTransactionProvider;
|
||||
import pro.taskana.task.internal.jobs.TaskCleanupJob;
|
||||
import pro.taskana.task.internal.jobs.TaskRefreshJob;
|
||||
import pro.taskana.testapi.TaskanaConfigurationModifier;
|
||||
|
@ -126,4 +130,46 @@ class AbstractTaskanaJobAccTest {
|
|||
assertThat(nextJobs).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_CreateSampleTaskanaJob_When_JobHasMoreThenOneConstructor() {
|
||||
|
||||
ScheduledJob scheduledJob = new ScheduledJob();
|
||||
scheduledJob.setType(SampleTaskanaJob.class.getName());
|
||||
|
||||
ThrowingCallable call =
|
||||
() -> AbstractTaskanaJob.createFromScheduledJob(taskanaEngine, null, scheduledJob);
|
||||
|
||||
assertThatCode(call).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
public static class SampleTaskanaJob extends AbstractTaskanaJob {
|
||||
|
||||
public SampleTaskanaJob() {
|
||||
super(null, null, null, true);
|
||||
}
|
||||
|
||||
public SampleTaskanaJob(
|
||||
TaskanaEngine taskanaEngine,
|
||||
TaskanaTransactionProvider txProvider,
|
||||
ScheduledJob scheduledJob) {
|
||||
super(taskanaEngine, txProvider, scheduledJob, true);
|
||||
}
|
||||
|
||||
public SampleTaskanaJob(
|
||||
TaskanaEngine taskanaEngine,
|
||||
TaskanaTransactionProvider txProvider,
|
||||
ScheduledJob job,
|
||||
boolean async) {
|
||||
super(taskanaEngine, txProvider, job, async);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getType() {
|
||||
return SampleTaskanaJob.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void execute() throws TaskanaException {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,18 +36,17 @@ public abstract class AbstractTaskanaJob implements TaskanaJob {
|
|||
}
|
||||
|
||||
public static TaskanaJob createFromScheduledJob(
|
||||
TaskanaEngine engine, TaskanaTransactionProvider txProvider, ScheduledJob job)
|
||||
throws ClassNotFoundException,
|
||||
IllegalAccessException,
|
||||
InstantiationException,
|
||||
InvocationTargetException {
|
||||
TaskanaEngine engine, TaskanaTransactionProvider txProvider, ScheduledJob job) {
|
||||
|
||||
return (TaskanaJob)
|
||||
Thread.currentThread()
|
||||
.getContextClassLoader()
|
||||
.loadClass(job.getType())
|
||||
.getConstructors()[0]
|
||||
.newInstance(engine, txProvider, job);
|
||||
Class<?> jobClass;
|
||||
try {
|
||||
jobClass = Thread.currentThread().getContextClassLoader().loadClass(job.getType());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new SystemException(
|
||||
String.format("Can't load class '%s'", job.getType()));
|
||||
}
|
||||
|
||||
return initTaskanaJob(engine, jobClass, txProvider, job);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -67,6 +66,21 @@ public abstract class AbstractTaskanaJob implements TaskanaJob {
|
|||
* @throws SystemException if the jobClass could not be scheduled.
|
||||
*/
|
||||
public static void initializeSchedule(TaskanaEngine taskanaEngine, Class<?> jobClass) {
|
||||
AbstractTaskanaJob job = initTaskanaJob(taskanaEngine, jobClass, null, null);
|
||||
if (!job.async) {
|
||||
throw new SystemException(
|
||||
String.format("Job '%s' is not an async job. Please declare it as async", jobClass));
|
||||
}
|
||||
JobServiceImpl jobService = (JobServiceImpl) taskanaEngine.getJobService();
|
||||
jobService.deleteJobs(job.getType());
|
||||
job.scheduleNextJob();
|
||||
}
|
||||
|
||||
private static AbstractTaskanaJob initTaskanaJob(
|
||||
TaskanaEngine taskanaEngine,
|
||||
Class<?> jobClass,
|
||||
TaskanaTransactionProvider txProvider,
|
||||
ScheduledJob scheduledJob) {
|
||||
if (!AbstractTaskanaJob.class.isAssignableFrom(jobClass)) {
|
||||
throw new SystemException(
|
||||
String.format("Job '%s' is not a subclass of '%s'", jobClass, AbstractTaskanaJob.class));
|
||||
|
@ -84,7 +98,7 @@ public abstract class AbstractTaskanaJob implements TaskanaJob {
|
|||
}
|
||||
AbstractTaskanaJob job;
|
||||
try {
|
||||
job = (AbstractTaskanaJob) constructor.newInstance(taskanaEngine, null, null);
|
||||
job = (AbstractTaskanaJob) constructor.newInstance(taskanaEngine, txProvider, scheduledJob);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new SystemException(
|
||||
String.format(
|
||||
|
@ -104,13 +118,7 @@ public abstract class AbstractTaskanaJob implements TaskanaJob {
|
|||
TaskanaEngine.class, TaskanaTransactionProvider.class, ScheduledJob.class, jobClass),
|
||||
e);
|
||||
}
|
||||
if (!job.async) {
|
||||
throw new SystemException(
|
||||
String.format("Job '%s' is not an async job. Please declare it as async", jobClass));
|
||||
}
|
||||
JobServiceImpl jobService = (JobServiceImpl) taskanaEngine.getJobService();
|
||||
jobService.deleteJobs(job.getType());
|
||||
job.scheduleNextJob();
|
||||
return job;
|
||||
}
|
||||
|
||||
public boolean isAsync() {
|
||||
|
|
Loading…
Reference in New Issue