1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: AbstractMessageSender.java,v 1.4 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import javax.jms.Destination;
48 import javax.jms.JMSException;
49 import javax.jms.Message;
50 import javax.jms.MessageProducer;
51 import javax.jms.QueueSender;
52 import javax.jms.TopicPublisher;
53
54
55 /***
56 * Helper that implements behaviour common to both QueueMessageSender and
57 * TopicMessageSender
58 *
59 * @version $Revision: 1.4 $ $Date: 2004/01/31 13:44:24 $
60 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
61 * @see MessageSender
62 * @see QueueMessageSender
63 * @see TopicMessageSender
64 */
65 abstract class AbstractMessageSender implements MessageSender {
66
67 /***
68 * The producer used to send messages
69 */
70 private MessageProducer _producer = null;
71
72 /***
73 * The messaging behaviour
74 */
75 private MessagingBehaviour _behaviour = null;
76
77 /***
78 * Construct an instance with the producer to send messages with
79 *
80 * @param producer the producer used to send messages
81 * @param behaviour the messaging behaviour
82 */
83 public AbstractMessageSender(MessageProducer producer,
84 MessagingBehaviour behaviour) {
85 if (producer == null) {
86 throw new IllegalArgumentException("Argument producer is null");
87 }
88 if (behaviour == null) {
89 throw new IllegalArgumentException("Argument behaviour is null");
90 }
91 _producer = producer;
92 _behaviour = behaviour;
93 }
94
95 /***
96 * Send messages to a destination
97 *
98 * @param message the message to send
99 * @param count the number of times to send the message
100 * @throws JMSException if any of the JMS operations fail
101 */
102 public void send(Message message, int count) throws JMSException {
103 send(message, count, null, getProducer().getTimeToLive());
104 }
105
106 /***
107 * Send messages to a destination
108 *
109 * @param message the message to send
110 * @param count the number of times to send the message
111 * @param timeToLive the message time-to-live
112 * @throws JMSException if any of the JMS operations fail
113 */
114 public void send(Message message, int count, long timeToLive)
115 throws JMSException {
116 send(message, count, null, timeToLive);
117 }
118
119 /***
120 * Send messages to a destination, invoking a message populator prior
121 * to each message being sent
122 *
123 * @param message the message to send
124 * @param count the number of times to send the message
125 * @param populator the message populator, or null, if no population is
126 * required
127 * @throws JMSException if any of the JMS operations fail
128 */
129 public void send(Message message, int count, MessagePopulator populator)
130 throws JMSException {
131 send(message, count, populator, getProducer().getTimeToLive());
132 }
133
134 /***
135 * Return the destination associated with the MessageProducer
136 *
137 * @return the destination to receive messages from
138 * @throws JMSException if the operation fails
139 */
140 public Destination getDestination() throws JMSException {
141 Destination result = null;
142 if (_producer instanceof QueueSender) {
143 result = ((QueueSender) _producer).getQueue();
144 } else {
145 result = ((TopicPublisher) _producer).getTopic();
146 }
147 return result;
148 }
149
150 /***
151 * Close the underlying MessageProducer
152 *
153 * @throws JMSException if the operation fails
154 */
155 public void close() throws JMSException {
156 if (_producer != null) {
157 _producer.close();
158 _producer = null;
159 }
160 }
161
162 /***
163 * Returns the underlying message producer
164 *
165 * @return the underlying message producer
166 */
167 protected MessageProducer getProducer() {
168 return _producer;
169 }
170
171 /***
172 * Returns the messaging behaviour
173 *
174 * @return the messaging behaviour
175 */
176 protected MessagingBehaviour getBehaviour() {
177 return _behaviour;
178 }
179
180 }
This page was automatically generated by Maven