@Async方法在Spring Boot中会在调用方法发生异常后继续执行。

huangapple 未分类评论46阅读模式
英文:

@Async method get executed in spring boot even after execption in caller method

问题

class ServiceImpl {
    @Autowired
    EmailService emailService;

    public void arr() throws Exception, CommonCustomException {
        int[] arr = { 1, 2, 3, 4 };
        try {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(i + " ");
            }
        } catch (Exception e) {
            throw new Exception();
        }

        try {
            emailService.sendMail("Subject of mail", "body of mail", "xuz@gmail.com", "abc@gmail.com");
        } catch (CommonCustomException e) {
            throw new CommonCustomException("Exception occurred while sending mail");
        }
    }
}

@EnableAsync
class EmailService {
    @Async
    public void sendMail(String subject, String body, String from, String to) {
        // Implementation of sendMail function
    }
}

I have used the @Async annotation in the email service to trigger the email asynchronously. If an error occurs in the caller method, the email is currently being triggered regardless of any exceptions in the caller method.

To prevent the email method from executing after an error occurs in the caller method, you can adjust your code as follows:

In the arr() method, catch the specific exception that occurs in the loop (for example, ArrayIndexOutOfBoundsException), and handle it without rethrowing it. This way, the subsequent email triggering code won't be reached when an exception occurs in the loop.

public void arr() throws CommonCustomException {
    int[] arr = { 1, 2, 3, 4 };
    try {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i + " ");
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        // Handle the exception without rethrowing
        // You can log the exception or take appropriate action
    }

    try {
        emailService.sendMail("Subject of mail", "body of mail", "xuz@gmail.com", "abc@gmail.com");
    } catch (CommonCustomException e) {
        throw new CommonCustomException("Exception occurred while sending mail");
    }
}

This way, the email sending code will only be reached if no exception occurs in the loop.

英文:
class ServiceImpl {
	@Autowired
	EmailService emailService;

	public void arr() throws Exception,CommonCustomException {
		int[] arr = { 1, 2, 3, 4 };
       try{
		for (int i = 0; i &lt; arr.length() + 1; i++) {
			System.out.print(i + &quot; &quot;);
		}
        }
       catch(exception e)
       {
          throw new Exception();
       }

     try{
		emailService.sendMail(&quot;Subject of mail&quot;, &quot;body of mail&quot;, &quot;xuz@gmail.com&quot;, &quot;abc@gmail.com&quot;);
         }
     Catch(CommonCustomException e)
       {
           throw new CommonCustomException(&quot;Exception occured while sending 
                   mail&quot;);
       }
	}
}

@EnableAsync
class EmailService {
	@Async
	public void sendMail(String subject, String body, String from, String to) {
		// Implementation of sendMail function
	}
}

I have used @Async annotation in the email service for triggering the mail, if any error occurs in the caller method the mail is getting triggered irrespective of any exceptions in the caller method.

I don't want the email method to get executed after the error occurs in the caller method. how can I handle this?

In the above example, the for-loop throws arrayIndexOutOfBoundException even after the exception occurs the mail will get triggered since it is an async function. I don't want the mail to get triggered if an exception occurs in arr() method.

huangapple
  • 本文由 发表于 2020年5月5日 13:14:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/61606210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定