001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jms.resource;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.jms.queue.AbstractDestination;
034: import com.caucho.jms.JmsConnectionFactory;
035: import com.caucho.services.message.MessageSender;
036: import com.caucho.services.message.MessageServiceException;
037: import com.caucho.util.L10N;
038: import com.caucho.util.Log;
039:
040: import javax.annotation.*;
041: import javax.jms.*;
042: import java.util.HashMap;
043: import java.util.logging.Logger;
044:
045: /**
046: * Configures message senders, avoiding JCA.
047: */
048: public class MessageSenderResource implements MessageSender {
049: private static final L10N L = new L10N(MessageSenderResource.class);
050: private static final Logger log = Logger
051: .getLogger(MessageSenderResource.class.getName());
052:
053: private ConnectionFactory _connFactory;
054: private Connection _conn;
055: private Destination _destination;
056:
057: public MessageSenderResource() {
058: }
059:
060: /**
061: * Sets the JMS connection factory.
062: *
063: * @param factory
064: */
065: public void setConnectionFactory(ConnectionFactory factory) {
066: _connFactory = factory;
067: }
068:
069: /**
070: * Sets the JMS Destination (Queue or Topic)
071: *
072: * @param destination
073: */
074: public void setDestination(Destination destination) {
075: _destination = destination;
076: }
077:
078: /**
079: * Initialize the sender resource.
080: *
081: * @throws JMSException
082: * @throws ConfigException
083: */
084: @PostConstruct
085: public void init() throws JMSException, ConfigException {
086: if (_destination == null)
087: throw new ConfigException(L
088: .l("'destination' required for message sender."));
089:
090: if (_connFactory == null
091: && _destination instanceof AbstractDestination)
092: _connFactory = new JmsConnectionFactory();
093:
094: if (_connFactory == null)
095: throw new ConfigException(
096: L
097: .l("'connection-factory' required for message sender"));
098:
099: _conn = _connFactory.createConnection();
100:
101: if (_conn == null)
102: throw new NullPointerException();
103: }
104:
105: /**
106: * Sends a message to the destination
107: *
108: * @param header
109: * @param value
110: * @throws MessageServiceException
111: */
112: public void send(HashMap header, Object value)
113: throws MessageServiceException {
114: try {
115: Session session = getSession();
116:
117: try {
118: Message message;
119:
120: if (value == null) {
121: message = session.createMessage();
122: } else if (value instanceof String) {
123: message = session.createTextMessage((String) value);
124: } else if (value instanceof java.io.Serializable) {
125: ObjectMessage objMessage = session
126: .createObjectMessage();
127: objMessage.setObject((java.io.Serializable) value);
128: message = objMessage;
129: } else {
130: throw new MessageServiceException(L.l(
131: "value '{0}' must be serializable", value));
132: }
133:
134: MessageProducer producer = session
135: .createProducer(_destination);
136:
137: producer.send(message);
138:
139: producer.close();
140: } finally {
141: session.close();
142: }
143: } catch (MessageServiceException e) {
144: throw e;
145: } catch (Exception e) {
146: throw new MessageServiceException(e);
147: }
148: }
149:
150: /**
151: * Returns the JMS session.
152: */
153: private Session getSession() throws JMSException {
154: return _conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
155: }
156: }
|