001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jmsra.bean;
023:
024: import javax.ejb.MessageDrivenBean;
025: import javax.ejb.MessageDrivenContext;
026: import javax.ejb.EJBException;
027:
028: import javax.naming.*;
029: import javax.jms.*;
030:
031: import org.apache.log4j.Category;
032:
033: /**
034: * Listen on a topic, send to queue
035: *
036: *
037: * Created: Sat Nov 25 18:07:50 2000
038: *
039: * @author Peter Antman Tim <peter.antman@tim.se>
040: * @version
041: */
042:
043: public class TopicAdapter implements MessageDrivenBean, MessageListener {
044:
045: private static final Category log = Category
046: .getInstance(TopicAdapter.class);
047:
048: private static final String CONNECTION_JNDI = "java:comp/env/jms/MyQueueConnection";
049: private static final String QUEUE_JNDI = "java:comp/env/jms/QueueName";
050: private MessageDrivenContext ctx = null;
051: private Queue queue = null;
052: private QueueConnection queueConnection = null;
053:
054: public TopicAdapter() {
055:
056: }
057:
058: public void setMessageDrivenContext(MessageDrivenContext ctx) {
059: this .ctx = ctx;
060: }
061:
062: public void ejbCreate() {
063: try {
064: Context context = new InitialContext();
065: queue = (Queue) context.lookup(QUEUE_JNDI);
066:
067: QueueConnectionFactory factory = (QueueConnectionFactory) context
068: .lookup(CONNECTION_JNDI);
069: queueConnection = factory.createQueueConnection();
070:
071: } catch (Exception ex) {
072: // JMSException or NamingException could be thrown
073: log.debug("failed", ex);
074: throw new EJBException(ex.toString());
075: }
076: }
077:
078: public void ejbRemove() {
079: if (queueConnection != null) {
080: try {
081: queueConnection.close();
082: } catch (Exception e) {
083: log.debug("failed", e);
084: }
085: }
086: ctx = null;
087: }
088:
089: public void onMessage(Message message) {
090: log.debug("TopicBean got message" + message.toString());
091: QueueSession queueSession = null;
092: try {
093: QueueSender queueSender = null;
094:
095: queueSession = queueConnection.createQueueSession(true,
096: Session.AUTO_ACKNOWLEDGE);
097: queueSender = queueSession.createSender(queue);
098: queueSender.send(message);
099:
100: } catch (JMSException ex) {
101:
102: log.debug("failed", ex);
103: ctx.setRollbackOnly();
104: throw new EJBException(ex.toString());
105: } finally {
106: if (queueSession != null) {
107: try {
108: queueSession.close();
109: } catch (Exception e) {
110: log.debug("failed", e);
111: }
112: }
113: }
114: }
115: } // MessageBeanImpl
|