Source Code Cross Referenced for NamedQueueClient.java in  » Net » Coadunation_1.0.1 » com » rift » coad » daemon » messageservice » named » 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 » Net » Coadunation_1.0.1 » com.rift.coad.daemon.messageservice.named 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * <Add library description here>
003:         * Copyright (C) 2007 Rift IT Contracting
004:         *
005:         * This library is free software; you can redistribute it and/or
006:         * modify it under the terms of the GNU Lesser General Public
007:         * License as published by the Free Software Foundation; either
008:         * version 2.1 of the License, or (at your option) any later version.
009:         *
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         * Lesser General Public License for more details.
014:         *
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
018:         *
019:         * NamedQueueClient.java
020:         */
021:
022:        // package path
023:        package com.rift.coad.daemon.messageservice.named;
024:
025:        // java imports
026:        import java.util.Date;
027:        import javax.naming.Context;
028:        import javax.naming.InitialContext;
029:
030:        // logging import
031:        import org.apache.log4j.Logger;
032:
033:        // coadunation imports
034:        import com.rift.coad.daemon.messageservice.Message;
035:        import com.rift.coad.daemon.messageservice.MessageServiceException;
036:        import com.rift.coad.daemon.messageservice.QueueManager;
037:        import com.rift.coad.daemon.messageservice.NamedQueue;
038:        import com.rift.coad.util.connection.ConnectionManager;
039:
040:        /**
041:         * This object is responsible for managing the connection to the named queue
042:         * object.
043:         *
044:         * @author Brett Chaldecott
045:         */
046:        public class NamedQueueClient {
047:
048:            // class constants
049:            private static long MAX_TIMEOUT = 1000;
050:
051:            // log refernce
052:            private Logger log = Logger.getLogger(NamedQueueClient.class
053:                    .getName());
054:
055:            // private member variables
056:            private Context context = null;
057:            private String jndiUrl = null;
058:            private String name = null;
059:            private NamedQueue namedQueue = null;
060:
061:            /** 
062:             * Creates a new instance of NamedQueueClient
063:             *
064:             * @param name The name of the queue to make a connection to.
065:             * @exception MessageServiceException
066:             */
067:            private NamedQueueClient(String name)
068:                    throws MessageServiceException {
069:                try {
070:                    this .jndiUrl = QueueManager.JNDI_URL;
071:                    this .name = name;
072:                } catch (Exception ex) {
073:                    throw new MessageServiceException(
074:                            "Failed to instanciate the NamedQueueClient : "
075:                                    + ex.getMessage(), ex);
076:                }
077:            }
078:
079:            /** 
080:             * Creates a new instance of NamedQueueClient
081:             *
082:             * @param context The context that should be used to make a connection to
083:             *      the message service.
084:             * @param jndiUrl The url of the message queue manager.
085:             * @param name The name of the queue to connect to.
086:             * @exception MessageServiceException
087:             */
088:            private NamedQueueClient(Context context, String jndiUrl,
089:                    String name) throws MessageServiceException {
090:                try {
091:                    context = new InitialContext();
092:                    this .jndiUrl = jndiUrl;
093:                    this .name = name;
094:                } catch (Exception ex) {
095:                    throw new MessageServiceException(
096:                            "Failed to instanciate the NamedQueueClient : "
097:                                    + ex.getMessage(), ex);
098:                }
099:            }
100:
101:            /**
102:             * This method returns an instance of the NamedQueueClient object.
103:             *
104:             * @return The reference to the NamedQueueClient object.
105:             * @param name The name of the queue to return.
106:             * @exception MessageServiceException
107:             */
108:            public static NamedQueueClient create(String name)
109:                    throws MessageServiceException {
110:                return new NamedQueueClient(name);
111:            }
112:
113:            /**
114:             * This method returns an instance of the NamedQueueClient object.
115:             *
116:             * @return The reference to the NamedQueueClient object.
117:             * @param context The context that should be used to make a connection to
118:             *      the message service.
119:             * @param jndiUrl The url of the message queue manager.
120:             * @param name The name of the queue to connect to.
121:             * @exception MessageServiceException
122:             */
123:            public static NamedQueueClient create(Context context,
124:                    String jndiUrl, String name) throws MessageServiceException {
125:                return new NamedQueueClient(context, jndiUrl, name);
126:            }
127:
128:            /**
129:             * This method returns the reference to the received message from the
130:             * named queue, or null if not message is available.
131:             *
132:             * @return A reference to the retrieved message or NULL.
133:             * @param delay The delay in processing.
134:             * @exception MessageServiceException
135:             */
136:            public Message receive(long delay) throws MessageServiceException {
137:                Date startTime = new Date();
138:                Date currentTime = null;
139:                while ((startTime.getTime() + delay) > (currentTime = new Date())
140:                        .getTime()) {
141:                    try {
142:                        NamedQueue namedQueue = getNamedQueue();
143:                        Message result = null;
144:                        if ((delay == 0) || (delay > MAX_TIMEOUT)) {
145:                            result = namedQueue.receive(MAX_TIMEOUT);
146:                        } else {
147:                            result = namedQueue.receive(delay);
148:                        }
149:                        if (result != null) {
150:                            return result;
151:                        }
152:                    } catch (java.rmi.RemoteException ex) {
153:                        log.error("Failed to retrieve the queue entry : "
154:                                + ex.getMessage(), ex);
155:                        this .namedQueue = null;
156:                    } catch (MessageServiceException ex) {
157:                        throw ex;
158:                    } catch (Exception ex) {
159:                        throw new MessageServiceException(
160:                                "Failed to retrieve a message : "
161:                                        + ex.getMessage(), ex);
162:                    }
163:                }
164:                return null;
165:            }
166:
167:            /**
168:             * This method returns the named queue
169:             *
170:             * @return The reference to the named queue.
171:             * @exception MessageServiceException
172:             */
173:            private NamedQueue getNamedQueue() throws MessageServiceException {
174:                try {
175:                    if (namedQueue != null) {
176:                        return namedQueue;
177:                    }
178:                    QueueManager queueManager = null;
179:                    if (context == null) {
180:                        queueManager = (QueueManager) ConnectionManager
181:                                .getInstance().getConnection(
182:                                        QueueManager.class, this .jndiUrl);
183:                    } else {
184:                        queueManager = (QueueManager) ConnectionManager
185:                                .getInstance(context).getConnection(
186:                                        QueueManager.class, this .jndiUrl);
187:                    }
188:                    return this .namedQueue = queueManager
189:                            .getNamedQueue(this .name);
190:                } catch (Exception ex) {
191:                    throw new MessageServiceException(
192:                            "Failed to retrieve a named queue because : "
193:                                    + ex.getMessage(), ex);
194:                }
195:            }
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.