以编程方式配置Apache ActiveMQ

时间:2017-07-03 13:10:23

标签: java maven jms activemq message-queue

我正在使用Apache ActiveMQ对大量消息进行排队,然后在一天结束时将它们出列。不过,我对ActiveMQ的运作方式感到困惑。在我的电脑上我没有安装ActiveMQ作为服务,我也没有在某处安装服务器。我刚刚将“activemq-all-5.14.5.jar”作为Maven依赖项包含在我的项目中,到目前为止我使用了以下代码:

public static void main(String[] args) throws URISyntaxException, Exception {
    BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:4848)"));
    broker.start();
    Connection connection = null;
    try {
        // Producer
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:4848");
        connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("customerQueue");
        String payload = "Important Task";
        Message msg = session.createTextMessage(payload);
        MessageProducer producer = session.createProducer(queue);
        System.out.println("Sending text '" + payload + "'");
        msg.setLongProperty("_AMQ_SCHED_DELIVERY", System.currentTimeMillis() + 5000L);
        producer.send(msg);

        // Consumer
        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();
        QueueBrowser browser = session.createBrowser(queue);
        while (browser.getEnumeration().hasMoreElements()) {
            TextMessage textMsg = (TextMessage) consumer.receive();
            browser.getEnumeration().nextElement();
            System.out.println(textMsg);
            System.out.println("Received: " + textMsg.getText());
        }

        session.close();
    } finally {
        if (connection != null) {
            connection.close();
        }
        broker.stop();
    }
}

正如您所看到的,我想将消息延迟5秒(或更多,可能会有所不同),但在我找到的每个指南中,我都被指示配置XML配置文件。但是,这是在将ActiveMQ作为服务运行时使用的文件。我目前正在使用只是jar库

最初,我已经安装了Glassgfish服务器以便使用JMS以便对所有消息进行排队,但从那时起我就放弃了该项目,但仍然在ActiveMQ中使用IP(localhost:4848)。

请注意,以下是一个完美的示例-KahaDB也用于在服务器发生故障时存储消息。

就我而言,ActiveMQ确实从我运行此代码的STS启动本地服务器,但配置文件在哪里?我可以以编程方式更改其属性吗?

1 个答案:

答案 0 :(得分:2)

这应该有效(适用于ActiveMQ 5.12.3)。请务必先清理KahaDB存储,以避免以前的消息被读取。

public static void main(String[] args) throws Exception {
    BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:4848)"));
    broker.setSchedulerSupport(true);
    broker.start();
    Connection connection = null;
    try {
        // Producer
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:4848");
        connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("customerQueue");
        String payload = "Important Task";
        Message msg = session.createTextMessage(payload);
        MessageProducer producer = session.createProducer(queue);
        System.out.println("Sending text '" + payload + "'");
        msg.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 5000L);
        producer.send(msg);
        connection.start();

        // Consumer
        MessageConsumer consumer = null;
        consumer = session.createConsumer(queue);

        QueueBrowser browser = session.createBrowser(queue);
        while (browser.getEnumeration().hasMoreElements()) {
            TextMessage textMsg = (TextMessage) consumer.receive();
            browser.getEnumeration().nextElement();
            System.out.println(textMsg);
            System.out.println("Received: " + textMsg.getText());
        }

        session.close();
    } finally{
        if (connection != null) {
            connection.close();
        }
        broker.stop();
    }
}

第一次清理运行(使用空的KahaDB存储)不应该输出

  

"收到:重要任务"

,而第二个是,如果你不删除它们之间的数据文件。

删除行

  

msg.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY,5000L);

将进行第一次清理运行输出"收到:重要任务"