如何处理 quarkus 中的超时异常?

问题描述

我正在尝试从 quarkus 控制器连接到第三方 API。我有一个使用服务方法的控制器。 try catch 块不起作用。我拥有所有必需的依赖项,并且我遵循了 quarkus doc 这是代码

控制器

package com.ncr.invoice;

// all imports over here 

@Path("/api")
@RequestScoped
public class InvoiceController {
    // all variable and injection 
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/invoice")
    @Timeout(250)
    @PermitAll
    public Response getInvoice(AuthTokenRequestModel atrm){
            SoupResponseInvoiceDetail srid = null;
            try{
                 srid = service.getInvoice(
                    atrm.getMcn(),"transactionId",atrm.getInvoiceNumber()
                 );
                 LOG.info("try block end");
            }
            catch(InterruptedException e)
            {
                LOG.info("Over here");
                return Response.status(401).build();
            }
          
            return Response.ok(srid).build();
        }

        return Response.status(401).build();
    }

 
    // all getter setter 
}

服务

package com.ncr.invoice;

//all imports 

@Path("/")
@RegisterRestClient(configKey="invoice-api")
@ClientHeaderParam(name = "Authorization",value = "{basicAuth}")
public interface InvoiceService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/")
    public SoupResponseInvoiceDetail getInvoice(@QueryParam("id") String id,@QueryParam("txn_id") String txnId,@QueryParam("invoice") String invoice) throws InterruptedException;

    default String basicAuth() {
        String uName = ConfigProvider.getConfig().getValue("auth.username",String.class);
        String pwd = ConfigProvider.getConfig().getValue("auth.pwd",String.class);
        String creds =  uName + ":" + pwd;
        return "Basic " + Base64.getEncoder().encodeToString(creds.getBytes());
    }
}

我收到的错误 2021-01-07 13:07:42,286 INFO [com.ncr.inv.InvoiceController] (executor-thread-189) 尝试阻止结束 2021-01-07 13:07:42,555 错误 [io.und.req.io] (executor-thread-189) 异常处理请求 58a6d4b3-76c1-4a8b-b4a0-1e241219fb4d-4 到 /api/injboice: .resteasy.spi.UnhandledException: org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException: Timeout[com.ncr.invoice.InvoiceController#getInvoice] 超时 在 org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:106) 在 org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:372) 在 org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218) 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519) 在 org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:261) 在 org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:161) ...................................................

解决方法

在我看来,您需要执行以下操作: @提供者 公共类 TimeoutExceptionMapper 实现 ExceptionMapper { private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass());
@覆盖 公共响应响应(超时异常){ LOGGER.warn(exception.getMessage()); 返回 getResponse(); }

public static Response getResponse() {      
    ErrorResponse response = new ErrorResponse();
    response.setDescription("Operation timet out,try again later");
    return status(Response.Status.GATEWAY_TIMEOUT).entity(response).build();
}

}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...