AsyncWrapper.java 874 B

12345678910111213141516171819202122232425262728293031323334
  1. package cn.seecoder.paas.util;
  2. import lombok.extern.apachecommons.CommonsLog;
  3. import org.springframework.scheduling.annotation.Async;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @CommonsLog
  7. public class AsyncWrapper {
  8. @FunctionalInterface
  9. public interface Method {
  10. void invoke() throws Exception;
  11. }
  12. @Async("asyncExecutor")
  13. public void asyncInvoke(Method method) {
  14. try {
  15. method.invoke();
  16. } catch (Exception e) {
  17. log.error("进行异步调用时发生异常[" + e.getClass() + "]:" + e.getMessage());
  18. }
  19. }
  20. @Async("asyncExecutor")
  21. public void asyncInvoke(Method method, long delay) {
  22. try {
  23. Thread.sleep(delay);
  24. } catch (InterruptedException e) {
  25. Thread.currentThread().interrupt();
  26. }
  27. asyncInvoke(method);
  28. }
  29. }