KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jms.jdbc;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.jdbc.JdbcMetaData;
34 import com.caucho.jms.AbstractDestination;
35 import com.caucho.util.Alarm;
36 import com.caucho.util.L10N;
37 import com.caucho.util.Log;
38
39 import javax.sql.DataSource JavaDoc;
40 import java.sql.Connection JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Represents a JDBC destination.
49  */

50 public class JdbcDestination extends AbstractDestination {
51   static final Logger JavaDoc log = Log.open(JdbcDestination.class);
52   static final L10N L = new L10N(JdbcDestination.class);
53   
54   protected JdbcManager _jdbcManager = new JdbcManager();
55   
56   private DataSource JavaDoc _dataSource;
57   
58   private String JavaDoc _name;
59   
60   private long _lastPurgeTime;
61
62   public JdbcDestination()
63   {
64   }
65
66   /**
67    * Sets the name.
68    */

69   public void setName(String JavaDoc name)
70   {
71     _name = name;
72   }
73
74   /**
75    * Gets the name.
76    */

77   public String JavaDoc getName()
78   {
79     return _name;
80   }
81
82   /**
83    * Returns true for a topic.
84    */

85   public boolean isTopic()
86   {
87     return false;
88   }
89
90   /**
91    * Sets the jdbc manager
92    */

93   public void setJdbcManager(JdbcManager jdbcManager)
94   {
95     _jdbcManager = jdbcManager;
96   }
97
98   /**
99    * Gets the JDBC manager.
100    */

101   public JdbcManager getJdbcManager()
102   {
103     return _jdbcManager;
104   }
105   
106   /**
107    * Sets the data source.
108    */

109   public void setDataSource(DataSource JavaDoc dataSource)
110   {
111     _jdbcManager.setDataSource(dataSource);
112   }
113
114   /**
115    * Sets the tablespace for Oracle.
116    */

117   public void setTablespace(String JavaDoc tablespace)
118   {
119     _jdbcManager.setTablespace(tablespace);
120   }
121
122   /**
123    * Initializes the JdbcQueue
124    */

125   public void init()
126     throws ConfigException, SQLException JavaDoc
127   {
128     _jdbcManager.init();
129     
130     _dataSource = _jdbcManager.getDataSource();
131   }
132
133   /**
134    * Creates a queue.
135    */

136   protected int createDestination(String JavaDoc name, boolean isTopic)
137     throws SQLException JavaDoc
138   {
139     Connection JavaDoc conn = _jdbcManager.getDataSource().getConnection();
140     String JavaDoc destinationTable = _jdbcManager.getDestinationTable();
141     String JavaDoc destinationSequence = _jdbcManager.getDestinationSequence();
142     
143     try {
144       String JavaDoc sql = ("SELECT id FROM " + destinationTable +
145             " WHERE name=? AND is_topic=?");
146       
147       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
148       pstmt.setString(1, name);
149       pstmt.setInt(2, isTopic ? 1 : 0);
150
151       ResultSet JavaDoc rs = pstmt.executeQuery();
152       if (rs.next()) {
153     return rs.getInt(1);
154       }
155       rs.close();
156
157       if (destinationSequence != null) {
158     JdbcMetaData metaData = _jdbcManager.getMetaData();
159     sql = metaData.selectSequenceSQL(destinationSequence);
160     int id = 0;
161     
162     pstmt = conn.prepareStatement(sql);
163
164     rs = pstmt.executeQuery();
165     if (rs.next())
166       id = rs.getInt(1);
167     else
168       throw new RuntimeException JavaDoc("can't create sequence");
169
170     sql = "INSERT INTO " + destinationTable + " (id,name,is_topic) VALUES(?,?,?)";
171
172     pstmt = conn.prepareStatement(sql);
173
174     pstmt.setInt(1, id);
175     pstmt.setString(2, name);
176     pstmt.setInt(3, isTopic ? 1 : 0);
177
178     pstmt.executeUpdate();
179
180     if (isTopic)
181       log.fine("JMSTopic[" + name + "," + id + "] created");
182     else
183       log.fine("JMSQueue[" + name + "," + id + "] created");
184
185     return id;
186       }
187       else {
188     sql = "INSERT INTO " + destinationTable + " (name,is_topic) VALUES(?,?)";
189     pstmt = conn.prepareStatement(sql,
190                       PreparedStatement.RETURN_GENERATED_KEYS);
191     pstmt.setString(1, name);
192     pstmt.setInt(2, isTopic ? 1 : 0);
193
194     pstmt.executeUpdate();
195
196     rs = pstmt.getGeneratedKeys();
197
198     if (rs.next()) {
199       int id = rs.getInt(1);
200
201       if (isTopic)
202         log.fine("JMSTopic[" + name + "," + id + "] created");
203       else
204         log.fine("JMSQueue[" + name + "," + id + "] created");
205     
206       return id;
207     }
208     else
209       throw new SQLException JavaDoc(L.l("can't generate destination for {0}",
210                      name));
211       }
212     } finally {
213       conn.close();
214     }
215   }
216
217   /**
218    * Purges expired messages.
219    */

220   protected void purgeExpiredMessages()
221   {
222     long purgeInterval = _jdbcManager.getPurgeInterval();
223     long now = Alarm.getCurrentTime();
224
225     if (now < _lastPurgeTime + purgeInterval)
226       return;
227
228     _lastPurgeTime = now;
229     
230     try {
231       DataSource JavaDoc dataSource = _jdbcManager.getDataSource();
232       String JavaDoc messageTable = _jdbcManager.getMessageTable();
233       JdbcMessage jdbcMessage = _jdbcManager.getJdbcMessage();
234     
235       Connection JavaDoc conn = dataSource.getConnection();
236       try {
237     String JavaDoc sql = ("DELETE FROM " + messageTable +
238               " WHERE expire < ? AND consumer IS NULL");
239
240     PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
241     pstmt.setLong(1, Alarm.getCurrentTime());
242     
243     int count = pstmt.executeUpdate();
244
245     if (count > 0)
246       log.fine("JMSQueue[" + getName() + "] purged " + count + " expired mesages");
247
248     pstmt.close();
249       } finally {
250     conn.close();
251       }
252     } catch (Exception JavaDoc e) {
253       log.log(Level.FINER, e.toString(), e);
254     }
255   }
256 }
257
258
Popular Tags