KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > resource > MessageSenderResource


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 Software Foundation, 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.resource;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.jms.AbstractDestination;
34 import com.caucho.jms.ConnectionFactoryImpl;
35 import com.caucho.services.message.MessageSender;
36 import com.caucho.services.message.MessageServiceException;
37 import com.caucho.util.L10N;
38 import com.caucho.util.Log;
39
40 import javax.annotation.*;
41 import javax.jms.*;
42 import java.util.HashMap JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * Configures message senders, avoiding JCA.
47  */

48 public class MessageSenderResource implements MessageSender {
49   private static final L10N L = new L10N(MessageSenderResource.class);
50   private static final Logger JavaDoc log = Log.open(MessageSenderResource.class);
51
52   private ConnectionFactory _connFactory;
53   private Connection _conn;
54   private Destination _destination;
55
56   /**
57    * Sets the JMS connection factory.
58    *
59    * @param factory
60    */

61   public void setConnectionFactory(ConnectionFactory factory)
62   {
63     _connFactory = factory;
64   }
65
66   /**
67    * Sets the JMS Destination (Queue or Topic)
68    *
69    * @param destination
70    */

71   public void setDestination(Destination destination)
72   {
73     _destination = destination;
74   }
75
76   /**
77    * Initialize the sender resource.
78    *
79    * @throws JMSException
80    * @throws ConfigException
81    */

82   @PostConstruct
83   public void init() throws JMSException, ConfigException
84   {
85     if (_destination == null)
86       throw new ConfigException(L.l("'destination' required for message sender."));
87
88     if (_connFactory == null && _destination instanceof AbstractDestination)
89       _connFactory = new ConnectionFactoryImpl();
90
91     if (_connFactory == null)
92       throw new ConfigException(L.l("'connection-factory' required for message sender"));
93
94     _conn = _connFactory.createConnection();
95   }
96
97   /**
98    * Sends a message to the destination
99    *
100    * @param header
101    * @param value
102    * @throws MessageServiceException
103    */

104   public void send(HashMap JavaDoc header, Object JavaDoc value)
105     throws MessageServiceException
106   {
107     try {
108       Session session = getSession();
109
110       try {
111         Message message;
112
113         if (value == null) {
114       message = session.createMessage();
115         }
116         else if (value instanceof String JavaDoc) {
117       message = session.createTextMessage((String JavaDoc) value);
118         }
119         else if (value instanceof java.io.Serializable JavaDoc) {
120       ObjectMessage objMessage = session.createObjectMessage();
121       objMessage.setObject((java.io.Serializable JavaDoc) value);
122       message = objMessage;
123         }
124         else {
125       throw new MessageServiceException(L.l("value '{0}' must be serializable",
126                           value));
127         }
128
129         MessageProducer producer = session.createProducer(_destination);
130
131         producer.send(message);
132
133         producer.close();
134       } finally {
135         session.close();
136       }
137     } catch (MessageServiceException e) {
138       throw e;
139     } catch (Exception JavaDoc e) {
140       throw new MessageServiceException(e);
141     }
142   }
143
144   /**
145    * Returns the JMS session.
146    */

147   private Session getSession() throws JMSException
148   {
149     return _conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
150   }
151 }
152
153
Popular Tags