KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.ejb.SessionContext JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.naming.*;
30 import javax.jms.*;
31
32
33 public class TopicPublisherBean implements SessionBean JavaDoc {
34
35        static org.jboss.logging.Logger log =
36        org.jboss.logging.Logger.getLogger(TopicPublisherBean.class);
37
38     private static final String JavaDoc CONNECTION_JNDI = "java:comp/env/jms/MyTopicConnection";
39     private static final String JavaDoc TOPIC_JNDI = "java:comp/env/jms/TopicName";
40
41     private static final String JavaDoc BEAN_JNDI = "java:comp/env/ejb/PublisherCMP";
42
43     private SessionContext JavaDoc ctx = null;
44     private Topic topic = null;
45     private TopicConnection topicConnection = null;
46     public TopicPublisherBean() {
47     }
48
49     public void setSessionContext(SessionContext JavaDoc ctx) {
50         this.ctx = ctx;
51     }
52
53     public void ejbCreate() {
54         try {
55             Context JavaDoc context = new InitialContext();
56             topic = (Topic)context.lookup(TOPIC_JNDI);
57     
58         TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup(CONNECTION_JNDI);
59         topicConnection = factory.createTopicConnection();
60     
61         } catch (Exception JavaDoc ex) {
62             // JMSException or NamingException could be thrown
63
log.debug("failed", ex);
64         throw new EJBException JavaDoc(ex.toString());
65         }
66     }
67
68     /**
69      * Send a message with a message nr in property MESSAGE_NR
70      */

71     public void simple(int messageNr) {
72     sendMessage(messageNr);
73     }
74     /**
75      * Try send a message with a message nr in property MESSAGE_NR,
76      * but set rollback only
77      */

78     public void simpleFail(int messageNr) {
79     sendMessage(messageNr);
80     // Roll it back, no message should be sent if transactins work
81
log.debug("DEBUG: Setting rollbackOnly");
82     ctx.setRollbackOnly();
83     log.debug("DEBUG rollback set: " + ctx.getRollbackOnly());
84     
85     }
86
87     public void beanOk(int messageNr) {
88     // DO JMS - First transaction
89
sendMessage(messageNr);
90     PublisherCMPHome h = null;
91     try {
92         // DO entity bean - Second transaction
93
h = (PublisherCMPHome) new InitialContext().lookup(BEAN_JNDI);
94         PublisherCMP b = h.create(new Integer JavaDoc(messageNr));
95         b.ok(messageNr);
96     }catch(Exception JavaDoc ex) {
97         throw new EJBException JavaDoc("OPS exception in ok: " + ex);
98     } finally {
99         try {
100         h.remove(new Integer JavaDoc(messageNr));
101         }catch(Exception JavaDoc e) {
102         log.debug("failed", e);
103         }
104     }
105     
106     }
107
108     public void beanError(int messageNr) {
109     // DO JMS - First transaction
110
sendMessage(messageNr);
111     PublisherCMPHome h = null;
112     try {
113         // DO entity bean - Second transaction
114
h = (PublisherCMPHome) new InitialContext().lookup(BEAN_JNDI);
115         PublisherCMP b = h.create(new Integer JavaDoc(messageNr));
116         b.error(messageNr);
117     } catch(Exception JavaDoc ex) {
118         throw new EJBException JavaDoc("Exception in erro: " + ex);
119     }finally {
120         try {
121         h.remove(new Integer JavaDoc(messageNr));
122         }catch(Exception JavaDoc ex) {
123         log.debug("failed", ex);
124         }
125     }
126     }
127
128     public void ejbRemove() throws RemoteException JavaDoc {
129         if(topicConnection != null) {
130             try {
131                 topicConnection.close();
132             } catch (Exception JavaDoc e) {
133                 log.debug("failed", e);
134             }
135         }
136     }
137
138     public void ejbActivate() {}
139     public void ejbPassivate() {}
140
141     private void sendMessage(int messageNr) {
142     TopicSession topicSession = null;
143     try {
144         TopicPublisher topicPublisher = null;
145         TextMessage message = null;
146     
147             topicSession =
148                 topicConnection.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
149             topicPublisher = topicSession.createPublisher(topic);
150     
151             message = topicSession.createTextMessage();
152         message.setText(String.valueOf(messageNr));
153         message.setIntProperty(Publisher.JMS_MESSAGE_NR, messageNr);
154         topicPublisher.publish(message);
155     
156     
157         } catch (JMSException ex) {
158     
159             log.debug("failed", ex);
160             ctx.setRollbackOnly();
161         throw new EJBException JavaDoc(ex.toString());
162         } finally {
163             if (topicSession != null) {
164                 try {
165                     topicSession.close();
166                 } catch (Exception JavaDoc e) {
167                     log.debug("failed", e);
168                 }
169             }
170         }
171     }
172
173 }
174
Popular Tags