KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > jdbc > JdbcQueueBrowser


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.jdbc;
30
31 import com.caucho.jms.selector.Selector;
32 import com.caucho.jms.selector.SelectorParser;
33 import com.caucho.jms.session.SessionImpl;
34 import com.caucho.log.Log;
35 import com.caucho.util.Alarm;
36 import com.caucho.util.L10N;
37
38 import javax.jms.JMSException JavaDoc;
39 import javax.jms.Message JavaDoc;
40 import javax.jms.Queue JavaDoc;
41 import javax.jms.QueueBrowser JavaDoc;
42 import java.sql.Connection JavaDoc;
43 import java.sql.PreparedStatement JavaDoc;
44 import java.sql.ResultSet JavaDoc;
45 import java.util.Enumeration JavaDoc;
46 import java.util.logging.Level JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48
49 /**
50  * Represents a JDBC queue browser.
51  */

52 public class JdbcQueueBrowser implements QueueBrowser JavaDoc {
53   static final Logger JavaDoc log = Log.open(JdbcQueueBrowser.class);
54   static final L10N L = new L10N(JdbcQueueBrowser.class);
55
56   private SessionImpl _session;
57   private JdbcManager _jdbcManager;
58
59   private JdbcQueue _queue;
60
61   private String JavaDoc _messageSelector;
62   private Selector _selector;
63   
64   private boolean _isClosed;
65
66   public JdbcQueueBrowser(SessionImpl session, String JavaDoc messageSelector,
67               JdbcQueue queue)
68     throws JMSException JavaDoc
69   {
70     _session = session;
71     _jdbcManager = queue.getJdbcManager();
72     _queue = queue;
73
74     _messageSelector = messageSelector;
75     if (_messageSelector != null) {
76       SelectorParser parser = new SelectorParser();
77       _selector = parser.parse(messageSelector);
78     }
79   }
80
81   /**
82    * Returns the queue.
83    */

84   public Queue JavaDoc getQueue()
85   {
86     return _queue;
87   }
88
89   /**
90    * Returns the message selector.
91    */

92   public String JavaDoc getMessageSelector()
93   {
94     return _messageSelector;
95   }
96
97   /**
98    * Returns an enumeration of the entries.
99    */

100   public Enumeration JavaDoc getEnumeration()
101   {
102     return new BrowserEnumeration();
103   }
104
105   /**
106    * Closes the consumer.
107    */

108   public void close()
109     throws JMSException JavaDoc
110   {
111     if (_isClosed)
112       return;
113     _isClosed = true;
114   }
115
116   /**
117    * Returns a printable view of the queue.
118    */

119   public String JavaDoc toString()
120   {
121     return "JdbcQueueBrowser[" + _queue + "]";
122   }
123
124   class BrowserEnumeration implements Enumeration JavaDoc {
125     private long _maxId = -1;
126     private Message JavaDoc _msg;
127
128     public boolean hasMoreElements()
129     {
130       if (_msg == null)
131     getNextMessage();
132
133       return _msg != null;
134     }
135
136     public Object JavaDoc nextElement()
137     {
138       if (_msg == null)
139     getNextMessage();
140
141       Message JavaDoc msg = _msg;
142       _msg = null;
143       
144       return msg;
145     }
146
147     /**
148      * Gets the next message matching the selector.
149      */

150     private Message JavaDoc getNextMessage()
151     {
152       try {
153     Connection JavaDoc conn = _jdbcManager.getDataSource().getConnection();
154     String JavaDoc messageTable = _jdbcManager.getMessageTable();
155
156     try {
157       String JavaDoc sql = ("SELECT m_id, msg_type, delivered, body, header" +
158             " FROM " + messageTable +
159             " WHERE ?<m_id AND queue=?" +
160             " AND consumer IS NULL AND ?<expire");
161       
162       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
163       pstmt.setLong(1, _maxId);
164       pstmt.setInt(2, _queue.getId());
165       pstmt.setLong(3, Alarm.getCurrentTime());
166
167       ResultSet JavaDoc rs = pstmt.executeQuery();
168       while (rs.next()) {
169         _maxId = rs.getLong(1);
170
171         _msg = _jdbcManager.getJdbcMessage().readMessage(rs);
172
173         if (_selector == null || _selector.isMatch(_msg))
174           break;
175         else
176           _msg = null;
177       }
178
179       rs.close();
180       pstmt.close();
181     } finally {
182       conn.close();
183     }
184       } catch (Throwable JavaDoc e) {
185     log.log(Level.WARNING, e.toString(), e);
186       }
187
188       return _msg;
189     }
190   }
191 }
192
193
Popular Tags