Make JobExecutor execute jobs asynchronously

This commit is contained in:
AsamK 2023-11-12 11:51:38 +01:00
parent eaa6b7cf57
commit ed11bf6368
5 changed files with 31 additions and 3 deletions

View file

@ -1,6 +1,7 @@
package org.asamk.signal.manager.api;
import java.util.Arrays;
import java.util.Base64;
public class StickerPackId {
@ -32,4 +33,9 @@ public class StickerPackId {
public int hashCode() {
return Arrays.hashCode(id);
}
@Override
public String toString() {
return "StickerPackId{" + Base64.getUrlEncoder().encodeToString(id) + '}';
}
}

View file

@ -9,7 +9,7 @@ import org.asamk.signal.manager.storage.stickerPacks.StickerPackStore;
import java.util.function.Supplier;
public class Context {
public class Context implements AutoCloseable {
private final Object LOCK = new Object();
@ -170,6 +170,11 @@ public class Context {
}
}
@Override
public void close() {
jobExecutor.close();
}
private interface Callable {
void call();

View file

@ -2,16 +2,31 @@ package org.asamk.signal.manager.internal;
import org.asamk.signal.manager.helper.Context;
import org.asamk.signal.manager.jobs.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JobExecutor {
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class JobExecutor implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(JobExecutor.class);
private final Context context;
private final ExecutorService executorService;
public JobExecutor(final Context context) {
this.context = context;
this.executorService = Executors.newCachedThreadPool();
}
public void enqueueJob(Job job) {
job.run(context);
logger.debug("Enqueuing {} job", job.getClass().getSimpleName());
executorService.execute(() -> job.run(context));
}
@Override
public void close() {
executorService.close();
}
}

View file

@ -1332,6 +1332,7 @@ public class ManagerImpl implements Manager {
stopReceiveThread(thread);
}
executor.close();
context.close();
dependencies.getSignalWebSocket().disconnect();
dependencies.getPushServiceSocket().close();

View file

@ -23,6 +23,7 @@ public class RetrieveStickerPackJob implements Job {
@Override
public void run(Context context) {
logger.trace("Downloading sticker pack {}", packId);
try {
context.getStickerHelper().retrieveStickerPack(packId, packKey);
} catch (IOException e) {