Skip to content

Instantly share code, notes, and snippets.

@b1a9id
Last active July 9, 2021 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b1a9id/bb5059c20430bab9802a57b6fbb63873 to your computer and use it in GitHub Desktop.
Save b1a9id/bb5059c20430bab9802a57b6fbb63873 to your computer and use it in GitHub Desktop.
asyncのAsyncServiceImpl.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.b1a9idps.springasyncdemo.dto.request.AsyncRequest;
import com.b1a9idps.springasyncdemo.exception.FailedFileUploadException;
import com.b1a9idps.springasyncdemo.infrastructure.FileService;
import com.b1a9idps.springasyncdemo.service.AsyncService;
@Service
public class AsyncServiceImpl implements AsyncService {
private static final Logger LOGGER = LoggerFactory.getLogger(AsyncServiceImpl.class);
private final FileService fileService;
public AsyncServiceImpl(FileService fileService) {
this.fileService = fileService;
}
@Override
@Retryable(value = FailedFileUploadException.class, recover = "saveRecover")
@Async
public void save(AsyncRequest request) {
LOGGER.info("Start Async processing.(number = " + request.getNumber() + ")");
try {
Thread.sleep(500);
fileService.upload();
} catch (InterruptedException e) {
LOGGER.error("thrown InterruptedException.");
}
LOGGER.info("End Async processing.(number = " + request.getNumber() + ")");
}
@Recover
private void saveRecover(FailedFileUploadException e, AsyncRequest request) {
LOGGER.error("failed to upload file(number = " + request.getNumber() + ")", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment