Is there any ways which can store the object references that can be used by multiple instances ?
I have an API in which it has an in-memory mapping named mapping
where the object stored inside (CompletableFuture<Student>
) will be used by another thread at some time later.
It works fine when there is only one instance. But if there are multiple instances of the same API, each instance will have its own variable of mapping
. However, I would like to have only one mapping
which is shared across all instances. As far as I know, if using redis, the returned object is the copy of the original object, but I would want to store the object reference to the CompletableFuture<Student>
object, so that I could use it later (to call the complete
method in the CompletableFuture
). In theory, is it possible ?
@RestControllerpublic class StudentAPI { private Map<Long, CompletableFuture<Student>> mapping = new ConcurrentHashMap<>(); private AtomicLong id = new AtomicLong(); @PostMapping("/student") public CompletableFuture<Student> createStudent(@RequestBody Student student) { CompletableFuture<Student> cf = new CompletableFuture<>(); mapping.put(id.addAndGet(1), cf); invokeLegacyAPI(id, student); return cf; } private void invokeLegacyAPI(AtomicLong id, Student student) { //invoking legacy API }}