KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > amq > AmqProducer


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 SoftwareFoundation, 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.amq;
31
32 import com.caucho.jms.JMSExceptionWrapper;
33 import com.caucho.jms.session.MessageProducerImpl;
34 import com.caucho.jms.session.SessionImpl;
35 import com.caucho.util.L10N;
36
37 import javax.jms.Destination JavaDoc;
38 import javax.jms.JMSException JavaDoc;
39 import javax.jms.Message JavaDoc;
40 import javax.jms.MessageProducer JavaDoc;
41 import javax.jms.TextMessage JavaDoc;
42 import java.io.ByteArrayInputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45
46 /**
47  * A basic message producer.
48  */

49 public class AmqProducer extends MessageProducerImpl
50 {
51   private static final L10N L = new L10N(MessageProducer JavaDoc.class);
52
53   protected AmqQueue _queue;
54   private AmqClientChannel _channel;
55
56   public AmqProducer(SessionImpl session, AmqQueue queue)
57   {
58     super(session, queue);
59
60     _queue = queue;
61   }
62   
63   /**
64    * Sends a message to the destination
65    *
66    * @param destination the destination the message should be send to
67    * @param message the message to send
68    * @param deliveryMode the delivery mode
69    * @param priority the priority
70    * @param timeToLive how long the message should live
71    */

72   public void send(Destination JavaDoc destination,
73                    Message JavaDoc message,
74                    int deliveryMode,
75                    int priority,
76                    long timeToLive)
77     throws JMSException JavaDoc
78   {
79     /*
80     if (destination != _queue)
81       throw new UnsupportedOperationException("can't handle non-local " + destination + " " + _queue);
82     */

83
84     try {
85       AmqClientChannel channel = getChannel();
86
87       TextMessage JavaDoc msg = (TextMessage JavaDoc) message;
88
89       byte []data = msg.getText().getBytes();
90     
91       channel.publish(data.length, new ByteArrayInputStream JavaDoc(data));
92     } catch (IOException JavaDoc e) {
93       throw new JMSExceptionWrapper(e);
94     }
95   }
96
97   private InputStream JavaDoc messageToInputStream(Message JavaDoc message)
98   {
99     return null;
100   }
101   
102   private AmqClientChannel getChannel()
103     throws IOException JavaDoc
104   {
105     if (_channel == null) {
106       _channel = _queue.openChannel();
107     }
108     
109     return _channel;
110   }
111
112   /**
113    * Closes the producer.
114    */

115   public void close()
116     throws JMSException JavaDoc
117   {
118     AmqChannel channel = _channel;
119     _channel = null;
120
121     if (channel != null)
122       channel.close();
123   }
124 }
125
126
Popular Tags