当前位置: 首页 > 知识库问答 >
问题:

ApacheCamel轮询restendpoint

侯令雪
2023-03-14

我有一个restendpoint示例。org,返回表单的json响应

{
  "response" : "pending" 
}

我的路线是这样的

from("http://sample.org")
.marshal(xmlFormatConverterUtil.getxmlJsonDataFormat())  //To convert into json as I receive data in xml format which needs to be converted to json

我读过关于轮询消费者的内容,但找不到如何继续轮询endpoint的示例,直到它返回“success”响应。

是否应该使用轮询消费者?如果是这样的话,可以举一个与我的案例相关的例子。用于轮询restendpoint的任何其他资源都非常有用。

共有2个答案

温峻熙
2023-03-14

我也遇到了类似的问题,最后编写了一个自定义endpoint进行轮询。

它充当生产者并轮询指定的uri,直到满足指定的谓词或轮询达到最大尝试次数。

from("direct:start")
  .to("poll:http://example.com/status?maxRetries=3&successPredicate=#statusSuccess")

轮询endpoint使用一个简单的处理器,该处理器使用轮询消费者进行轮询。

public class PollProcessor implements Processor {

    private final String uri;
    private final long requestTimeoutMs;
    private final long period;
    private final int maxTries;
    private final Predicate<Exchange> successPredicate;

    public PollProcessor(String uri, long requestTimeoutMs, long period, int maxTries, Predicate<Exchange> successPredicate) {
        Preconditions.checkArgument(maxTries > 0);
        Preconditions.checkArgument(period >= 0);
        Preconditions.checkNotNull(successPredicate);

        this.uri = uri;
        this.requestTimeoutMs = requestTimeoutMs;
        this.period = period;
        this.maxTries = maxTries;
        this.successPredicate = successPredicate;
    }

    @Override
    public void process(Exchange exchange) throws Exception {
        PollingConsumer consumer = exchange.getContext().getEndpoint(uri).createPollingConsumer();

        for (int tryNumber = 1; tryNumber <= maxTries; ++tryNumber) {
            Exchange pollExchange = consumer.receive(requestTimeoutMs);
            if (successPredicate.test(pollExchange)) {
                exchange.setOut(pollExchange.getOut());
                exchange.setException(pollExchange.getException());
                return;
            }

            log.warn("Polling {} failed try number {}, waiting {} ms for next try...", uri, tryNumber);
            Thread.sleep(period);
        }

        throw new RuntimeException("Polling failed maximum allowed number of tries [" + maxTries + "], see log for details.");
    }
}
赏彭薄
2023-03-14

您需要从计时器开始,然后调用restendpoint。然后,您可以检查结果,如果结果正确,则使用controlbus停止路线。过滤器可用于检查其是否挂起,然后停止继续路由,然后下一个计时器将重试。

沿着这条伪路线的一些东西

from timer
  to http
  marshal
  filter (if pending)
     stop 
  end
  to something with positive response
  to controlbus stop route

您可以在以下网址找到更多详细信息:

  • http://camel.apache.org/timer
  • http://camel.apache.org/controlbus
  • http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html
  • http://camel.apache.org/message-filter.html
 类似资料:
  • 我正在努力寻找一个成熟的例子,说明如何在Spring Boot框架中使用ApacheCamel进行轮询。 我已经看过了:https://camel.apache.org/manual/latest/polling-consumer.html除此之外:https://camel.apache.org/components/latest/timer-component.html但是代码示例不够广泛,我

  • 我正在尝试向异步路由发送消息,但它不起作用。我刚刚在github上创建了一个项目来模拟这个问题

  • 我正在使用apache camel cxf开发一个Web服务(肥皂),我遇到了这个错误。 Java . lang . illegalargumentexception:Part { http://blue print . camel . ngt . TN/}返回的类型应为[ltn . ngt . camel . blue print . WB _ subscriptions;,而不是org . A

  • 问题内容: 我最近在StackOverflow上问了一个有关我的功能的问题,人们建议我使用Ajax Long Polling。我花了几天的时间研究该主题,并尝试编写基本的长轮询代码,但是这些代码都没有起作用,而且我什么也做不了。 这是我的基本功能: 有人能够告诉我如何将其转变为基本的长轮询功能,或者甚至直接指向我需要到达的路径。很感谢任何形式的帮助。谢谢! 问题答案: 通常(即,当不使用长时间轮询

  • 长轮询在GCP PubSub JS SDK上可用吗? 我希望能够同时处理多个PubSub消息,例如: 这是它将如何在AWS上工作的一个示例: SQS队列包含超过5条消息。 侦听器将在单个中一次获得5条消息。事件

  • 我的问题是我无法使用获得无限流。在我获得初始轮询()请求的凭据后-我执行初始轮询()请求。如果没有变化,每个轮询()请求会在25秒内响应,如果有任何变化,则会更早响应-返回changed_data[]。每个响应都包含下一个轮询请求所需的数据-我应该在每次轮询()响应后执行新轮询()请求。这是我的代码: 我是RxJava新手,也许我不懂一些东西,但我无法获得无限流。我接到3个电话,然后是onNext