SpringBoot Camunda Starter访问RestController

时间:2016-06-24 12:13:41

标签: spring-boot camunda taskservice

我按照这个例子:https://github.com/camunda/camunda-bpm-spring-boot-starter/tree/master/examples/camunda-spring-boot-starter-example-simple

当我访问此类中的taskService时:

@Component
public class TaskHelper {

private final Logger logger = getLogger(this.getClass());

@Autowired
private RuntimeService runtimeService;

@Autowired
private TaskService taskService;

private String processInstanceId;

@EventListener
public void notify(final ContextRefreshedEvent unused) {
    processInstanceId = runtimeService.startProcessInstanceByKey("Sample").getProcessInstanceId();
    logger.info("started instance: {}", processInstanceId);

    List<Task> task = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
    logger.info("completed task: {}", task.get(0).getName());
    taskService.complete(task.get(0).getId());
    logger.info("completed task: {}", task);

    // now jobExecutor should execute the async job
}

public String getProcessInstanceId() {
    return processInstanceId;
}

工作正常。但我如何在@RestController类中访问TaskService?

我试过这样:

@RestController
public class TaskController {

@Autowired
TaskService taskService;

@RequestMapping(value = "/tasklist")
public String taskList(@RequestParam(value = "name", defaultValue = "all") String name) {
    taskService.createTaskQuery().list();
    return "Task: ";
}
}

但我的taskService始终为null。在RestController类中访问它的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如果有人感兴趣:

RestController:

@RestController
public class TaskController {
@Autowired
TaskHelper taskHelper;

@RequestMapping(value = "/tasklist")
public String taskList(@RequestParam(value = "name", defaultValue = "all") String name) {
    taskHelper.getTasks();
    return "Tasklist ausgelesen.";
}

@RequestMapping(value = "/startTask")
public String startTask(@RequestParam(value = "name", defaultValue = "all") String name) {
    taskHelper.startTask();

    return "Task gestartet!";
}
}

访问TaskService:

@Component
public class TaskHelper {

private final Logger logger = getLogger(this.getClass());

@Autowired
private RuntimeService runtimeService;

@Autowired
private TaskService taskService;

private String processInstanceId;

public void getTasks() {
    processInstanceId = runtimeService.startProcessInstanceByKey("Sample").getProcessInstanceId();
    logger.info("started instance: {}", processInstanceId);

    List<Task> task = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
    logger.info("anzahl offener Tasks: {}", task.size());
    for (Task task2 : task) {
        logger.info("Name: {}", task2.getName());
    }
    taskService.complete(task.get(0).getId());
    logger.info("Task abgeschlossen: {}(ID:{})", task.get(0).getName(), task.get(0).getId());
}

public void startTask() {
    processInstanceId = runtimeService.startProcessInstanceByKey("Sample").getProcessInstanceId();
    logger.info("started instance: {}", processInstanceId);
    List<Task> task = taskService.createTaskQuery().list();
    for (Task task2 : task) {
        logger.info("Name: {}", task2.getName());
    }
}

public String getProcessInstanceId() {
    return processInstanceId;
}
}