KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > client > JBossProducer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.client;
8
9 import javax.jms.Destination JavaDoc;
10 import javax.jms.JMSException JavaDoc;
11 import javax.jms.Message JavaDoc;
12 import javax.jms.MessageProducer JavaDoc;
13 import javax.jms.Queue JavaDoc;
14 import javax.jms.QueueSender JavaDoc;
15 import javax.jms.Topic JavaDoc;
16 import javax.jms.TopicPublisher JavaDoc;
17
18 import org.jboss.jms.JMSValidator;
19 import org.jboss.jms.message.JBossMessage;
20
21 /**
22  * A producer
23  *
24  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
25  * @version $Revision: 1.4 $
26  */

27 public class JBossProducer
28    implements MessageProducer JavaDoc, QueueSender JavaDoc, TopicPublisher JavaDoc
29 {
30    // Constants -----------------------------------------------------
31

32    // Attributes ----------------------------------------------------
33

34    /** The producer delegate */
35    private ProducerDelegate delegate;
36
37    /** The default destination for this producer */
38    private Destination JavaDoc defaultDestination;
39
40    /** The default delivery mode */
41    private int defaultDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
42
43    /** The default priorty */
44    private int defaultPriority = Message.DEFAULT_PRIORITY;
45
46    /** The default time to live */
47    private long defaultTimeToLive = Message.DEFAULT_TIME_TO_LIVE;
48
49    /** The disable message id flag */
50    private boolean disableMessageID = false;
51
52    /** The disable message timestamp flag */
53    private boolean disableTimestamp = false;
54
55    // Static --------------------------------------------------------
56

57    // Constructors --------------------------------------------------
58

59    /**
60     * Create a new JBossProducer
61     *
62     * @param delegate the delegate
63     * @param destination the destination
64     * @throws JMSException for any error
65     */

66    public JBossProducer(ProducerDelegate delegate, Destination JavaDoc destination)
67       throws JMSException JavaDoc
68    {
69       this.delegate = delegate;
70       this.defaultDestination = destination;
71    }
72
73    // Public --------------------------------------------------------
74

75    // MessageProducer implementation --------------------------------
76

77     public void close() throws JMSException JavaDoc
78     {
79       delegate.closing();
80       delegate.close();
81     }
82
83     public int getDeliveryMode() throws JMSException JavaDoc
84     {
85       return defaultDeliveryMode;
86     }
87
88     public Destination JavaDoc getDestination() throws JMSException JavaDoc
89     {
90       return defaultDestination;
91     }
92
93     public boolean getDisableMessageID() throws JMSException JavaDoc
94     {
95       return disableMessageID;
96     }
97
98     public boolean getDisableMessageTimestamp() throws JMSException JavaDoc
99     {
100       return disableTimestamp;
101     }
102
103     public int getPriority() throws JMSException JavaDoc
104     {
105       return defaultPriority;
106     }
107
108     public long getTimeToLive() throws JMSException JavaDoc
109     {
110       return defaultTimeToLive;
111     }
112
113     public void send(Destination JavaDoc destination, Message JavaDoc message, int deliveryMode, int priority, long timeToLive)
114         throws JMSException JavaDoc
115     {
116       if (destination == null)
117          throw new JMSException JavaDoc("Null destination");
118       if (message == null)
119          throw new JMSException JavaDoc("Null message");
120       JMSValidator.validateDeliveryMode(deliveryMode);
121       JMSValidator.validatePriority(priority);
122       JMSValidator.validateTimeToLive(timeToLive);
123
124       JBossMessage msg;
125       if ((message instanceof JBossMessage))
126          msg = (JBossMessage) message;
127       else
128          msg = delegate.encapsulateMessage(message);
129
130       if (disableMessageID == false)
131          msg.generateMessageID();
132
133       if (disableTimestamp == false)
134          msg.generateTimestamp();
135
136       msg.setJMSDestination(destination);
137       msg.setJMSDeliveryMode(deliveryMode);
138       msg.setJMSPriority(priority);
139       if (disableTimestamp == false && timeToLive != 0)
140          msg.setJMSExpiration(msg.getJMSTimestamp() + timeToLive);
141
142       delegate.send(msg);
143     }
144
145     public void send(Destination JavaDoc destination, Message JavaDoc message) throws JMSException JavaDoc
146     {
147       send(destination, message, defaultDeliveryMode, defaultPriority, defaultTimeToLive);
148     }
149
150     public void send(Message JavaDoc message, int deliveryMode, int priority, long timeToLive) throws JMSException JavaDoc
151     {
152       send(defaultDestination, message, deliveryMode, priority, timeToLive);
153     }
154
155     public void send(Message JavaDoc message) throws JMSException JavaDoc
156     {
157       send(defaultDestination, message, defaultDeliveryMode, defaultPriority, defaultTimeToLive);
158     }
159
160     public void setDeliveryMode(int deliveryMode) throws JMSException JavaDoc
161     {
162       JMSValidator.validateDeliveryMode(deliveryMode);
163       this.defaultDeliveryMode = deliveryMode;
164     }
165
166     public void setDisableMessageID(boolean value) throws JMSException JavaDoc
167     {
168       this.disableMessageID = value;
169     }
170
171     public void setDisableMessageTimestamp(boolean value) throws JMSException JavaDoc
172     {
173       this.disableTimestamp = value;
174     }
175
176     public void setPriority(int defaultPriority) throws JMSException JavaDoc
177     {
178       JMSValidator.validatePriority(defaultPriority);
179       this.defaultPriority = defaultPriority;
180     }
181
182     public void setTimeToLive(long timeToLive) throws JMSException JavaDoc
183     {
184       JMSValidator.validateTimeToLive(timeToLive);
185       this.defaultTimeToLive = timeToLive;
186     }
187
188    // QueueReceiver implementation ----------------------------------
189

190    public Queue JavaDoc getQueue() throws JMSException JavaDoc
191    {
192       return (Queue JavaDoc) getDestination();
193    }
194
195    public void send(Queue JavaDoc queue, Message JavaDoc message, int deliveryMode, int priority, long timeToLive) throws JMSException JavaDoc
196    {
197       send(queue, message, deliveryMode, priority, timeToLive);
198    }
199
200    public void send(Queue JavaDoc queue, Message JavaDoc message) throws JMSException JavaDoc
201    {
202       send(queue, message);
203    }
204
205    // TopicPublisher implementation ---------------------------------
206

207     public Topic JavaDoc getTopic() throws JMSException JavaDoc
208     {
209       return (Topic JavaDoc) getDestination();
210     }
211
212     public void publish(Message JavaDoc message, int deliveryMode, int priority, long timeToLive) throws JMSException JavaDoc
213     {
214       send(defaultDestination, message, deliveryMode, priority, timeToLive);
215     }
216
217     public void publish(Message JavaDoc message) throws JMSException JavaDoc
218     {
219       send(message);
220     }
221
222     public void publish(Topic JavaDoc topic, Message JavaDoc message, int deliveryMode, int priority, long timeToLive)
223         throws JMSException JavaDoc
224     {
225       send(topic, message, deliveryMode, priority, timeToLive);
226     }
227
228     public void publish(Topic JavaDoc topic, Message JavaDoc message) throws JMSException JavaDoc
229     {
230       send(topic, message);
231     }
232
233    // Protected ------------------------------------------------------
234

235    // Package Private ------------------------------------------------
236

237    // Private --------------------------------------------------------
238

239    // Inner Classes --------------------------------------------------
240

241 }
242
Popular Tags