0

The requirement is accept a large file <1GB, save it to the DB, return the newly generated ID to be displayed in GUI & kick off the file processing as a child process / async process.

This question partially answers my query best way of reading large files spring boot.

However how to kick off the Child Thread to start the processing of the heavy file? Using CompletableFuture? (I just read about it - not sure how to use it though - https://www.baeldung.com/java-completablefuture) After the future is completed then the same record is updated in the DB.

Arijit Datta
  • 101
  • 1
  • 2
  • 10
  • Do you need some result on parent thread after file processing file on child thread? – Boris Jan 16 '22 at 16:21
  • @Boris No not really. We want to shift the processing of the file to a child thread so that the UX does not get degraded. After we show the "Request ID", the "Create New Request" UC is complete. The user can at a later point of time go to the request overview page and check the status of the processing of the document. This status is updated after the child thread finishes processing the file. – Arijit Datta Jan 17 '22 at 12:18

1 Answers1

0

I hope I understood everything about request correctly, so I believe that you can do that by calling method that is annotated with @Async. Be aware, that this annotation needs proxy to work as planned, which means that you can not call it as method from same class.

For example, this will not work:

@Service
class T1 {

    public void method1()
    {
        method2();
    }

    @Async
    public void method2() 
    {
        // some code
    }
}

You will need to call it like this:

@Service
class T1 {

    private final T2 t2;
    
    public T1 (T2 t2)
    {
        this.t2 = t2;
    }
    
    public void method1()
    {
        t2.method2();
    }

}

@Service
class T2
{
    @Async
    public void method2() 
    {
        // some code
    }
}

or using self reference:

@Service
class T1 {

    private T1 self;
    
    public void method1()
    {
        self.method2();
    }

    @Async
    public void method2() 
    {
        // some code
    }
    
    
    @Autowired
    public void setSelf (T1 t1)
    {
        this.self = t1;
    }
}

Also, I think you will need to put @EnableAsync on some configuration class or for example class that holds main method.

Boris
  • 726
  • 1
  • 10
  • 22