【问题标题】:Apache camel: filter / throttle to take just the first message from a queueApache camel:过滤/节流以仅从队列中获取第一条消息
【发布时间】:2014-03-01 18:40:08
【问题描述】:

从队列中获取第一条消息的最简单方法是什么?

鉴于我在标题中看不到任何要过滤的内容(没有序列号等,至少据我所知),还有什么比这更好的吗?

from("webspheremq:topic:SNAPSHOTS")
    .throttle(1).timePeriodMillis(1234567890L * 1000)
    .to("direct:anotherqueue")

比起 beans + java 代码更喜欢骆驼 DSL :)

编辑

实际上是从 webspheremq 主题中读取的。

编辑2

不要使用Long.MAX_VALUE 作为时间段!试试 1234567890L * 1000 代替

【问题讨论】:

  • 我对用例很好奇?
  • 用例是在队列中接收到一条大消息(数据的快照)(嗯,实际上,它是一个主题)。其他请求快照的用户将特别触发另一个快照发送给所有消费者。奇怪的行为,但我必须以这种方式处理它:)

标签: java apache-camel dsl throttling


【解决方案1】:

您可以尝试使用保持第一状态的单例进行过滤:

public static class FirstOrNot {
    private static FirstOrNot _instance;

    public synchronized boolean isfirst() {
        if ( _instance == null ) {
            _instance = new FirstOrNot();
            return true;
        }
        return false;
    }
}

FirstOrNot first = new FirstOrNot();

from("webspheremq:topic:SNAPSHOTS")
    .filter().method( first , "isFirst" )
    .to("direct:anotherqueue")

也许你可以以此为起点。

干杯,

【讨论】:

  • 是的,太好了,谢谢!我还添加了一个方法reset(),因为我需要它。
猜你喜欢
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 2021-03-10
相关资源
最近更新 更多