0

Good afternoon, I have a question, if in my Spring Boot application a client makes a request, I make an insert of an entity in the database and then I wait for a field of the entity to have the value 'x' and return the result to the client. The field with the value 'x' is modified by another service, so I have to wait and consult this field every few seconds.

In other words, all this can take even minutes.

I have this done but I don't think it's the best it can be.

By the way I have looked at dozens of threads on this topic.

**

@PostMapping("/some/new")
    @PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
    public void newSome(@RequestBody Some some, HttpServletRequest req, Principal principal) throws InterruptedException, ExecutionException {
        Usuario user = usuarioRepository.findByUsuario(principal.getName());
        
        if(user != null) {
            
            some.setUsuario_id(user.getId());
            some.setRealization(false);
            Some someDB = someRepository.save(some); ----- SAVE ENTITY
            return TestGreile(someDB.getId()).get()); ----- RETURN UNTIL REACH FIELD isRealization true in the BD, this is changed by another service
        }
           
    }
    
    @Async
    public CompletableFuture<Boolean>TestGreile(long some_id) throws InterruptedException {
                        
        Some someTmp = (Some) peticionesRepository.findById(peticion_id);
        while(!someTmp.isRealization()) {
            entityManager.clear();
            someTmp = peticionesRepository.findById(some_id);
            Thread.sleep(3000);
            System.out.println("Iteración! --> " + someTmp);
        }
        
        return CompletableFuture.completedFuture(someTmp.isRealization());
    }

**

ProgSnt6
  • 21
  • 4
  • `TestGreile` will [not be called asynchronously](https://stackoverflow.com/questions/24898547/spring-async-method-called-from-another-async-method). In general, A) provide an endpoint to submit a request, and another endpoint to get the result (which might still be in progress); or B) use a queue/topic for clients to submit requests and listen for results; or C) combination of A and B. – Andrew S Oct 14 '20 at 16:02
  • You shouldn't block connections. Use the post mapping to start processing the request and return nothing to client (except errors), and another get mapping for the client to poll regularly and check if request has finished – cdalxndr Oct 14 '20 at 16:14
  • @cdalxndr But if customers want to make the request and wait until it is finished, wouldn't this idea be better? Because asking them to make inquiries every x time to see the result of the request I don't see it right ... PS: Thread.Sleep is a good idea? Because working works but I don't know if it is the best idea. Greetings and thank you – ProgSnt6 Oct 15 '20 at 07:49
  • This should be handled by frontend. The frontend will block and poll regularly until the request is finished. Customers will only see "request in progress" message. – cdalxndr Oct 15 '20 at 11:31
  • @cdalxndr I will comment on this to my superior, I hope he says yes, because if not ... it will be a terrible botch – ProgSnt6 Oct 15 '20 at 11:45

0 Answers0