英文:
@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 < arr.length() + 1; 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 occured 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 @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.
专注分享java语言的经验与见解,让所有开发者获益!
评论