0

I followed this example: https://github.com/camunda/camunda-bpm-spring-boot-starter/tree/master/examples/camunda-spring-boot-starter-example-simple

when i access the taskService in this class:

@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;
}

its working fine. but how i would access the TaskService in a @RestController class?

i tried like this:

@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: ";
}
}

but my taskService is always null. What is the right way to access it in RestController class?

Hans
  • 119
  • 1
  • 12

1 Answers1

1

if someone interested:

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!";
}
}

access to 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;
}
}
Hans
  • 119
  • 1
  • 12