Source Code Cross Referenced for Listener.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » jtests » beans » message » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.jtests.beans.message 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999 Bull S.A.
004:         * Contact: jonas-team@objectweb.org
005:         * 
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         * 
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         * 
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: Listener.java 5995 2004-12-17 15:08:36Z joaninh $
023:         * --------------------------------------------------------------------------
024:         */
025:
026:        package org.objectweb.jonas.jtests.beans.message;
027:
028:        import java.rmi.RemoteException;
029:        import javax.ejb.CreateException;
030:        import javax.ejb.FinderException;
031:        import javax.ejb.MessageDrivenBean;
032:        import javax.ejb.MessageDrivenContext;
033:        import javax.ejb.TimedObject;
034:        import javax.ejb.Timer;
035:        import javax.ejb.TimerService;
036:        import javax.jms.MapMessage;
037:        import javax.jms.Message;
038:        import javax.jms.MessageListener;
039:        import javax.jms.JMSException;
040:        import javax.naming.InitialContext;
041:        import javax.naming.NamingException;
042:        import javax.rmi.PortableRemoteObject;
043:        import javax.transaction.Transaction;
044:
045:        import org.objectweb.jonas.common.Log;
046:        import org.objectweb.jonas.jtests.util.JBean;
047:        import org.objectweb.util.monolog.api.Logger;
048:        import org.objectweb.util.monolog.api.BasicLevel;
049:
050:        /**
051:         * Common code for all Message Driven Beans
052:         * @author Philippe Durieux, Philippe Coq
053:         */
054:        public abstract class Listener extends JBean implements 
055:                MessageDrivenBean, MessageListener, TimedObject {
056:            protected static Logger logger = null;
057:            protected transient MessageDrivenContext mdbContext;
058:            protected transient MRecordHome arh = null;
059:            protected String myname;
060:
061:            abstract protected String getMyDest();
062:
063:            public static final int CREATE_TIMER_MIN = 1500;
064:            public static final int CREATE_TIMER_MAX = 1600;
065:
066:            /** 
067:             * Default constructor
068:             */
069:            public Listener() {
070:            }
071:
072:            // ------------------------------------------------------------------
073:            // MessageDrivenBean implementation
074:            // ------------------------------------------------------------------
075:
076:            /** 
077:             * Set the associated context. The container call this method
078:             * after the instance creation. 
079:             * The enterprise Bean instance should store the reference to the context
080:             * object in an instance variable. 
081:             * This method is called with no transaction context.
082:             *
083:             * @param ctx A MessageDrivenContext interface for the instance.
084:             * @throws EJBException Thrown by the method to indicate a failure caused by
085:             * a system-level error.
086:             */
087:
088:            public void setMessageDrivenContext(MessageDrivenContext ctx) {
089:                if (logger == null) {
090:                    logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
091:                }
092:                logger.log(BasicLevel.DEBUG, "");
093:                mdbContext = ctx;
094:            }
095:
096:            /**
097:             * A container invokes this method before it ends the life of the message-driven object. 
098:             * This happens when a container decides to terminate the message-driven object. 
099:             *
100:             * This method is called with no transaction context. 
101:             *
102:             * @throws EJBException Thrown by the method to indicate a failure caused by
103:             * a system-level error.
104:             */
105:            public void ejbRemove() {
106:                logger.log(BasicLevel.DEBUG, "");
107:            }
108:
109:            /**
110:             * The Message driven  bean must define an ejbCreate methods with no args.
111:             */
112:            public void ejbCreate() {
113:                logger.log(BasicLevel.DEBUG, "");
114:
115:                // Get a ref on MRecordHome
116:                InitialContext ictx = null;
117:                try {
118:                    ictx = new InitialContext();
119:                    arh = (MRecordHome) PortableRemoteObject.narrow(ictx
120:                            .lookup("messageMRecordECHome"), MRecordHome.class);
121:                } catch (NamingException e) {
122:                    logger.log(BasicLevel.ERROR,
123:                            "Listener : Cannot get messageMRecordHome:" + e);
124:                }
125:
126:                // Accesses the bean env to get the bean name
127:                // This test also that we can access java:comp/env from here.
128:                try {
129:                    myname = (String) ictx.lookup("java:comp/env/mdbname");
130:                } catch (NamingException e) {
131:                    logger.log(BasicLevel.ERROR,
132:                            "Listener : Cannot access java:comp/env/mdbname from ejbCreate:"
133:                                    + e);
134:                }
135:            }
136:
137:            /**
138:             *  onMessage method
139:             */
140:            public void onMessage(Message message) {
141:                logger.log(BasicLevel.DEBUG, "");
142:
143:                sleep(20);
144:
145:                // Decode the message (MapMessage)
146:                String uuid = null;
147:                String dest = null;
148:                int value = 0;
149:                MapMessage msg = (MapMessage) message;
150:                try {
151:                    uuid = msg.getString("Id");
152:                    dest = msg.getString("Text");
153:                    value = msg.getInt("Value");
154:                } catch (JMSException e) {
155:                    logger.log(BasicLevel.ERROR, "Listener exception:" + e);
156:                    return;
157:                }
158:
159:                // Check destination
160:                if (!dest.equals(getMyDest())) {
161:                    logger.log(BasicLevel.ERROR, "Bad destination: " + dest
162:                            + ". Expected is " + getMyDest());
163:                    return;
164:                }
165:
166:                // Create a timer if required
167:                if (value >= CREATE_TIMER_MIN && value <= CREATE_TIMER_MAX) {
168:                    TimerService timerservice = mdbContext.getTimerService();
169:                    int dur = value - CREATE_TIMER_MIN;
170:                    Info info = new Info(uuid, dest, value, myname);
171:                    Timer mt = timerservice.createTimer(dur * 1000, dur * 1000,
172:                            info);
173:                    return;
174:                }
175:
176:                // Create a new Entity bean for this message
177:                // Check that transaction association did not change after create.
178:                try {
179:                    Transaction t1 = getCurrentTransaction();
180:                    arh.create(uuid, dest, value, myname);
181:                    if (t1 != getCurrentTransaction()) {
182:                        logger.log(BasicLevel.ERROR,
183:                                "Error in transaction association");
184:                    }
185:                } catch (CreateException e) {
186:                    logger.log(BasicLevel.ERROR, "Listener exception:" + e);
187:                } catch (RemoteException e) {
188:                    logger.log(BasicLevel.ERROR, "Listener exception:" + e);
189:                }
190:
191:            }
192:
193:            // -----------------------------------------------------------
194:            // TimedObject implementation
195:            // -----------------------------------------------------------
196:
197:            /**
198:             * A timer is expired.
199:             */
200:            public void ejbTimeout(Timer timer) {
201:                logger.log(BasicLevel.DEBUG, "");
202:                Info info = (Info) timer.getInfo();
203:                // Create a new Entity bean for this message
204:                try {
205:                    MRecord mr = arh.findByPrimaryKey(new MRecordPK(info.uuid,
206:                            info.ejbname));
207:                    int cnt = mr.getCount();
208:                    if (cnt <= 3) {
209:                        mr.updateCount();
210:                    } else {
211:                        timer.cancel();
212:                    }
213:                } catch (RemoteException e) {
214:                    logger.log(BasicLevel.ERROR, "Listener exception :" + e);
215:                } catch (FinderException e) {
216:                    try {
217:                        arh.create(info.uuid, info.dest, info.value,
218:                                info.ejbname);
219:                    } catch (CreateException ee) {
220:                        logger
221:                                .log(BasicLevel.ERROR, "Listener exception:"
222:                                        + ee);
223:                    } catch (RemoteException ee) {
224:                        logger.log(BasicLevel.ERROR,
225:                                "Listener exception on create:" + ee);
226:                    }
227:                }
228:
229:            }
230:
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.