apache camel polling a rest endpoint

时间:2016-11-23 09:50:48

标签: apache-camel

我有一个休息端点sample.org,它返回表单

的json响应
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

我的路线看起来像这样

    public class AppleScript : MonoBehaviour
    {
        public float fallSpeed = 8.0f;

        //Variables for starting position and length until reset
        private Vector3 _startingPos;
        public float FallDistance = 5f;

        void Start()
        {
            transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);
            // Save starting position
            _startingPos = transform.position;
        }

        void Update()
        {
            transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);

            // If the object has fallen longer than 
            // Starting height + FallDistance from its start position
            if (transform.position.y > _startingPos.y + FallDistance) {
                transform.position = _startingPos;
            }
        }
    }

我读到polling consumer但是找不到关于如何继续轮询端点的示例,直到它将响应返回为“成功”。

是否应该使用投票消费者?如果是这样,可以举例说明与我的案例相关的例子。用于轮询其余端点的任何其他资源都非常有用。

2 个答案:

答案 0 :(得分:5)

您需要从计时器开始,然后调用其余端点。然后你可以检查结果,然后它是否使用controlbus停止路由。过滤器可用于检查其是否挂起,然后只是停止继续路由,然后下一个计时器将再次尝试。

沿着这条伪路线徘徊

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

您可以在

找到更多详情

答案 1 :(得分:2)

我遇到了类似的问题,并以writing a custom endpoint结束投票。

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

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

轮询端点使用一个简单的处理器,该处理器使用轮询使用者进行轮询。

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.");
    }
}