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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jms;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.management.j2ee.J2EEManagedObject;
033: import com.caucho.management.j2ee.JMSResource;
034: import com.caucho.jms.memory.*;
035: import com.caucho.jms.message.*;
036: import com.caucho.jms.connection.*;
037: import com.caucho.jms.connection.ConnectionFactoryImpl;
038: import com.caucho.util.L10N;
039:
040: import javax.jms.*;
041: import javax.sql.DataSource;
042: import java.sql.SQLException;
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.logging.Logger;
048: import java.util.logging.Level;
049:
050: /**
051: * JMS facade
052: */
053: public class Jms {
054: private static final Logger log = Logger.getLogger(Jms.class
055: .getName());
056: private static final L10N L = new L10N(Jms.class);
057:
058: private ConnectionFactoryImpl _connectionFactory;
059: private Connection _conn;
060:
061: private MessageFactory _messageFactory = new MessageFactory();
062:
063: public Jms() {
064: try {
065: _connectionFactory = new ConnectionFactoryImpl();
066: _conn = _connectionFactory.createConnection();
067: _conn.start();
068: } catch (JMSException e) {
069: throw new JmsRuntimeException(e);
070: }
071: }
072:
073: public void send(Destination dest, Message msg) {
074: Session session = null;
075:
076: try {
077: session = getSession();
078:
079: MessageProducer producer = session.createProducer(null);
080:
081: producer.send(dest, msg);
082: } catch (JMSException e) {
083: } finally {
084: freeSession(session);
085: }
086: }
087:
088: public Message receive(Destination dest) {
089: Session session = null;
090: MessageConsumer consumer = null;
091:
092: try {
093: session = getSession();
094:
095: consumer = session.createConsumer(dest);
096:
097: return consumer.receive();
098: } catch (JMSException e) {
099: throw new RuntimeException(e);
100: } finally {
101: try {
102: if (consumer != null)
103: consumer.close();
104: } catch (JMSException e) {
105: log.log(Level.FINE, e.toString(), e);
106: }
107:
108: freeSession(session);
109: }
110: }
111:
112: /**
113: * Creates an auto-ack session.
114: */
115: public Session createSession() throws JmsRuntimeException {
116: try {
117: return _conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
118: } catch (JMSException e) {
119: throw new JmsRuntimeException(e);
120: }
121: }
122:
123: public Connection createConnection() {
124: try {
125: return _connectionFactory.createConnection();
126: } catch (JMSException e) {
127: throw new JmsRuntimeException(e);
128: }
129: }
130:
131: /**
132: * Creates a session and listener.
133: */
134: public Session createListener(Connection conn, Destination queue,
135: MessageListener listener) throws JmsRuntimeException {
136: try {
137: Session session = conn.createSession(false,
138: Session.AUTO_ACKNOWLEDGE);
139:
140: MessageConsumer consumer = session.createConsumer(queue);
141:
142: consumer.setMessageListener(listener);
143:
144: return session;
145: } catch (JMSException e) {
146: throw new JmsRuntimeException(e);
147: }
148: }
149:
150: private Session getSession() throws JMSException {
151: Session session = _conn.createSession(false,
152: Session.AUTO_ACKNOWLEDGE);
153:
154: return session;
155: }
156:
157: private void freeSession(Session session) {
158: try {
159: if (session != null)
160: session.close();
161: } catch (JMSException e) {
162: throw new JmsRuntimeException(e);
163: }
164: }
165:
166: public TextMessage createTextMessage(String msg) {
167: try {
168: return _messageFactory.createTextMessage(msg);
169: } catch (JMSException e) {
170: throw new JmsRuntimeException(e);
171: }
172: }
173: }
|