简体   繁体   English

如何在 Mulesoft 4 自定义连接器中使用非阻塞 HTTP 请求

[英]How to use non-blocking HTTP requests in Mulesoft 4 Custom Connector

I'm trying to build a Mulesoft custom connector which makes HTTP requests to a third-party system, and I'd like these HTTP requests to be made in a non-blocking manner such that execution can continue without waiting on the HTTP response to be returned.我正在尝试构建一个 Mulesoft 自定义连接器,该连接器向第三方系统发出 HTTP 请求,我希望以非阻塞方式发出这些 HTTP 请求,以便执行可以继续执行而无需等待 Z293C9EA246FFF9989ADC6 的响应被退回。

There is an example of this in the Mulesoft documentation here which shows this example code: 此处的 Mulesoft 文档中有一个示例,其中显示了此示例代码:

public void request(String url, @Connection HttpClient client, @Content String body, 
    CompletionCallback<InputStream, HttpAttributes> callback ) { 
 client.send(url, body, new HttpResponseCallback() {
   void onResponse(HttpResponse response) {
     callback.success(Result.<InputStream, HttpAttributes>builder() 
                          .output(response.getBody())
                          .attributes(toAttributes(response))
                          .build());
   }

   void onError(Exception e) {
     callback.error(e); 
   }
 });
}

It also states that non-blocking behaviour can be provided它还指出可以提供非阻塞行为

by an HttpClient that supports asynchronous responses通过支持异步响应的 HttpClient

Mulesoft's custom connector documentation statesMulesoft 的自定义连接器文档状态

The HttpClient is capable of using non-blocking I/O to make the requests. HttpClient 能够使用非阻塞 I/O 来发出请求。

but I don't understand how!但我不明白怎么做!

The example code above calls a method send(String, String, HttpResponseCallback) from the HttpClient interface.上面的示例代码从HttpClient接口调用方法send(String, String, HttpResponseCallback) However, the HttpClient interface, as documented in Mulesoft's API javadoc does not have such a method.但是, Mulesoft 的 API javadoc中记录的HttpClient接口没有这样的方法。

I see that the HttpClient interface does have sendAsync(HttpRequest request) methods, but I'm failing to understand how that could be used with the example code.我看到HttpClient接口确实具有sendAsync(HttpRequest request)方法,但我无法理解如何将其与示例代码一起使用。

I understand that Mulesoft's HttpClient is implemented using Project Grizzly's HTTP client, and that supports non-blocking requests, so I feel this is possible to do, I just don't understand how...我了解 Mulesoft 的HttpClient是使用 Project Grizzly 的 HTTP 客户端实现的,并且支持非阻塞请求,所以我觉得这是可以做到的,我只是不明白如何......

Thanks for any tips!感谢您的任何提示!

Hi was trying to achieve the same thing, with the same non-blocking-operations documentation, but I wasn't able, So I try based on the Slack connector , it has a lot of examples there, and they use other code to achieve async calls ChannelOperations with CompletionCallback嗨,我试图用相同 的非阻塞操作文档来实现同样的事情,但我做不到,所以我尝试基于Slack 连接器,那里有很多示例,他们使用其他代码来实现异步调用具有 CompletionCallback 的 ChannelOperations

However, this did not work for me, I think maybe I have to make another workaround on the server-side to achieve async calls.但是,这对我不起作用,我想也许我必须在服务器端进行另一种解决方法才能实现异步调用。 Anyway, In the end, I use CompletableFuture to run the request in other thread, the request is in sync way but is performed Async in the CompletableFuture.runAsync无论如何,最后,我使用 CompletableFuture 在其他线程中运行请求,请求是同步方式但在 CompletableFuture.runAsync 中执行异步

 public void request(String url, @Connection HttpClient client, @Content String body) { HttpResponse response = null; HttpEntity entity = new ByteArrayHttpEntity(body.toString().getBytes()); HttpRequest httpRequest = HttpRequest.builder().uri(url).addHeader("Content-Type", "application/json").method("POST").entity(entity).build(); CompletableFuture.runAsync(() -> { try { client.start(); response = client.send(httpRequest, 30000, true, null); } catch (IOException | TimeoutException e) { LOGGER.error(response.toString()); } LOGGER.info(response.toString()); client.stop(); LOGGER.info("Finish"); }); }

There is a Mule connector similar to DMI where the calls are made async有一个类似于DMI的 Mule 连接器,其中调用是异步的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM