Created
April 16, 2015 05:58
-
-
Save adrianhsieh/bb2af3a00122f9a79c97 to your computer and use it in GitHub Desktop.
Custom Mule Component with QueueBrowser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.mule.api.jms.extension; | |
| import java.util.Enumeration; | |
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| import java.util.Set; | |
| import javax.jms.ConnectionFactory; | |
| import javax.jms.Message; | |
| import javax.jms.QueueBrowser; | |
| import javax.jms.Session; | |
| import org.apache.activemq.ActiveMQConnection; | |
| import org.apache.activemq.ActiveMQConnectionFactory; | |
| import org.apache.activemq.command.ActiveMQQueue; | |
| import org.apache.log4j.Logger; | |
| import org.mule.api.MuleEventContext; | |
| import org.mule.api.lifecycle.Callable; | |
| /** | |
| * A way to browse queue depth | |
| * @author adrianhsieh | |
| * | |
| */ | |
| public class CurrentQueueSize implements Callable { | |
| private String host1; | |
| private String host2; | |
| private int port1; | |
| private int port2; | |
| @SuppressWarnings("unchecked") | |
| @Override | |
| public Object onCall(MuleEventContext eventContext) throws Exception { | |
| Logger logger = Logger.getLogger(org.mule.api.jms.extension.CurrentQueueSize.class); | |
| if (host2 == null) { | |
| host2 = host1; | |
| port2 = port1; | |
| } | |
| String brokerURL = "failover:(tcp://" + host1 + ":" + port1 + ",tcp://" + host2 + ":" + port2 + ")?randomize=false"; | |
| logger.debug("Broker URL: " + brokerURL); | |
| ConnectionFactory out = new ActiveMQConnectionFactory(brokerURL); | |
| ActiveMQConnection connection = (ActiveMQConnection) out.createConnection(); | |
| connection.start(); | |
| Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | |
| Set<ActiveMQQueue> amqs = connection.getDestinationSource().getQueues(); | |
| Iterator<ActiveMQQueue> queues = amqs.iterator(); | |
| HashMap<String, Integer> payload = new HashMap<String, Integer>(); | |
| while ( queues.hasNext() ) | |
| { | |
| ActiveMQQueue queue_t = queues.next(); | |
| String q_name = queue_t.getPhysicalName(); | |
| logger.debug( "Queue = " + q_name); | |
| QueueBrowser queueBrowser = session.createBrowser(queue_t); | |
| Enumeration<Message> e = (Enumeration<Message>) queueBrowser.getEnumeration(); | |
| int numMsgs = 0; | |
| while(e.hasMoreElements()) | |
| { | |
| Message message = (Message) e.nextElement(); | |
| logger.trace("Current message: " + message.toString()); | |
| numMsgs++; | |
| } | |
| logger.debug(q_name + ": No of messages = " + numMsgs); | |
| queueBrowser.close(); | |
| payload.put(q_name, numMsgs); | |
| } | |
| session.close(); | |
| connection.close(); | |
| return payload; | |
| } | |
| public String getHost1() { | |
| return host1; | |
| } | |
| public void setHost1(String host1) { | |
| this.host1 = host1; | |
| } | |
| public String getHost2() { | |
| return host2; | |
| } | |
| public void setHost2(String host2) { | |
| this.host2 = host2; | |
| } | |
| public int getPort1() { | |
| return port1; | |
| } | |
| public void setPort1(int port1) { | |
| this.port1 = port1; | |
| } | |
| public int getPort2() { | |
| return port2; | |
| } | |
| public void setPort2(int port2) { | |
| this.port2 = port2; | |
| } | |
| } |
org.mule.api.MuleEventContext is deprecated in Mule 4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do we have utility in MULE or any future to get the depth of the queues?