KickJava   Java API By Example, From Geeks To Geeks.

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


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.jms.JMSExceptionWrapper;
34 import com.caucho.jms.session.MessageConsumerImpl;
35 import com.caucho.jms.session.SessionImpl;
36 import com.caucho.log.Log;
37 import com.caucho.util.L10N;
38
39 import javax.annotation.PostConstruct;
40 import javax.jms.JMSException JavaDoc;
41 import javax.jms.Message JavaDoc;
42 import javax.jms.Queue JavaDoc;
43 import javax.jms.QueueBrowser JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * A jdbc queue.
49  */

50 public class JdbcQueue extends JdbcDestination implements Queue JavaDoc {
51   static final Logger JavaDoc log = Log.open(JdbcQueue.class);
52   static final L10N L = new L10N(JdbcQueue.class);
53
54   private int _id;
55
56   public JdbcQueue()
57   {
58   }
59
60   /**
61    * Returns the queue's name.
62    */

63   public String JavaDoc getQueueName()
64   {
65     return getName();
66   }
67
68   /**
69    * Sets the queue's name.
70    */

71   public void setQueueName(String JavaDoc name)
72   {
73     setName(name);
74   }
75
76   /**
77    * Returns the JDBC id for the queue.
78    */

79   public int getId()
80   {
81     return _id;
82   }
83
84   /**
85    * Initializes the JdbcQueue
86    */

87   @PostConstruct
88   public void init()
89     throws ConfigException, SQLException JavaDoc
90   {
91     if (_jdbcManager.getDataSource() == null)
92       throw new ConfigException(L.l("JdbcQueue requires a <data-source> element."));
93     
94     if (getName() == null)
95       throw new ConfigException(L.l("JdbcQueue requires a <queue-name> element."));
96     
97     _jdbcManager.init();
98
99     _id = createDestination(getName(), false);
100   }
101
102   /**
103    * Creates a consumer.
104    */

105   public MessageConsumerImpl createConsumer(SessionImpl session,
106                         String JavaDoc selector,
107                         boolean noWait)
108     throws JMSException JavaDoc
109   {
110     return new JdbcQueueConsumer(session, selector, _jdbcManager, this);
111   }
112
113   /**
114    * Creates a browser.
115    */

116   public QueueBrowser JavaDoc createBrowser(SessionImpl session, String JavaDoc selector)
117     throws JMSException JavaDoc
118   {
119     return new JdbcQueueBrowser(session, selector, this);
120   }
121
122   /**
123    * Sends the message to the queue.
124    */

125   public void send(Message JavaDoc message)
126     throws JMSException JavaDoc
127   {
128     long expireTime = message.getJMSExpiration();
129     if (expireTime <= 0)
130       expireTime = Long.MAX_VALUE / 2;
131
132     purgeExpiredMessages();
133     
134     try {
135       _jdbcManager.getJdbcMessage().send(message, _id, expireTime);
136     } catch (Exception JavaDoc e) {
137       throw new JMSExceptionWrapper(e);
138     }
139
140     messageAvailable();
141   }
142
143   /**
144    * Removes the first message matching the selector.
145    */

146   public void commit(int session)
147     throws JMSException JavaDoc
148   {
149   }
150
151   /**
152    * Returns a printable view of the queue.
153    */

154   public String JavaDoc toString()
155   {
156     return "JdbcQueue[" + getName() + "]";
157   }
158 }
159
160
Popular Tags