【问题标题】:How to handle timeout exception in quarkus?如何处理 quarkus 中的超时异常?
【发布时间】:2023-03-18 09:33:02
【问题描述】:

我正在尝试从 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/invoice: org.jboss .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) .........................................

【问题讨论】:

  • 看起来您想确保外部服务调用不会花费太长时间?在这种情况下,只需将 @Timeout(250) 从端点 (InvoiceController.getInvoice) 移动到客户端接口 (InvoiceService.getInvoice)。在端点中,捕获TimeoutException

标签: exception quarkus fault timeoutexception microprofile


【解决方案1】:

在我看来,您需要执行以下操作: @Provider 公共类 TimeoutExceptionMapper 实现 ExceptionMapper { 私有静态最终记录器 LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass());
@覆盖 公共响应到响应(TimeoutException 异常){ 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();
}

}

【讨论】:

    猜你喜欢
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2013-02-19
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多