How to use java API ExternalTaskService to query multiple topics

I would like to use Java API ExternalTaskService to query external tasks on multiple topics. I know that I need to build an ExternalTaskQuery object from createExternalTaskQuery call. ExternalTaskQuery has a topicName method. This would limit the query to a single topic. But I would like to be able to query multiple topics at once. How should I do this?

Can I call topicName multiple times? For example, can I do this:

ExternalTaskQuery externalTaskQuery = externalTaskService.createExternalTaskQuery()
topicName(“topic1”).topicName(“topic2”).topicName(“topic3”);

List tasks = externalTaskQuery.list();

Would the list contains all the external tasks of topic1, topic2 and topic3?

Same question goes with fetchAndLock API, can it be used to fetch multiple topics?

Any pointers would be greatly appreciated. Thanks.

Hi @carbon_60 and welcome back.

Actually for querying external tasks from multiple topics, you’ll have to do multiple calls via createExternalTaskQuery as that only allows one topic name for the query.
Calling the method topicName on the ExternalTaskQuery a second time, will overwrite the previously selected topic name.

For fetchAndLock this is however different, as external tasks from multiple topics can be fetched and locked. Here is an example for that:

List<LockedExternalTask> tasks = externalTaskService.fetchAndLock(10, "externalWorkerId")
    .topic("topic1", 10L * 1000L) // lockDuration 10 seconds
    .topic("topic2", 20L * 1000L)
    .execute();

This will result in locked tasks for topic1 and topic2.

Thank you so much, @rohwerj!