forked from comixed/comixed
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WorkerTaskAdaptor.java
133 lines (117 loc) · 4.29 KB
/
WorkerTaskAdaptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/
package org.comixedproject.task.adaptors;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import lombok.extern.log4j.Log4j2;
import org.comixedproject.model.tasks.Task;
import org.comixedproject.model.tasks.TaskType;
import org.comixedproject.service.task.TaskService;
import org.comixedproject.task.TaskException;
import org.comixedproject.task.encoders.WorkerTaskEncoder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
/**
* <code>TaskAdadptor</code> provides methods for fetching {@link WorkerTaskEncoder} objects for
* encoding and decoding persisted {@link Task} objects.
*
* @author Darryl L. Pierce
*/
@Service
@EnableConfigurationProperties
@PropertySource("classpath:task-adaptors.properties")
@ConfigurationProperties(prefix = "task", ignoreUnknownFields = false)
@Log4j2
public class WorkerTaskAdaptor implements InitializingBean {
Map<TaskType, String> adaptorMap = new EnumMap<>(TaskType.class);
@Autowired private ApplicationContext applicationContext;
@Autowired private TaskService taskService;
private List<TaskTypeEntry> adaptors = new ArrayList<>();
public List<TaskTypeEntry> getAdaptors() {
return adaptors;
}
/**
* Returns the next task to be executed.
*
* @return the task
*/
public List<Task> getNextTask() {
log.debug("Getting the next task to run");
return this.taskService.getTasksToRun(1);
}
@Override
public void afterPropertiesSet() throws Exception {
log.debug("Loading task action configuration");
this.adaptorMap.clear();
for (TaskTypeEntry entry : this.adaptors) {
if (this.adaptorMap.containsKey(entry.getType())) {
throw new TaskException("Configuration already set for type: " + entry.getType());
} else {
if (!this.applicationContext.containsBean(entry.getName()))
throw new TaskException("Missing decoder bean: " + entry.getName());
this.adaptorMap.put(entry.getType(), entry.getName());
}
}
}
/**
* Returns the configured {@link WorkerTaskEncoder} for the given {@link TaskType}.
*
* @param taskType the task type
* @param <T> the task encoder type
* @return the adaptor bean
* @throws TaskException if no adaptor is configured
*/
public <T extends WorkerTaskEncoder> T getEncoder(final TaskType taskType) throws TaskException {
log.debug("Getting task adaptor: type={}", taskType);
if (!this.adaptorMap.containsKey(taskType))
throw new TaskException("No adaptor configured: " + taskType);
return (T) this.applicationContext.getBean(this.adaptorMap.get(taskType));
}
/**
* Returns the total number of tasks running and in the database.
*
* @return the task count
*/
public long getTaskCount() {
log.debug("Getting the task count");
return this.taskService.getTaskCount();
}
public static class TaskTypeEntry {
private TaskType type;
private String name;
public TaskType getType() {
return type;
}
public void setType(final TaskType type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
}