in general you can do it using TaskService and TaskQuery, as follows:
List<Task> tasks = taskService.createTaskQuery(). processInstanceId(processInstance.getId()).activityName("activityName").list();
You can use as well orderByDesc or Asc to sort the result.
sorry, I gave you wrong examples. It should be:
List<HistoryTask> history = historyService.createHistoryTaskQuery().executionId(processInstance.getId()).list();
Unfortunately there is no way to narrow down the search by activity name but you can either use task id or outcome.
In the worst case you will get all completed task for execution and the filter it manually.
I went with a work-around:
I made transitions to the tasks match tasks names: if a task is called 'task-1', then transition is 'to-task-1'. Holding the pattern, i can do:
where activityName is 'task-1'
Thanks again for help!
Found a better way to get a history task via HistoryActivityInstance:
HistoryActivityInstance hai = historyService.createHistoryActivityInstanceQuery().processInstanceId(pid).activityName(activityName).uniqueResult();
if (hai.getClass() == HistoryTaskInstanceImpl.class)
HistoryTask ht = ((HistoryTask)hai).getHistoryTask();