KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.rmi.RemoteException JavaDoc;
25
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.ejb.SessionContext JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.Context JavaDoc;
32
33 import javax.jms.QueueConnectionFactory JavaDoc;
34 import javax.jms.QueueConnection JavaDoc;
35 import javax.jms.QueueSession JavaDoc;
36 import javax.jms.QueueReceiver JavaDoc;
37 import javax.jms.Queue JavaDoc;
38 import javax.jms.Session JavaDoc;
39 import javax.jms.Message JavaDoc;
40 import javax.jms.JMSException JavaDoc;
41
42 import org.jboss.logging.Logger;
43
44 /**
45  * <p>QueueRec bean, get a message from the configured queue. The JMS stuff is
46  * configured via the deployment descriptor.
47  *
48  * <p>Test sync receive for jms ra.
49  *
50  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman </a>
51  * @version $Revision: 58115 $
52  */

53 public class QueueRecBean implements SessionBean JavaDoc
54 {
55
56     private final Logger log = Logger.getLogger(this.getClass());
57
58     /**
59      * Name used to lookup QueueConnectionFactory
60      */

61     private static final String JavaDoc CONNECTION_JNDI = "java:comp/env/jms/MyQueueConnection";
62
63     private QueueConnectionFactory JavaDoc factory = null;
64
65     /**
66      * Name used to lookup queue destination
67      */

68     private static final String JavaDoc QUEUE_JNDI = "java:comp/env/jms/QueueName";
69
70     private SessionContext JavaDoc ctx = null;
71
72     private Queue JavaDoc queue = null;
73
74     public QueueRecBean()
75     {
76     }
77
78     public void setSessionContext(SessionContext JavaDoc ctx)
79     {
80         this.ctx = ctx;
81     }
82
83     public void ejbCreate()
84     {
85         try
86         {
87             Context JavaDoc context = new InitialContext JavaDoc();
88
89             // Lookup the queue
90
queue = (Queue JavaDoc) context.lookup(QUEUE_JNDI);
91
92             // Lookup the connection factory
93
factory = (QueueConnectionFactory JavaDoc) context.lookup(CONNECTION_JNDI);
94
95             // Keep both around
96
}
97         catch (Exception JavaDoc ex)
98         {
99             // JMSException or NamingException could be thrown
100
log.debug("failed", ex);
101             throw new EJBException JavaDoc(ex.toString());
102         }
103     }
104
105     public void ejbRemove() throws RemoteException JavaDoc
106     {
107     }
108
109     public void ejbActivate()
110     {
111     }
112
113     public void ejbPassivate()
114     {
115     }
116
117     /**
118      * Get a message with sync rec.
119      *
120      * @return int property name defined in Publisher.JMS_MESSAGE_NR, or -1 if
121      * fail.
122      */

123     public int getMessage()
124     {
125         QueueConnection JavaDoc queueConnection = null;
126         QueueSession JavaDoc queueSession = null;
127         int ret;
128         try
129         {
130
131             // Create a session
132
queueConnection = factory.createQueueConnection();
133             queueConnection.start();
134             queueSession = queueConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
135             // Get message
136
QueueReceiver JavaDoc queueReceiver = queueSession.createReceiver(queue);
137             log.info("Waiting for message");
138             Message JavaDoc msg = queueReceiver.receive(500L);
139             if (msg != null)
140             {
141                 log.info("Recived message: " + msg);
142                 int nr = msg.getIntProperty(Publisher.JMS_MESSAGE_NR);
143                 log.debug("nr: " + nr);
144                 ret = nr;
145             }
146             else
147             {
148                 log.info("NO message recived");
149                 ret = -1;
150             }
151
152         }
153         catch (JMSException JavaDoc ex)
154         {
155
156             log.warn("failed", ex);
157             ctx.setRollbackOnly();
158             throw new EJBException JavaDoc(ex.toString());
159         }
160         finally
161         {
162             // ALWAYS close the session. It's pooled, so do not worry.
163
if (queueConnection != null)
164             {
165                 try
166                 {
167                     queueConnection.close();
168                 }
169                 catch (Exception JavaDoc e)
170                 {
171                     log.debug("failed", e);
172                 }
173             }
174         }
175         return ret;
176     }
177
178 }
179
Popular Tags