0

この例に従いました: 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 クラスでアクセスする正しい方法は何ですか?

4

1 に答える 1

1

誰かが興味を持っている場合:

レストコントローラ:

@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;
}
}
于 2016-06-28T07:22:52.673 に答える