KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.ejb.SessionContext JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.QueueConnection JavaDoc;
30 import javax.jms.QueueConnectionFactory JavaDoc;
31 import javax.jms.QueueSender JavaDoc;
32 import javax.jms.QueueSession JavaDoc;
33 import javax.jms.Session JavaDoc;
34 import javax.jms.TextMessage JavaDoc;
35 import javax.jms.Queue JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38
39 import org.jboss.logging.Logger;
40
41 /**
42  * Bean to help JMS RA test publish/send JMS messages and test transactional
43  * behavior.
44  *
45  * <p>Created: Mon Apr 23 21:35:25 2001
46  *
47  * @author Unknown
48  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
49  * @version $Revision: 58115 $
50  */

51 public class PublisherBean
52     implements SessionBean JavaDoc
53 {
54     private static final String JavaDoc CONNECTION_JNDI =
55         "java:comp/env/jms/MyQueueConnection";
56     
57     private static final String JavaDoc QUEUE_JNDI =
58         "java:comp/env/jms/QueueName";
59
60     private static final String JavaDoc BEAN_JNDI =
61         "java:comp/env/ejb/PublisherCMP";
62
63     private final Logger log = Logger.getLogger(this.getClass());
64    
65     private SessionContext JavaDoc ctx; // = null;
66
private Queue JavaDoc queue; // = null;
67
private QueueConnection JavaDoc queueConnection; // = null;
68

69     public PublisherBean() {
70         // empty
71
}
72
73     public void setSessionContext(final SessionContext JavaDoc ctx) {
74         this.ctx = ctx;
75     }
76
77     public void ejbCreate() {
78         try {
79             Context JavaDoc context = new InitialContext JavaDoc();
80             queue = (Queue JavaDoc)context.lookup(QUEUE_JNDI);
81
82             QueueConnectionFactory JavaDoc factory =
83                 (QueueConnectionFactory JavaDoc)context.lookup(CONNECTION_JNDI);
84             queueConnection = factory.createQueueConnection();
85         }
86         catch (Exception JavaDoc e) {
87             // JMSException or NamingException could be thrown
88
log.error("failed to create bean", e);
89             throw new EJBException JavaDoc(e);
90         }
91     }
92
93     /**
94      * Send a message with a message nr in property MESSAGE_NR
95      */

96     public void simple(int messageNr) {
97         log.info("sending message");
98         sendMessage(messageNr);
99         log.info("sent");
100     }
101     
102     /**
103      * Try send a message with a message nr in property MESSAGE_NR,
104      * but set rollback only
105      */

106     public void simpleFail(int messageNr) {
107         log.info("sending message");
108         sendMessage(messageNr);
109         log.info("sent");
110         
111         // Roll it back, no message should be sent if transactins work
112
log.info("Setting rollbackOnly");
113         ctx.setRollbackOnly();
114         log.info("rollback set: " + ctx.getRollbackOnly());
115     }
116
117     public void beanOk(int messageNr) {
118         log.info("sending message");
119         // DO JMS - First transaction
120
sendMessage(messageNr);
121         log.info("sent");
122         
123         PublisherCMPHome h = null;
124         try {
125             // DO entity bean - Second transaction
126
h = (PublisherCMPHome) new InitialContext JavaDoc().lookup(BEAN_JNDI);
127             PublisherCMP b = h.create(new Integer JavaDoc(messageNr));
128             log.info("calling bean");
129             b.ok(messageNr);
130             log.info("called bean");
131         }
132         catch (Exception JavaDoc e) {
133             log.error("failed to contact 3rdparty bean", e);
134             throw new EJBException JavaDoc(e);
135         }
136         finally {
137             try {
138                 h.remove(new Integer JavaDoc(messageNr));
139             }
140             catch (Exception JavaDoc e) {
141                 log.error("failed to remove 3rdparty bean", e);
142             }
143             finally {
144                 log.info("done");
145             }
146         }
147     }
148
149     public void beanError(int messageNr) {
150         log.info("sending message");
151         // DO JMS - First transaction
152
sendMessage(messageNr);
153         log.info("sent");
154         
155         PublisherCMPHome h = null;
156         try {
157             // DO entity bean - Second transaction
158
h = (PublisherCMPHome) new InitialContext JavaDoc().lookup(BEAN_JNDI);
159             PublisherCMP b = h.create(new Integer JavaDoc(messageNr));
160             log.info("calling bean");
161             b.error(messageNr);
162             log.info("bean called (should never get here)");
163         }
164         catch (Exception JavaDoc e) {
165             log.info("caught exception (as expected)");
166             throw new EJBException JavaDoc("Exception in erro: " + e);
167         }
168         finally {
169             try {
170                 h.remove(new Integer JavaDoc(messageNr));
171             }
172             catch (Exception JavaDoc e) {
173                 log.error("failed to remove 3rdparty bean", e);
174             }
175             finally {
176                 log.info("done");
177             }
178         }
179     }
180
181     public void ejbRemove() throws RemoteException JavaDoc {
182         if (queueConnection != null) {
183             try {
184                 queueConnection.close();
185             }
186             catch (Exception JavaDoc e) {
187                 log.error("failed to close connection", e);
188             }
189         }
190     }
191
192     public void ejbActivate() {}
193     public void ejbPassivate() {}
194
195     private void sendMessage(int messageNr) {
196         log.info("sending message wtih nr: " + messageNr);
197         QueueSession JavaDoc queueSession = null;
198         try {
199             QueueSender JavaDoc queueSender = null;
200             TextMessage JavaDoc message = null;
201             queueSession =
202                 queueConnection.createQueueSession(true,
203                                                    Session.AUTO_ACKNOWLEDGE);
204             queueSender = queueSession.createSender(queue);
205     
206             message = queueSession.createTextMessage();
207             message.setText(String.valueOf(messageNr));
208             message.setIntProperty(Publisher.JMS_MESSAGE_NR, messageNr);
209             queueSender.send(message);
210             log.info("sent message with nr = " + messageNr);
211         }
212         catch (JMSException JavaDoc e) {
213             log.debug("failed to send", e);
214             ctx.setRollbackOnly();
215             throw new EJBException JavaDoc(e);
216         }
217         finally {
218             if (queueSession != null) {
219                 try {
220                     queueSession.close();
221                 }
222                 catch (Exception JavaDoc e) {
223                     log.error("failed to close session", e);
224                 }
225                 finally {
226                     log.info("done sending message");
227                 }
228             }
229         }
230     }
231
232 }
233
Popular Tags