繁体   English   中英

Java监视activemq,但不轮询队列

[英]Java monitoring activemq but without polling the queue

我需要编写一些程序来监视Java中的activemq队列。 这意味着我需要记录何时将消息排入队列以及何时将消息排出队列。 我的程序不能发送消息或接收消息,它只需要登录。

我发现要推送消息和接收消息,但这不是我想要做的,只是记录外部进程是否将消息放入队列或从队列中取出。

为了更清楚,我画了一张图 在此处输入图片说明

我使用apache骆驼进行集成,我的routebuilder看起来像

public void configure() throws Exception {
        Processor queueProcessor = new QueueProcessor();

        from("activemq:queue:KBC").process(queueProcessor);
    }

它称为以下处理器

@Override
    public void process(Exchange exchange) throws Exception {
        Trax_EventDao dao = new Trax_EventDao();
        dao.insert(new Trax_Event("Queue",exchange.getExchangeId(),"UP","KBC", new Time(new Date().getTime())));
    }

dao处理数据库连接并插入一条记录

实际的问题是,当我将消息推送到队列中并且程序运行时,该消息已记录下来,这是可以的,但是它也会立即被轮询,这是不可以的。 如何在不轮询消息的情况下进行插入?

您可以使用ActiveMQ咨询消息来监视队列活动...

参见http://activemq.apache.org/advisory-message.html

我最后要做的是编写一个自己的运行程序类,该类使用队列浏览器。

我想在这堂课上做的是

  1. 与AvtiveMQ建立连接并启动它
  2. 进行无限循环,以控制指定的队列。 我有队列中项目的列表。 每次循环我都检查一下
  3. 如果列表大于队列的大小,则有项目出队。 这意味着我需要循环循环并检查哪些项目已出队。 否则,我循环队列的枚举并将元素添加到列表(如果尚不存在)

     package queueFeed; import dao.ProcmonDao; import dao.EventDao; import domain.Event; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue; import javax.jms.*; import java.sql.SQLException; import java.sql.Time; import java.util.*; public class QueueRunner { private ProcmonDao dao; private Connection connection; private String queueName; public QueueRunner() throws SQLException { dao = new EventDao(); } public void setConnection(String username, String password, String url) throws JMSException { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(username, password, url); connection = factory.createConnection(); } public void run() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); QueueBrowser browser = session.createBrowser(new ActiveMQQueue(queueName)); List<String> ids = new ArrayList<>(); int queueSize = 0; int counter = 0; connection.start(); while (true) { Enumeration enumeration = browser.getEnumeration(); if (queueSize < ids.size()) { while (enumeration.hasMoreElements()) { Message message = (Message) enumeration.nextElement(); ids.remove(message.getJMSMessageID()); counter++; } if (ids.size() > 0 && ids.size() > 0) { Iterator<String> iterator = ids.iterator(); while (iterator.hasNext()) { String messageId = iterator.next(); dao.insert(new Event("Queue", messageId, "UP", browser.getQueue().getQueueName(), new Time(new Date().getTime()))); iterator.remove(); } } queueSize = counter; counter = 0; } else { while (enumeration.hasMoreElements()) { counter++; Message message = (Message) enumeration.nextElement(); String id = message.getJMSMessageID(); if (!ids.contains(id)) { ids.add(id); dao.insert(new Event("Queue", id, "UP", browser.getQueue().getQueueName(), new Time(new Date().getTime()))); } } queueSize = counter; counter = 0; } } } public void setQueueName(String queueName) { this.queueName = queueName; } public String getQueueName() { return this.queueName; } 

    }

这还不够完美。 我认为其中存在一个小逻辑问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM