001package dk.netarkivet.harvester.tools;
002
003import java.util.Enumeration;
004
005import javax.jms.JMSException;
006import javax.jms.QueueBrowser;
007
008import com.sun.messaging.jms.Message;
009
010import dk.netarkivet.common.distribute.ChannelID;
011import dk.netarkivet.common.distribute.JMSConnection;
012import dk.netarkivet.common.distribute.JMSConnectionFactory;
013import dk.netarkivet.harvester.distribute.HarvesterChannels;
014
015/** 
016 * Use the JMSConnection.createQueueBrowser() method to test number of
017 * entries in a specific Queue.
018 * If test successful, we will add a check of messages in the queue for the harvestjobchannel before submitting another 
019 * message to this queue 
020 * 
021 * @author svc
022 *
023 */
024public class HarvesterQueueControl {
025
026        public static void main(String[] args) throws JMSException {
027                String harvestChannelName = args[0];
028                
029                ChannelID queueID = HarvesterChannels.getHarvestJobChannelId(harvestChannelName, false);
030                System.out.println("Found in queue '" + queueID.getName() + "' #messages: " + getCount(queueID));
031        }
032
033        public static int getCount(ChannelID queueID) throws JMSException {
034                JMSConnection con = JMSConnectionFactory.getInstance();
035                QueueBrowser qBrowser = con.createQueueBrowser(queueID);
036                Enumeration msgs = qBrowser.getEnumeration();
037                int count=0;
038                if ( !msgs.hasMoreElements() ) {
039                    //System.out.println("No messages in queue '" + queueID.getName() + "'"):
040                } else { 
041                    while (msgs.hasMoreElements()) { 
042                        Message tempMsg = (Message)msgs.nextElement(); 
043                        System.out.println("Message # " + count + ": "+ tempMsg);
044                        count++;
045                    }
046                }
047                con.cleanup();
048                qBrowser.close();
049                return count;
050        }
051        
052}