Minor refactoring

This commit is contained in:
ViacheslavKlimov 2025-05-05 10:50:39 +03:00
parent ac9e738018
commit 5993b8b963
2 changed files with 25 additions and 1 deletions

View File

@ -15,6 +15,7 @@
*/
package org.thingsboard.server.common.msg.queue;
import com.google.common.util.concurrent.SettableFuture;
import org.thingsboard.server.common.data.id.EntityId;
import java.util.UUID;
@ -42,4 +43,18 @@ public interface TbCallback {
void onFailure(Throwable t);
static <V> TbCallback wrap(SettableFuture<V> future) {
return new TbCallback() {
@Override
public void onSuccess() {
future.set(null);
}
@Override
public void onFailure(Throwable t) {
future.setException(t);
}
};
}
}

View File

@ -183,6 +183,15 @@ public abstract class TaskProcessor<T extends Task<R>, R extends TaskResult> {
discardedJobs.add(jobId);
}
protected <V> V wait(Future<V> future) throws Exception {
try {
return future.get(); // will be interrupted after task processing timeout
} catch (InterruptedException e) {
future.cancel(true); // interrupting the underlying task
throw e;
}
}
@PreDestroy
public void destroy() {
taskConsumer.stop();