简体   繁体   English

Java监视activemq,但不轮询队列

[英]Java monitoring activemq but without polling the queue

I need to program something that monitors an activemq queue in java. 我需要编写一些程序来监视Java中的activemq队列。 This means I need to log when a message is enqueued on the queue and when a message is dequeued. 这意味着我需要记录何时将消息排入队列以及何时将消息排出队列。 My program must not send messages or receives messages, it only needs to log. 我的程序不能发送消息或接收消息,它只需要登录。

I found out to push messages and receive messages but this is not what I want to do, just log if an external process puts messages on or off the queue. 我发现要推送消息和接收消息,但这不是我想要做的,只是记录外部进程是否将消息放入队列或从队列中取出。

To make it more clear I made a drawing 为了更清楚,我画了一张图 在此处输入图片说明

I use apache camel to make the integration, my routebuilder looks like 我使用apache骆驼进行集成,我的routebuilder看起来像

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

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

it calls the folowwing processor 它称为以下处理器

@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())));
    }

The dao handles a database connection and makes an insert of a record dao处理数据库连接并插入一条记录

The actual problem is that when I push a message on the queue and the program runs, the message got logged which is okay, but it also get polled immediately, which is not okay. 实际的问题是,当我将消息推送到队列中并且程序运行时,该消息已记录下来,这是可以的,但是它也会立即被轮询,这是不可以的。 How can I make the insert without the message being polled? 如何在不轮询消息的情况下进行插入?

you can use ActiveMQ Advisory Messages to monitor queue activity... 您可以使用ActiveMQ咨询消息来监视队列活动...

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

What I finally did was writing an own runner class, which uses a queuebrowser. 我最后要做的是编写一个自己的运行程序类,该类使用队列浏览器。

What I wanted to do with this class is 我想在这堂课上做的是

  1. Make a connection with avtivemq and start it 与AvtiveMQ建立连接并启动它
  2. Make an endless loop, which controls the queue specified. 进行无限循环,以控制指定的队列。 I have a list of the items on the queue. 我有队列中项目的列表。 At every loop I check this 每次循环我都检查一下
  3. if the list is bigger than the size of the queue, there are items dequeued. 如果列表大于队列的大小,则有项目出队。 This means I need to loop this and check which items are dequeued. 这意味着我需要循环循环并检查哪些项目已出队。 Otherwhise I loop the enumeration of the queue and add elements to the list if they don't exist yet 否则,我循环队列的枚举并将元素添加到列表(如果尚不存在)

     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; } 

    } }

This works not perfect yet. 这还不够完美。 I think there is a small logical issue in it. 我认为其中存在一个小逻辑问题。

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

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