Vivek Aditya
Vivek Aditya

Reputation: 1173

Store the Disposable when subscribed to a Mono in database

I am currently exploring the reactor-core. I am able to subscribe to a Mono and also save the disposable to handle the execution of the subscriber.

Mono<Object> task1 = Mono.just("Task1");
Mono<Object> task2 = Mono.just("Task2");
System.out.println(System.currentTimeMillis());
HashMap<Mono,Disposable> taskHandlerMap = new HashMap<>();
Disposable task1Handler = task1.delayElement(Duration.ofSeconds(5)).subscribe(
(Object x)->{
        System.out.println(x.toString() + "\t" + System.currentTimeMillis());
});
taskHandlerMap.put(task1,task1Handler);
Disposable task2Handler = task2.delayElement(Duration.ofSeconds(10)).subscribe(
(Object x)->{
        System.out.println(x.toString() + "\t" + System.currentTimeMillis());
});
taskHandlerMap.put(task2,task2Handler);

I am able to dispose a scheduled task by using the handler map as follows.

taskHandlerMap.get(task2).dispose();

Can I save this taskHandlerMap in a database? Is yes, how can I save it. Else is there a better way to store the disposable created at subscription.

Upvotes: 2

Views: 3036

Answers (1)

Simon Basl&#233;
Simon Basl&#233;

Reputation: 28341

as @akarnokd points out, a Disposable is merely a reference to a live computation, and it makes no sense to try and save it and restore it to/from a database.

It is kind of like if you submitted an long HTTP call to an ExecutorService and expected to be able to save the Future to the DB and somehow restore it... If the application stops, the HTTP call is gone anyway. If the application keeps on running, there's no need to keep the Future in a DB (you'd keep it in memory, with the same lifespan as the HTTP call).

Upvotes: 3

Related Questions