我将预定的消息发送到activemq代理,但在Web控制台中显示了两条消息

时间:2013-02-18 10:52:58

标签: java jms activemq message-queue

我编写了一个简单的ActiveMQ客户端程序,用于生成如下消息:

public static void main(String[] args) throws Throwable
{
    final ActiveMQConnectionFactory conFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    final QueueConnection connection = conFactory.createQueueConnection();
    final Session session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    final Destination destination = new ActiveMQQueue("MJ_SAF");
    final MessageProducer producer = session.createProducer(destination);

    Message message = session.createTextMessage("test");
    message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 20);
    message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1);
    message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
    producer.send(message);
}

当这个程序执行时,我希望在ActiveMQ的预定部分存储一条消息,并在20秒之后发送MJ_SAF队列但是当我连接到web consol时看到以下结果:

enter image description here

MJ_SAF存储了两条消息并且它不正确但是当我正常发送消息(没有调度)时,我在Web控制台中看到一条消息。

public static void main(String[] args) throws Throwable
{
    final ActiveMQConnectionFactory conFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    final QueueConnection connection = conFactory.createQueueConnection();
    final Session session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    final Destination destination = new ActiveMQQueue("MJ_SAF");
    final MessageProducer producer = session.createProducer(destination);

    Message message = session.createTextMessage("test");
    producer.send(message);
}

通过上面的代码,一切都很好:

enter image description here

我不明白这种行为。有谁知道这个的原因?

1 个答案:

答案 0 :(得分:4)

您将属性ScheduledMessage.AMQ_SCHEDULED_REPEAT设置为1,因此它会重复1次,总共会显示2条消息。

有关属性的说明,请参阅here。据我了解,您当前的设置将等待20毫秒,发布1条消息,等待1毫秒,然后发布第二条消息,然后结束。