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 java.util.*;
026: import javax.ejb.SessionBean;
027: import javax.ejb.SessionContext;
028: import javax.ejb.EJBException;
029: import javax.naming.*;
030: import javax.jms.*;
031:
032: public class TopicPublisherBean implements SessionBean {
033:
034: static org.apache.log4j.Category log = org.apache.log4j.Category
035: .getInstance(TopicPublisherBean.class);
036:
037: private static final String CONNECTION_JNDI = "java:comp/env/jms/MyTopicConnection";
038: private static final String TOPIC_JNDI = "java:comp/env/jms/TopicName";
039:
040: private static final String BEAN_JNDI = "java:comp/env/ejb/PublisherCMP";
041:
042: private SessionContext ctx = null;
043: private Topic topic = null;
044: private TopicConnection topicConnection = null;
045:
046: public TopicPublisherBean() {
047: }
048:
049: public void setSessionContext(SessionContext ctx) {
050: this .ctx = ctx;
051: }
052:
053: public void ejbCreate() {
054: try {
055: Context context = new InitialContext();
056: topic = (Topic) context.lookup(TOPIC_JNDI);
057:
058: TopicConnectionFactory factory = (TopicConnectionFactory) context
059: .lookup(CONNECTION_JNDI);
060: topicConnection = factory.createTopicConnection();
061:
062: } catch (Exception ex) {
063: // JMSException or NamingException could be thrown
064: log.debug("failed", ex);
065: throw new EJBException(ex.toString());
066: }
067: }
068:
069: /**
070: * Send a message with a message nr in property MESSAGE_NR
071: */
072: public void simple(int messageNr) {
073: sendMessage(messageNr);
074: }
075:
076: /**
077: * Try send a message with a message nr in property MESSAGE_NR,
078: * but set rollback only
079: */
080: public void simpleFail(int messageNr) {
081: sendMessage(messageNr);
082: // Roll it back, no message should be sent if transactins work
083: log.debug("DEBUG: Setting rollbackOnly");
084: ctx.setRollbackOnly();
085: log.debug("DEBUG rollback set: " + ctx.getRollbackOnly());
086:
087: }
088:
089: public void beanOk(int messageNr) {
090: // DO JMS - First transaction
091: sendMessage(messageNr);
092: PublisherCMPHome h = null;
093: try {
094: // DO entity bean - Second transaction
095: h = (PublisherCMPHome) new InitialContext()
096: .lookup(BEAN_JNDI);
097: PublisherCMP b = h.create(new Integer(messageNr));
098: b.ok(messageNr);
099: } catch (Exception ex) {
100: throw new EJBException("OPS exception in ok: " + ex);
101: } finally {
102: try {
103: h.remove(new Integer(messageNr));
104: } catch (Exception e) {
105: log.debug("failed", e);
106: }
107: }
108:
109: }
110:
111: public void beanError(int messageNr) {
112: // DO JMS - First transaction
113: sendMessage(messageNr);
114: PublisherCMPHome h = null;
115: try {
116: // DO entity bean - Second transaction
117: h = (PublisherCMPHome) new InitialContext()
118: .lookup(BEAN_JNDI);
119: PublisherCMP b = h.create(new Integer(messageNr));
120: b.error(messageNr);
121: } catch (Exception ex) {
122: throw new EJBException("Exception in erro: " + ex);
123: } finally {
124: try {
125: h.remove(new Integer(messageNr));
126: } catch (Exception ex) {
127: log.debug("failed", ex);
128: }
129: }
130: }
131:
132: public void ejbRemove() throws RemoteException {
133: if (topicConnection != null) {
134: try {
135: topicConnection.close();
136: } catch (Exception e) {
137: log.debug("failed", e);
138: }
139: }
140: }
141:
142: public void ejbActivate() {
143: }
144:
145: public void ejbPassivate() {
146: }
147:
148: private void sendMessage(int messageNr) {
149: TopicSession topicSession = null;
150: try {
151: TopicPublisher topicPublisher = null;
152: TextMessage message = null;
153:
154: topicSession = topicConnection.createTopicSession(true,
155: Session.AUTO_ACKNOWLEDGE);
156: topicPublisher = topicSession.createPublisher(topic);
157:
158: message = topicSession.createTextMessage();
159: message.setText(String.valueOf(messageNr));
160: message.setIntProperty(Publisher.JMS_MESSAGE_NR, messageNr);
161: topicPublisher.publish(message);
162:
163: } catch (JMSException ex) {
164:
165: log.debug("failed", ex);
166: ctx.setRollbackOnly();
167: throw new EJBException(ex.toString());
168: } finally {
169: if (topicSession != null) {
170: try {
171: topicSession.close();
172: } catch (Exception e) {
173: log.debug("failed", e);
174: }
175: }
176: }
177: }
178:
179: }
|