简体   繁体   中英

Java monitoring activemq but without polling the queue

I need to program something that monitors an activemq queue in java. 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

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

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...

see 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
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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