KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmsra > bean > TopicAdapter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jmsra.bean;
23
24 import javax.ejb.MessageDrivenBean JavaDoc;
25 import javax.ejb.MessageDrivenContext JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27
28 import javax.naming.*;
29 import javax.jms.*;
30
31 import org.jboss.logging.Logger;
32
33 /**
34  * Listen on a topic, send to queue
35  *
36  *
37  * Created: Sat Nov 25 18:07:50 2000
38  *
39  * @author
40  * @version
41  */

42
43 public class TopicAdapter implements MessageDrivenBean JavaDoc, MessageListener{
44
45     private static final Logger log = Logger.getLogger(TopicAdapter.class);
46    
47     private static final String JavaDoc CONNECTION_JNDI = "java:comp/env/jms/MyQueueConnection";
48     private static final String JavaDoc QUEUE_JNDI = "java:comp/env/jms/QueueName";
49     private MessageDrivenContext JavaDoc ctx = null;
50     private Queue queue = null;
51     private QueueConnection queueConnection = null;
52
53     public TopicAdapter() {
54     
55     }
56     public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
57     {
58     this.ctx = ctx;
59     }
60     
61     public void ejbCreate() {
62     try {
63             Context JavaDoc context = new InitialContext();
64             queue = (Queue)context.lookup(QUEUE_JNDI);
65
66         QueueConnectionFactory factory = (QueueConnectionFactory)context.lookup(CONNECTION_JNDI);
67         queueConnection = factory.createQueueConnection();
68         
69         } catch (Exception JavaDoc ex) {
70             // JMSException or NamingException could be thrown
71
log.debug("failed", ex);
72         throw new EJBException JavaDoc(ex.toString());
73         }
74     }
75
76     public void ejbRemove() {
77     if(queueConnection != null) {
78             try {
79                 queueConnection.close();
80             } catch (Exception JavaDoc e) {
81                 log.debug("failed", e);
82             }
83         }
84     ctx=null;
85     }
86
87     public void onMessage(Message message) {
88     log.debug("TopicBean got message" + message.toString() );
89     QueueSession queueSession = null;
90     try {
91         QueueSender queueSender = null;
92         
93             queueSession =
94                 queueConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
95             queueSender = queueSession.createSender(queue);
96         queueSender.send(message);
97         
98         
99         } catch (JMSException ex) {
100         
101             log.debug("failed", ex);
102             ctx.setRollbackOnly();
103         throw new EJBException JavaDoc(ex.toString());
104         } finally {
105             if (queueSession != null) {
106                 try {
107                     queueSession.close();
108                 } catch (Exception JavaDoc e) {
109                     log.debug("failed", e);
110                 }
111             }
112         }
113     }
114 } // MessageBeanImpl
115
Popular Tags