KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > JMSProducer


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jms;
8
9 import javax.jms.DeliveryMode JavaDoc;
10 import javax.jms.Destination JavaDoc;
11 import javax.jms.JMSException JavaDoc;
12 import javax.jms.Message JavaDoc;
13 import javax.jms.MessageProducer JavaDoc;
14 import javax.jms.Queue JavaDoc;
15 import javax.jms.QueueSender JavaDoc;
16 import javax.jms.Topic JavaDoc;
17 import javax.jms.TopicPublisher JavaDoc;
18
19 import org.jfox.ioc.util.UUID;
20
21 /**
22  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
23  */

24
25 public class JMSProducer implements MessageProducer JavaDoc, QueueSender JavaDoc, TopicPublisher JavaDoc {
26     /**
27      * The default delivery mode
28      */

29     private int deliveryMode = Message.DEFAULT_DELIVERY_MODE;
30     /**
31      * The default priorty
32      */

33     private int priority = Message.DEFAULT_PRIORITY;
34
35     private long timeToLive = Message.DEFAULT_TIME_TO_LIVE;
36
37     /**
38      * The disable message id flag
39      */

40     private boolean disableMessageID = false;
41     /**
42      * The disable message timestamp flag
43      */

44     private boolean disableTimestamp = false;
45
46     private Destination JavaDoc destination;
47     private JMSSession session;
48
49     private boolean closed = false;
50
51     public JMSProducer(JMSSession session, Destination JavaDoc destination) {
52         this.session = session;
53         this.destination = destination;
54     }
55
56     public void setDisableMessageID(boolean disableMessageID) throws JMSException JavaDoc {
57         checkClosed();
58         this.disableMessageID = disableMessageID;
59     }
60
61     public boolean getDisableMessageID() throws JMSException JavaDoc {
62         return disableMessageID;
63     }
64
65     public void setDisableMessageTimestamp(boolean disableMessageTimestamp) throws JMSException JavaDoc {
66         checkClosed();
67         this.disableTimestamp = disableMessageTimestamp;
68     }
69
70     public boolean getDisableMessageTimestamp() throws JMSException JavaDoc {
71         return disableTimestamp;
72     }
73
74     public void setDeliveryMode(int deliveryMode) throws JMSException JavaDoc {
75         checkClosed();
76         validateDeliveryMode(deliveryMode);
77         this.deliveryMode = deliveryMode;
78     }
79
80     public int getDeliveryMode() throws JMSException JavaDoc {
81         return deliveryMode;
82     }
83
84     public void setPriority(int priority) throws JMSException JavaDoc {
85         checkClosed();
86         validatePriority(priority);
87         this.priority = priority;
88     }
89
90     public int getPriority() throws JMSException JavaDoc {
91         return priority;
92     }
93
94     public void setTimeToLive(long timeToLive) throws JMSException JavaDoc {
95         checkClosed();
96         this.timeToLive = timeToLive;
97     }
98
99     public long getTimeToLive() throws JMSException JavaDoc {
100         return timeToLive;
101     }
102
103     public Destination JavaDoc getDestination() throws JMSException JavaDoc {
104         return destination;
105     }
106
107     public void close() throws JMSException JavaDoc {
108         this.closed = true;
109     }
110
111     public void send(Message JavaDoc message) throws JMSException JavaDoc {
112         send(destination, message, deliveryMode, priority, timeToLive);
113     }
114
115     public void send(Message JavaDoc message, int deliveryMode, int priority, long timeToLive) throws JMSException JavaDoc {
116         send(destination, message, deliveryMode, priority, timeToLive);
117     }
118
119     public void send(Destination JavaDoc destination, Message JavaDoc message) throws JMSException JavaDoc {
120         send(destination, message, deliveryMode, priority, timeToLive);
121     }
122
123     public void send(Destination JavaDoc destination, Message JavaDoc message, int deliveryMode, int priority, long timeToLive) throws JMSException JavaDoc {
124         checkClosed();
125         if (destination == null)
126             throw new JMSException JavaDoc("Null destination");
127         if (message == null)
128             throw new JMSException JavaDoc("Null message");
129         if (disableMessageID == false) {
130             message.setJMSMessageID("ID:" + UUID.randomUUID().toString());
131         }
132
133         if (disableTimestamp == false) {
134             message.setJMSTimestamp(System.currentTimeMillis());
135         }
136
137         message.setJMSDestination(destination);
138         message.setJMSDeliveryMode(deliveryMode);
139         message.setJMSPriority(priority);
140
141         if (disableTimestamp == false && timeToLive != 0) {
142             message.setJMSExpiration(message.getJMSTimestamp() + timeToLive);
143         }
144         session.sendMessage(message);
145 // session.getJMSConnection().getContainer().sendMessage(null);
146
}
147
148     public Queue JavaDoc getQueue() throws JMSException JavaDoc {
149         return (Queue JavaDoc) destination;
150     }
151
152     public void send(Queue JavaDoc queue, Message JavaDoc message) throws JMSException JavaDoc {
153         send(queue, message, deliveryMode, priority, timeToLive);
154     }
155
156     public void send(Queue JavaDoc queue, Message JavaDoc message, int deliveryMode, int priority, long timeToLive) throws JMSException JavaDoc {
157         send((Destination JavaDoc) queue, message, deliveryMode, priority, timeToLive);
158     }
159
160     public Topic JavaDoc getTopic() throws JMSException JavaDoc {
161         return (Topic JavaDoc) destination;
162     }
163
164     public void publish(Message JavaDoc message) throws JMSException JavaDoc {
165         send(message);
166     }
167
168     public void publish(Message JavaDoc message, int deliveryMode, int priority, long timeToLive) throws JMSException JavaDoc {
169         send(destination, message, deliveryMode, priority, timeToLive);
170     }
171
172     public void publish(Topic JavaDoc topic, Message JavaDoc message) throws JMSException JavaDoc {
173         send(topic, message);
174     }
175
176     public void publish(Topic JavaDoc topic, Message JavaDoc message, int deliveryMode, int priority, long timeToLive) throws JMSException JavaDoc {
177         send((Destination JavaDoc) topic, message, deliveryMode, priority, timeToLive);
178     }
179
180     /**
181      * validate the deliveryMode
182      *
183      * @param deliveryMode
184      * @throws JMSException
185      */

186     static void validateDeliveryMode(int deliveryMode) throws JMSException JavaDoc {
187         if (deliveryMode != DeliveryMode.NON_PERSISTENT &&
188                 deliveryMode != DeliveryMode.PERSISTENT)
189             throw new JMSException JavaDoc("Invalid delivery mode " + deliveryMode);
190     }
191
192     /**
193      * Validate the priority
194      */

195     static void validatePriority(int priority) throws JMSException JavaDoc {
196         if (priority < 0 || priority > 9) {
197             throw new JMSException JavaDoc("Invalid priority " + priority);
198         }
199     }
200
201     private void checkClosed() throws javax.jms.IllegalStateException JavaDoc {
202         if (closed) {
203             throw new javax.jms.IllegalStateException JavaDoc("MessageProducer closed");
204         }
205     }
206
207     public static void main(String JavaDoc[] args) {
208
209     }
210 }
211
Popular Tags