KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

70   public void setTopicName(String JavaDoc name)
71   {
72     setName(name);
73   }
74
75   /**
76    * Returns true for a topic.
77    */

78   public boolean isTopic()
79   {
80     return true;
81   }
82
83   /**
84    * Returns the JDBC id for the topic.
85    */

86   public int getId()
87   {
88     return _id;
89   }
90
91   /**
92    * Initializes the JdbcQueue
93    */

94   @PostConstruct
95   public void init()
96     throws ConfigException, SQLException JavaDoc
97   {
98     if (_jdbcManager.getDataSource() == null)
99       throw new ConfigException(L.l("JdbcTopic requires a <data-source> element."));
100     
101     if (getName() == null)
102       throw new ConfigException(L.l("JdbcTopic requires a <topic-name> element."));
103     
104     _jdbcManager.init();
105
106     _id = createDestination(getName(), true);
107   }
108
109   /**
110    * Creates a consumer.
111    */

112   public MessageConsumerImpl createConsumer(SessionImpl session,
113                         String JavaDoc selector,
114                         boolean noLocal)
115     throws JMSException JavaDoc
116   {
117     return new JdbcTopicConsumer(session, selector,
118                  _jdbcManager, this, noLocal);
119   }
120
121   /**
122    * Creates a durable subscriber.
123    */

124   public TopicSubscriber JavaDoc createDurableSubscriber(SessionImpl session,
125                          String JavaDoc selector,
126                          boolean noLocal,
127                          String JavaDoc name)
128     throws JMSException JavaDoc
129   {
130     return new JdbcTopicConsumer(session, selector,
131                  _jdbcManager, this, noLocal, name);
132   }
133
134   /**
135    * Sends the message to the queue.
136    */

137   public void send(Message JavaDoc message)
138     throws JMSException JavaDoc
139   {
140     long expireTime = message.getJMSExpiration();
141     if (expireTime <= 0)
142       expireTime = Long.MAX_VALUE / 2;
143
144     purgeExpiredMessages();
145     
146     try {
147       _jdbcManager.getJdbcMessage().send(message, _id, expireTime);
148     } catch (Exception JavaDoc e) {
149       throw new JMSExceptionWrapper(e);
150     }
151
152     messageAvailable();
153   }
154
155   /**
156    * Returns a printable view of the queue.
157    */

158   public String JavaDoc toString()
159   {
160     return "JdbcTopic[" + getName() + "]";
161   }
162 }
163
164
Popular Tags