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: import javax.ejb.EJBException;
026: import javax.ejb.SessionBean;
027: import javax.ejb.SessionContext;
028: import javax.jms.JMSException;
029: import javax.jms.QueueConnection;
030: import javax.jms.QueueConnectionFactory;
031: import javax.jms.QueueSender;
032: import javax.jms.QueueSession;
033: import javax.jms.Session;
034: import javax.jms.TextMessage;
035: import javax.jms.Queue;
036: import javax.naming.Context;
037: import javax.naming.InitialContext;
038:
039: import org.apache.log4j.Category;
040:
041: /**
042: * Bean to help JMS RA test publish/send JMS messages and test transactional
043: * behavior.
044: *
045: * <p>Created: Mon Apr 23 21:35:25 2001
046: *
047: * @author Unknown
048: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
049: * @version $Revision: 57211 $
050: */
051: public class PublisherBean implements SessionBean {
052: private static final String CONNECTION_JNDI = "java:comp/env/jms/MyQueueConnection";
053:
054: private static final String QUEUE_JNDI = "java:comp/env/jms/QueueName";
055:
056: private static final String BEAN_JNDI = "java:comp/env/ejb/PublisherCMP";
057:
058: private final Category log = Category.getInstance(this .getClass());
059:
060: private SessionContext ctx; // = null;
061: private Queue queue; // = null;
062: private QueueConnection queueConnection; // = null;
063:
064: public PublisherBean() {
065: // empty
066: }
067:
068: public void setSessionContext(final SessionContext ctx) {
069: this .ctx = ctx;
070: }
071:
072: public void ejbCreate() {
073: try {
074: Context context = new InitialContext();
075: queue = (Queue) context.lookup(QUEUE_JNDI);
076:
077: QueueConnectionFactory factory = (QueueConnectionFactory) context
078: .lookup(CONNECTION_JNDI);
079: queueConnection = factory.createQueueConnection();
080: } catch (Exception e) {
081: // JMSException or NamingException could be thrown
082: log.error("failed to create bean", e);
083: throw new EJBException(e);
084: }
085: }
086:
087: /**
088: * Send a message with a message nr in property MESSAGE_NR
089: */
090: public void simple(int messageNr) {
091: log.info("sending message");
092: sendMessage(messageNr);
093: log.info("sent");
094: }
095:
096: /**
097: * Try send a message with a message nr in property MESSAGE_NR,
098: * but set rollback only
099: */
100: public void simpleFail(int messageNr) {
101: log.info("sending message");
102: sendMessage(messageNr);
103: log.info("sent");
104:
105: // Roll it back, no message should be sent if transactins work
106: log.info("Setting rollbackOnly");
107: ctx.setRollbackOnly();
108: log.info("rollback set: " + ctx.getRollbackOnly());
109: }
110:
111: public void beanOk(int messageNr) {
112: log.info("sending message");
113: // DO JMS - First transaction
114: sendMessage(messageNr);
115: log.info("sent");
116:
117: PublisherCMPHome h = null;
118: try {
119: // DO entity bean - Second transaction
120: h = (PublisherCMPHome) new InitialContext()
121: .lookup(BEAN_JNDI);
122: PublisherCMP b = h.create(new Integer(messageNr));
123: log.info("calling bean");
124: b.ok(messageNr);
125: log.info("called bean");
126: } catch (Exception e) {
127: log.error("failed to contact 3rdparty bean", e);
128: throw new EJBException(e);
129: } finally {
130: try {
131: h.remove(new Integer(messageNr));
132: } catch (Exception e) {
133: log.error("failed to remove 3rdparty bean", e);
134: } finally {
135: log.info("done");
136: }
137: }
138: }
139:
140: public void beanError(int messageNr) {
141: log.info("sending message");
142: // DO JMS - First transaction
143: sendMessage(messageNr);
144: log.info("sent");
145:
146: PublisherCMPHome h = null;
147: try {
148: // DO entity bean - Second transaction
149: h = (PublisherCMPHome) new InitialContext()
150: .lookup(BEAN_JNDI);
151: PublisherCMP b = h.create(new Integer(messageNr));
152: log.info("calling bean");
153: b.error(messageNr);
154: log.info("bean called (should never get here)");
155: } catch (Exception e) {
156: log.info("caught exception (as expected)");
157: throw new EJBException("Exception in erro: " + e);
158: } finally {
159: try {
160: h.remove(new Integer(messageNr));
161: } catch (Exception e) {
162: log.error("failed to remove 3rdparty bean", e);
163: } finally {
164: log.info("done");
165: }
166: }
167: }
168:
169: public void ejbRemove() throws RemoteException {
170: if (queueConnection != null) {
171: try {
172: queueConnection.close();
173: } catch (Exception e) {
174: log.error("failed to close connection", e);
175: }
176: }
177: }
178:
179: public void ejbActivate() {
180: }
181:
182: public void ejbPassivate() {
183: }
184:
185: private void sendMessage(int messageNr) {
186: log.info("sending message wtih nr: " + messageNr);
187: QueueSession queueSession = null;
188: try {
189: QueueSender queueSender = null;
190: TextMessage message = null;
191: queueSession = queueConnection.createQueueSession(true,
192: Session.AUTO_ACKNOWLEDGE);
193: queueSender = queueSession.createSender(queue);
194:
195: message = queueSession.createTextMessage();
196: message.setText(String.valueOf(messageNr));
197: message.setIntProperty(Publisher.JMS_MESSAGE_NR, messageNr);
198: queueSender.send(message);
199: log.info("sent message with nr = " + messageNr);
200: } catch (JMSException e) {
201: log.debug("failed to send", e);
202: ctx.setRollbackOnly();
203: throw new EJBException(e);
204: } finally {
205: if (queueSession != null) {
206: try {
207: queueSession.close();
208: } catch (Exception e) {
209: log.error("failed to close session", e);
210: } finally {
211: log.info("done sending message");
212: }
213: }
214: }
215: }
216:
217: }
|