首页 > 解决方案 > 如何使用 Apache Camel 从 ActiveMQ 主题中读取旧的入队消息

问题描述

我有 Apache Camel 客户端(消费者),它监听来自远程 ActiveMQ 主题的消息,我观察到它只在消费者运行时读取消息。

如果消费者没有收听并且生产者发送消息,它们将永远排队并由消费者选择。

我希望我的客户在启动时应该阅读所有排队的消息

    public static void main(String[] args) throws Exception
    {
        ActiveMQComponent amq = new ActiveMQComponent();
        amq.setConnectionFactory( new ActiveMQConnectionFactory() );
        amq.setUsername("admin");
        amq.setPassword("admin");
        amq.setBrokerURL("tcp://localhost:8161");

        Main main = new Main();
        main.bind("activemq", amq);
        main.addRouteBuilder(new MyRouter());
        main.run(args);
    }

以下是路由器代码

    public void configure() throws Exception
    {
        from("activemq:topic:saadtopic")
        .transform(simple(" ${body}"))
        .to("stream:out");
    }

ActiveMQ=5.15.9 ApacheCamel=2.24.1

在此处输入图像描述

标签: apache-cameljmsactivemq

解决方案


如果您没有使用持久订阅,并且您将消息发送到没有订阅者的主题,则该消息本质上将被丢弃,再也不会被看到。仅当存在具有离线订阅者的持久订阅并且消息被标记为持久时,代理才存储主题消息。

如果您希望能够访问在消费者关闭时发送的消息,那么您需要确保这些消费者至少运行一次并创建了持久主题订阅。


推荐阅读