java - WLP MicroProfile (FaultTolerance) 超时实现不会中断线程?

标签 java websphere-liberty fault-tolerance open-liberty microprofile

我正在测试 websphere liberty 的容错(microprofile)实现。因此,我制作了一个简单的 REST 服务,其中的资源会 hibernate 5 秒:

 @Path("client")
 public class Client {

      @GET
      @Path("timeout")
      public Response getClientTimeout() throws InterruptedException {
          Thread.sleep(5000);
          return Response.ok().entity("text").build();
      }
 }

我在另一个 REST 服务的同一应用程序中调用此客户端:

 @Path("mpfaulttolerance")
 @RequestScoped
 public class MpFaultToleranceController {

      @GET
      @Path("timeout")
      @Timeout(4)
      public Response getFailingRequest() {
          System.out.println("start");
          // calls the 5 seconds-ressource; should time out
          Response response = ClientBuilder.newClient().target("http://localhost:9080").path("/resilience/api/client/timeout").request().get();
          System.out.println("hello");
      }
 }

现在我预计 getFailingRequest() 方法会在 4 毫秒后超时并抛出异常。实际行为是应用程序打印“start”,等待 5 秒直到客户端返回,打印“hello”,然后抛出“org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException”。

我打开了更多调试信息:

<logging traceSpecification="com.ibm.ws.microprofile.*=all" />

在 server.xml 中。我得到这些信息,即使在调用客户端之前也已注册超时!但线程并没有被中断。

(如果有人告诉我如何在这里获得漂亮的堆栈跟踪......我可以做到。)

因为这是一个非常基本的例子:我在这里做错了什么吗?我该怎么做才能使这个例子正常运行。

谢谢

编辑:在 WebSphere Application Server 18.0.0.2/wlp-1.0.21.cl180220180619-0403) auf Java HotSpot(TM) 64 位服务器 VM,版本 1.8.0_172-b11 (de_DE) 上运行此示例,具有以下功能webProfile-8.0、mpFaultTolerance-1.0 和 localConnector-1.0。

编辑:解决方案,感谢 Andy McCright 和 Azquelt。 由于调用不能被中断,我必须使其异步。所以你有 2 个线程:第一个线程通过调用调用第二个线程。第一个线程将被中断,第二个线程将保留直到调用完成。但现在您可以继续进行故障处理、打开电路等,以防止进一步调用损坏的服务。

@Path("mpfaulttolerance")
@RequestScoped
public class MpFaultToleranceController {

    @Inject
    private TestBase test;

    @GET
    @Path("timeout")
    @Timeout(4)
    public Response getFailingRequest() throws InterruptedException, ExecutionException {
        Future<Response> resp = test.createFailingRequestToClientAsynch();
        return resp.get();
    }
}

客户端调用:

@ApplicationScoped
public class TestBase {

    @Asynchronous
    public Future<Response> createFailingRequestToClientAsynch() {
        Response response = ClientBuilder.newClient().target("http://localhost:9080").path("/resilience/api/client/timeout").request().get();
        return CompletableFuture.completedFuture(response);
    }
}

最佳答案

它确实使用 Thread.interrupt() 中断线程,但遗憾的是并非所有 Java 操作都会响应线程中断。

很多东西确实通过抛出 InterruptedException 来响应中断(例如 Thread.sleep()Object.wait()Future.get()InterruptableChannel 的子类),但 InputStreams 和 Sockets 不会。

我怀疑您(或您用来发出请求的库)正在使用不可中断的 Socket,因此您不会看到您的方法提前返回。

这特别不直观,因为 Liberty 的 JAX-RS 客户端不会响应 Andy McCright 提到的线程中断。我们知道情况不太好,我们正在努力改善情况。

关于java - WLP MicroProfile (FaultTolerance) 超时实现不会中断线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52427677/

相关文章:

java - 每次迭代后删除二维数组的列和行

java - 将图像从桌面拖放到 WYSIWYG 编辑器

java - 如何在 websphere liberty 8.5 中设置 spring.profiles.active

blockchain - 我们是否需要在许可的区 block 链网络中支持 PBFT 算法?

P2P应用程序容错的Java实现

java - Play Framework - 值登录不是 controllers.Application 的成员

java - Hibernate使用单表继承

docker - 使用 docker 获得 websphere 自由有什么好处?

java - 使用 openidConnectClient 功能的 WAS Liberty 中出现无效 cookie header 错误