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 java.rmi.RemoteException;
025:
026: import javax.ejb.SessionBean;
027: import javax.ejb.SessionContext;
028: import javax.ejb.EJBException;
029:
030: import javax.naming.InitialContext;
031: import javax.naming.Context;
032:
033: import javax.jms.QueueConnectionFactory;
034: import javax.jms.QueueConnection;
035: import javax.jms.QueueSession;
036: import javax.jms.QueueReceiver;
037: import javax.jms.Queue;
038: import javax.jms.Session;
039: import javax.jms.Message;
040: import javax.jms.JMSException;
041:
042: import org.apache.log4j.Category;
043:
044: /**
045: * <p>QueueRec bean, get a message from the configured queue. The JMS stuff is
046: * configured via the deployment descriptor.
047: *
048: * <p>Test sync receive for jms ra.
049: *
050: * @author <a href="mailto:peter.antman@tim.se">Peter Antman </a>
051: * @version $Revision: 57211 $
052: */
053: public class QueueRecBean implements SessionBean {
054:
055: private final Category log = Category.getInstance(this .getClass());
056:
057: /**
058: * Name used to lookup QueueConnectionFactory
059: */
060: private static final String CONNECTION_JNDI = "java:comp/env/jms/MyQueueConnection";
061:
062: private QueueConnectionFactory factory = null;
063:
064: /**
065: * Name used to lookup queue destination
066: */
067: private static final String QUEUE_JNDI = "java:comp/env/jms/QueueName";
068:
069: private SessionContext ctx = null;
070:
071: private Queue queue = null;
072:
073: public QueueRecBean() {
074: }
075:
076: public void setSessionContext(SessionContext ctx) {
077: this .ctx = ctx;
078: }
079:
080: public void ejbCreate() {
081: try {
082: Context context = new InitialContext();
083:
084: // Lookup the queue
085: queue = (Queue) context.lookup(QUEUE_JNDI);
086:
087: // Lookup the connection factory
088: factory = (QueueConnectionFactory) context
089: .lookup(CONNECTION_JNDI);
090:
091: // Keep both around
092: } catch (Exception ex) {
093: // JMSException or NamingException could be thrown
094: log.debug("failed", ex);
095: throw new EJBException(ex.toString());
096: }
097: }
098:
099: public void ejbRemove() throws RemoteException {
100: }
101:
102: public void ejbActivate() {
103: }
104:
105: public void ejbPassivate() {
106: }
107:
108: /**
109: * Get a message with sync rec.
110: *
111: * @return int property name defined in Publisher.JMS_MESSAGE_NR, or -1 if
112: * fail.
113: */
114: public int getMessage() {
115: QueueConnection queueConnection = null;
116: QueueSession queueSession = null;
117: int ret;
118: try {
119:
120: // Create a session
121: queueConnection = factory.createQueueConnection();
122: queueConnection.start();
123: queueSession = queueConnection.createQueueSession(true,
124: Session.AUTO_ACKNOWLEDGE);
125: // Get message
126: QueueReceiver queueReceiver = queueSession
127: .createReceiver(queue);
128: log.info("Waiting for message");
129: Message msg = queueReceiver.receive(500L);
130: if (msg != null) {
131: log.info("Recived message: " + msg);
132: int nr = msg.getIntProperty(Publisher.JMS_MESSAGE_NR);
133: log.debug("nr: " + nr);
134: ret = nr;
135: } else {
136: log.info("NO message recived");
137: ret = -1;
138: }
139:
140: } catch (JMSException ex) {
141:
142: log.warn("failed", ex);
143: ctx.setRollbackOnly();
144: throw new EJBException(ex.toString());
145: } finally {
146: // ALWAYS close the session. It's pooled, so do not worry.
147: if (queueConnection != null) {
148: try {
149: queueConnection.close();
150: } catch (Exception e) {
151: log.debug("failed", e);
152: }
153: }
154: }
155: return ret;
156: }
157:
158: }
|