Skip to content

Commit 48860d9

Browse files
committed
wip
1 parent 2cc027e commit 48860d9

4 files changed

Lines changed: 84 additions & 1 deletion

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2021 DANS - Data Archiving and Networked Services (info@dans.knaw.nl)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.knaw.dans.lib.util.pollingtaskexec;
17+
18+
import lombok.RequiredArgsConstructor;
19+
20+
@RequiredArgsConstructor
21+
public class ExecutorServiceTaskScheduler implements TaskScheduler {
22+
private final java.util.concurrent.ExecutorService executorService;
23+
24+
@Override
25+
public void schedule(Runnable task) {
26+
executorService.submit(task);
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) 2021 DANS - Data Archiving and Networked Services (info@dans.knaw.nl)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.knaw.dans.lib.util.pollingtaskexec;
17+
18+
public class ImmediateTaskScheduler implements TaskScheduler {
19+
@Override
20+
public void schedule(Runnable task) {
21+
task.run();
22+
}
23+
}

src/main/java/nl/knaw/dans/lib/util/pollingtaskexec/PollingTaskExecutor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ public class PollingTaskExecutor<R> implements Managed {
4040
private final Duration pollingInterval;
4141
private final TaskSource<R> taskSource;
4242
private final TaskFactory<R> taskFactory;
43+
private final TaskScheduler taskScheduler;
4344

4445
private ScheduledFuture<?> future;
4546

47+
public PollingTaskExecutor(String name, ScheduledExecutorService scheduler, Duration pollingInterval, TaskSource<R> taskSource, TaskFactory<R> taskFactory) {
48+
this(name, scheduler, pollingInterval, taskSource, taskFactory, new ImmediateTaskScheduler());
49+
}
50+
4651
/**
4752
* Copy constructor. The source executor must not be running. The purpose of this constructor is only to be able to wrap a PollingTaskExecutor in a UnitOfWorkAwareProxy. In general, no copies
4853
* should be created of a PollingTaskExecutor, and in particular should the schedular not be shared among PollingTaskExecutors.
@@ -59,6 +64,7 @@ public PollingTaskExecutor(PollingTaskExecutor<R> other) {
5964
this.pollingInterval = other.pollingInterval;
6065
this.taskSource = other.taskSource;
6166
this.taskFactory = other.taskFactory;
67+
this.taskScheduler = other.taskScheduler;
6268
}
6369

6470
@Override
@@ -86,7 +92,7 @@ public void tick() {
8692
}
8793
log.debug("{}: found next task record(s): {}", name, inputs);
8894
Runnable task = taskFactory.create(inputs);
89-
task.run();
95+
taskScheduler.schedule(task);
9096
}
9197
catch (Exception e) {
9298
log.error("{}: error while polling or running task", name, e);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2021 DANS - Data Archiving and Networked Services (info@dans.knaw.nl)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.knaw.dans.lib.util.pollingtaskexec;
17+
18+
public interface TaskScheduler {
19+
20+
/**
21+
* Schedules the task for execution.
22+
*
23+
* @param task the task to schedule
24+
*/
25+
void schedule(Runnable task);
26+
}

0 commit comments

Comments
 (0)