返回ServiceFault&使用单个@Async方法进行响应

时间:2015-09-22 16:14:56

标签: java spring spring-boot future

我正在使用@Async注释和Java Future通过传递不同的员工ID来异步调用Web服务。

@Async
public Future<Employee> getEmployeeDetails(String id){

    // Call external service
    return new AsyncResult<Employee>(response);
}

我的通话代码类似于下面的

Future<Employee> employeeFuture = asyncCustomerImpl.getEmployeeDetails("1415");
employeeList.add(employeeFuture.get());

我能够获得响应但是我想处理一些未经检查的异常返回并返回一条客户友好的消息。但是使用此代码,我无法在调用代码中捕获异常。请帮助我理解我需要遵循的逻辑。

编辑:

通话方式:

   try{
        employee = employeeFuture.get();
        System.out.println(" employee is " + employee);
    } catch (FrameworkError ex) {
        System.out.println(" Framework Error " + e);        
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        System.out.println(" InterruptedException" + e);    
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        System.out.println(" ExecutionException " + e);
    } catch (Exception ex){
        System.out.println(" General Exception ");
    }

被叫(异步方法)

@Async
public Future<Employee> getEmployeeDetails(String id){

    try{
     // Call external service
    } catch (Exception e){
       Throw new FrameworkError(e);
    }
    return new AsyncResult<Employee>(response);
}

当我运行上面的代码时,我看到员工在结果中为空。我无法看到控件进入任何catch块。

错误记录

  

错误6412 --- [task-scheduler-2] handler.LoggingHandler:java.lang.RuntimeException       at service.impl.EmployeeImpl.getEmployeeDetails(AsyncEmployeeImpl.java:49)       at service.impl.AsyncTicketsImpl $$ FastClassBySpringCGLIB $$ 576d0436.invoke()       在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)       在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)       在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)       在org.springframework.aop.interceptor.AsyncExecutionInterceptor $ 1.call(AsyncExecutionInterceptor.java:108)       在org.springframework.scheduling.concurrent.ThreadPoolTask​​Scheduler $ DelegatingErrorHandlingCallable.call(ThreadPoolTask​​Scheduler.java:386)       at java.util.concurrent.FutureTask.run(Unknown Source)       at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201(未知来源)       at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       在java.lang.Thread.run(未知来源)

创建了一个独立的应用程序(https://spring.io/guides/gs/async-method/#initial)并返回了一个异常,我能够按预期在.get上看到异常。但是,当我在我的大型程序中运行此代码时,我无法看到异常。

来自独立应用程序的Stacktrace:

  

at hello.FacebookLookupService.findPage(FacebookLookupService.java:22)       at hello.FacebookLookupService $$ FastClassBySpringCGLIB $$ a8016679.invoke()       在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)       在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)       在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)       在org.springframework.aop.interceptor.AsyncExecutionInterceptor $ 1.call(AsyncExecutionInterceptor.java:110)       at java.util.concurrent.FutureTask.run(Unknown Source)       在java.lang.Thread.run(未知来源)

我的应用程序中的Stacktrace已粘贴在上面。这是我看到的主要区别:

  

at org.springframework.scheduling.concurrent.ThreadPoolTask​​Scheduler $ DelegatingErrorHandlingCallable.call(ThreadPoolTask​​Scheduler.java:386)

工作独立应用程序在日志中没有此行。当我查看这个类(org.springframework.scheduling.concurrent.ThreadPoolTask​​Scheduler)时,我看到了下面的内容。它返回null而不是异常,我猜是导致错误:

        @Override
    public V call() throws Exception {
        try {
            return this.delegate.call();
        }
        catch (Throwable t) {
            this.errorHandler.handleError(t);
            return null;
        }
    }

0 个答案:

没有答案