Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods annotated with '@Async' must be overridable

Intellij show red underline.
and When I mouseover to red underline, that show this message.

Methods annotated with '@Async' must be overridable

Reports the cases when your code prevents a class from being subclassed by some framework (e.g. Spring or Hibernate) at runtime

What I should do for remove this error?
And It show red underline. but It still working without compile error.

I'm using Intellij 2017.2.5.

@Async
private void deleteFile(String fileName, String path) {
    BasicAWSCredentials credentials = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);
    AmazonS3 s3client = AmazonS3ClientBuilder.standard().withRegion("ap-northeast-2").withCredentials(new AWSStaticCredentialsProvider(credentials)).build();

    try {
        s3client.deleteObject(new DeleteObjectRequest(AWS_BUCKET_NAME, path + fileName));
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}
like image 322
chooco13 Avatar asked Nov 01 '25 11:11

chooco13


1 Answers

@Async is an indication to Spring to execute this method asynchronously. So it can only work on several conditions :

  1. The class must be managed by Spring
  2. The method has to be public
  3. The method has to be called using Spring

For the latter, it seems like you are calling this method directly in your class, so Spring has no way to know that you called this method, it's not htat magic.

You should refactor your code so the method is called on a bean managed by Spring as following code :

@Service
public class AsyncService {
    @Async
    public void executeThisAsync() {...}
}

@Service
public class MainService {
    @Inject
    private AsyncService asyncService;

    public mainMethod() {
        ...
        // This will be called asynchronusly
        asyncService.executeThisAsync();
    }
    ...
}
like image 55
Matt Avatar answered Nov 04 '25 00:11

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!