feat: 完成简单的任务执行
This commit is contained in:
@@ -7,6 +7,8 @@ import com.lanyuanxiaoyao.service.template.controller.SimpleControllerSupport;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.function.Function;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -20,8 +22,11 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
@RequestMapping("task")
|
||||
public class TaskController extends SimpleControllerSupport<Task, Void, TaskController.ListItem, TaskController.DetailItem> {
|
||||
private final TaskService taskService;
|
||||
|
||||
public TaskController(TaskService service) {
|
||||
super(service);
|
||||
this.taskService = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -29,6 +34,12 @@ public class TaskController extends SimpleControllerSupport<Task, Void, TaskCont
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@PostMapping("execute")
|
||||
public GlobalResponse<Object> execute(@RequestBody ExecuteRequest request) {
|
||||
taskService.execute(request.templateId());
|
||||
return GlobalResponse.responseSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Void, Task> saveItemMapper() {
|
||||
throw new UnsupportedOperationException();
|
||||
@@ -81,4 +92,7 @@ public class TaskController extends SimpleControllerSupport<Task, Void, TaskCont
|
||||
LocalDateTime finishedTime
|
||||
) {
|
||||
}
|
||||
|
||||
public record ExecuteRequest(Long templateId) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ public class TaskTemplateController extends SimpleControllerSupport<TaskTemplate
|
||||
return switch (item.type) {
|
||||
case CLASS -> {
|
||||
var template = new ClassTaskTemplate();
|
||||
template.setId(item.id());
|
||||
template.setName(item.name());
|
||||
template.setDescription(item.description());
|
||||
template.setClazz(item.clazz());
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
package com.lanyuanxiaoyao.leopard.server.service;
|
||||
|
||||
import com.lanyuanxiaoyao.leopard.server.entity.Task;
|
||||
import com.lanyuanxiaoyao.leopard.server.entity.templates.ClassTaskTemplate;
|
||||
import com.lanyuanxiaoyao.leopard.server.repository.TaskRepository;
|
||||
import com.lanyuanxiaoyao.leopard.server.service.task.TaskRunner;
|
||||
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@@ -13,7 +19,26 @@ import org.springframework.stereotype.Service;
|
||||
@Slf4j
|
||||
@Service
|
||||
public class TaskService extends SimpleServiceSupport<Task> {
|
||||
public TaskService(TaskRepository repository) {
|
||||
private final ApplicationContext context;
|
||||
private final TaskTemplateService taskTemplateService;
|
||||
private final ExecutorService executors = Executors.newFixedThreadPool(50);
|
||||
|
||||
public TaskService(TaskRepository repository, ApplicationContext context, TaskTemplateService taskTemplateService) {
|
||||
super(repository);
|
||||
this.context = context;
|
||||
this.taskTemplateService = taskTemplateService;
|
||||
}
|
||||
|
||||
public void execute(Long templateId) {
|
||||
var template = taskTemplateService.detail(templateId);
|
||||
switch (template.getType()) {
|
||||
case CLASS -> {
|
||||
ClassTaskTemplate classTaskTemplate = (ClassTaskTemplate) template;
|
||||
var runner = context.getBean(classTaskTemplate.getClazz(), TaskRunner.class);
|
||||
executors.submit(() -> {
|
||||
runner.runTask(classTaskTemplate, Map.of());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Component("com.lanyuanxiaoyao.leopard.server.service.task.UpdateStockInformationTask")
|
||||
public class UpdateStockInformationTask extends TaskRunner {
|
||||
private final StockService stockService;
|
||||
private final TuShareService tuShareService;
|
||||
|
||||
Reference in New Issue
Block a user