如何解决如何处理 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();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。