KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > filter > AmqMessagesQueryFilter


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.console.filter;
19
20 import org.apache.activemq.command.ActiveMQTopic;
21 import org.apache.activemq.command.ActiveMQQueue;
22 import org.apache.activemq.ActiveMQConnectionFactory;
23
24 import javax.jms.Destination JavaDoc;
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.QueueBrowser JavaDoc;
29 import java.net.URI JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 public class AmqMessagesQueryFilter extends AbstractQueryFilter {
35
36     private URI JavaDoc brokerUrl;
37     private Destination JavaDoc destination;
38
39     /**
40      * Create a JMS message query filter
41      * @param brokerUrl - broker url to connect to
42      * @param destination - JMS destination to query
43      */

44     public AmqMessagesQueryFilter(URI JavaDoc brokerUrl, Destination JavaDoc destination) {
45         super(null);
46         this.brokerUrl = brokerUrl;
47         this.destination = destination;
48     }
49
50     /**
51      * Queries the specified destination using the message selector format query
52      * @param queries - message selector queries
53      * @return list messages that matches the selector
54      * @throws Exception
55      */

56     public List JavaDoc query(List JavaDoc queries) throws Exception JavaDoc {
57         String JavaDoc selector = "";
58
59         // Convert to message selector
60
for (Iterator JavaDoc i=queries.iterator(); i.hasNext();) {
61             selector = selector + "(" + i.next().toString() + ") AND ";
62         }
63
64         // Remove last AND
65
if (selector != "") {
66             selector = selector.substring(0, selector.length() - 5);
67         }
68
69         if (destination instanceof ActiveMQQueue) {
70             return queryMessages((ActiveMQQueue)destination, selector);
71         } else {
72             return queryMessages((ActiveMQTopic)destination, selector);
73         }
74     }
75
76     /**
77      * Query the messages of a queue destination using a queue browser
78      * @param queue - queue destination
79      * @param selector - message selector
80      * @return list of messages that matches the selector
81      * @throws Exception
82      */

83     protected List JavaDoc queryMessages(ActiveMQQueue queue, String JavaDoc selector) throws Exception JavaDoc {
84         Connection JavaDoc conn = createConnection(getBrokerUrl());
85
86         Session JavaDoc sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
87         QueueBrowser JavaDoc browser = sess.createBrowser(queue, selector);
88
89         List JavaDoc messages = Collections.list(browser.getEnumeration());
90
91         conn.close();
92
93         return messages;
94     }
95
96     /**
97      * Query the messages of a topic destination using a message consumer
98      * @param topic - topic destination
99      * @param selector - message selector
100      * @return list of messages that matches the selector
101      * @throws Exception
102      */

103     protected List JavaDoc queryMessages(ActiveMQTopic topic, String JavaDoc selector) throws Exception JavaDoc {
104         // TODO: should we use a durable subscriber or a retroactive non-durable subscriber?
105
// TODO: if a durable subscriber is used, how do we manage it? subscribe/unsubscribe tasks?
106
return null;
107     }
108
109     /**
110      * Create and start a JMS connection
111      * @param brokerUrl - broker url to connect to.
112      * @return JMS connection
113      * @throws JMSException
114      */

115     protected Connection JavaDoc createConnection(URI JavaDoc brokerUrl) throws JMSException JavaDoc {
116         Connection JavaDoc conn = (new ActiveMQConnectionFactory(brokerUrl)).createConnection();
117         conn.start();
118         return conn;
119     }
120
121     /**
122      * Get the broker url being used.
123      * @return broker url
124      */

125     public URI JavaDoc getBrokerUrl() {
126         return brokerUrl;
127     }
128
129     /**
130      * Set the broker url to use.
131      * @param brokerUrl - broker url
132      */

133     public void setBrokerUrl(URI JavaDoc brokerUrl) {
134         this.brokerUrl = brokerUrl;
135     }
136
137     /**
138      * Get the destination being used.
139      * @return - JMS destination
140      */

141     public Destination JavaDoc getDestination() {
142         return destination;
143     }
144
145     /**
146      * Set the destination to use.
147      * @param destination - JMS destination
148      */

149     public void setDestination(Destination JavaDoc destination) {
150         this.destination = destination;
151     }
152
153 }
154
Popular Tags