Source Code Cross Referenced for GenericMessageEndpointManager.java in  » J2EE » spring-framework-2.5 » org » springframework » jca » endpoint » 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 » spring framework 2.5 » org.springframework.jca.endpoint 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2007 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.jca.endpoint;
018:
019:        import javax.resource.ResourceException;
020:        import javax.resource.spi.ActivationSpec;
021:        import javax.resource.spi.ResourceAdapter;
022:        import javax.resource.spi.endpoint.MessageEndpointFactory;
023:
024:        import org.springframework.beans.factory.DisposableBean;
025:        import org.springframework.beans.factory.InitializingBean;
026:        import org.springframework.context.Lifecycle;
027:
028:        /**
029:         * Generic bean that manages JCA 1.5 message endpoints within a Spring
030:         * application context, activating and deactivating the endpoint as part
031:         * of the application context's lifecycle.
032:         *
033:         * <p>This class is completely generic in that it may work with any
034:         * ResourceAdapter, any MessageEndpointFactory, and any ActivationSpec.
035:         * It can be configured in standard bean style, for example through
036:         * Spring's XML bean definition format, as follows:
037:         *
038:         * <pre class="code">
039:         * &lt;bean class="org.springframework.jca.endpoint.GenericMessageEndpointManager"&gt;
040:         * 	 &lt;property name="resourceAdapter" ref="resourceAdapter"/&gt;
041:         * 	 &lt;property name="messageEndpointFactory"&gt;
042:         *     &lt;bean class="org.springframework.jca.endpoint.GenericMessageEndpointFactory"&gt;
043:         *       &lt;property name="messageListener" ref="messageListener"/&gt;
044:         *     &lt;/bean&gt;
045:         * 	 &lt;/property&gt;
046:         * 	 &lt;property name="activationSpec"&gt;
047:         *     &lt;bean class="org.apache.activemq.ra.ActiveMQActivationSpec"&gt;
048:         *       &lt;property name="destination" value="myQueue"/&gt;
049:         *       &lt;property name="destinationType" value="javax.jms.Queue"/&gt;
050:         *     &lt;/bean&gt;
051:         *   &lt;/property&gt;
052:         * &lt;/bean&gt;</pre>
053:         *
054:         * In this example, Spring's own {@link GenericMessageEndpointFactory} is used
055:         * to point to a standard message listener object that happens to be supported
056:         * by the specified target ResourceAdapter: in this case, a JMS
057:         * {@link javax.jms.MessageListener} object as supported by the ActiveMQ
058:         * message broker, defined as a Spring bean:
059:         *
060:         * <pre class="code">
061:         * &lt;bean id="messageListener" class="com.myorg.messaging.myMessageListener"&gt;
062:         *   ...
063:         * &lt;/bean&gt;</pre>
064:         *
065:         * The target ResourceAdapter may be configured as a local Spring bean as well
066:         * (the typical case) or obtained from JNDI (e.g. on WebLogic). For the
067:         * example above, a local ResourceAdapter bean could be defined as follows
068:         * (matching the "resourceAdapter" bean reference above):
069:         *
070:         * <pre class="code">
071:         * &lt;bean id="resourceAdapter" class="org.springframework.jca.support.ResourceAdapterFactoryBean"&gt;
072:         *   &lt;property name="resourceAdapter"&gt;
073:         *     &lt;bean class="org.apache.activemq.ra.ActiveMQResourceAdapter"&gt;
074:         *       &lt;property name="serverUrl" value="tcp://localhost:61616"/&gt;
075:         *     &lt;/bean&gt;
076:         *   &lt;/property&gt;
077:         *   &lt;property name="workManager"&gt;
078:         *     &lt;bean class="org.springframework.jca.work.SimpleTaskWorkManager"/&gt;
079:         *   &lt;/property&gt;
080:         * &lt;/bean&gt;</pre>
081:         *
082:         * For a different target resource, the configuration would simply point to a
083:         * different ResourceAdapter and a different ActivationSpec object (which are
084:         * both specific to the resource provider), and possibly a different message
085:         * listener (e.g. a CCI {@link javax.resource.cci.MessageListener} for a
086:         * resource adapter which is based on the JCA Common Client Interface).
087:         *
088:         * <p>The asynchronous execution strategy can be customized through the
089:         * "workManager" property on the ResourceAdapterFactoryBean (as shown above).
090:         * Check out {@link org.springframework.jca.work.SimpleTaskWorkManager}'s
091:         * javadoc for its configuration options; alternatively, any other
092:         * JCA-compliant WorkManager can be used (e.g. Geronimo's).
093:         *
094:         * <p>Transactional execution is a responsibility of the concrete message endpoint,
095:         * as built by the specified MessageEndpointFactory. {@link GenericMessageEndpointFactory}
096:         * supports XA transaction participation through its "transactionManager" property,
097:         * typically with a Spring {@link org.springframework.transaction.jta.JtaTransactionManager}
098:         * or a plain {@link javax.transaction.TransactionManager} implementation specified there.
099:         *
100:         * <pre class="code">
101:         * &lt;bean class="org.springframework.jca.endpoint.GenericMessageEndpointManager"&gt;
102:         * 	 &lt;property name="resourceAdapter" ref="resourceAdapter"/&gt;
103:         * 	 &lt;property name="messageEndpointFactory"&gt;
104:         *     &lt;bean class="org.springframework.jca.endpoint.GenericMessageEndpointFactory"&gt;
105:         *       &lt;property name="messageListener" ref="messageListener"/&gt;
106:         *       &lt;property name="transactionManager" ref="transactionManager"/&gt;
107:         *     &lt;/bean&gt;
108:         * 	 &lt;/property&gt;
109:         * 	 &lt;property name="activationSpec"&gt;
110:         *     &lt;bean class="org.apache.activemq.ra.ActiveMQActivationSpec"&gt;
111:         *       &lt;property name="destination" value="myQueue"/&gt;
112:         *       &lt;property name="destinationType" value="javax.jms.Queue"/&gt;
113:         *     &lt;/bean&gt;
114:         *   &lt;/property&gt;
115:         * &lt;/bean&gt;
116:         *
117:         * &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/&gt;</pre>
118:         *
119:         * Alternatively, check out your resource provider's ActivationSpec object,
120:         * which should support local transactions through a provider-specific config flag,
121:         * e.g. ActiveMQActivationSpec's "useRAManagedTransaction" bean property.
122:         *
123:         * <pre class="code">
124:         * &lt;bean class="org.springframework.jca.endpoint.GenericMessageEndpointManager"&gt;
125:         * 	 &lt;property name="resourceAdapter" ref="resourceAdapter"/&gt;
126:         * 	 &lt;property name="messageEndpointFactory"&gt;
127:         *     &lt;bean class="org.springframework.jca.endpoint.GenericMessageEndpointFactory"&gt;
128:         *       &lt;property name="messageListener" ref="messageListener"/&gt;
129:         *     &lt;/bean&gt;
130:         * 	 &lt;/property&gt;
131:         * 	 &lt;property name="activationSpec"&gt;
132:         *     &lt;bean class="org.apache.activemq.ra.ActiveMQActivationSpec"&gt;
133:         *       &lt;property name="destination" value="myQueue"/&gt;
134:         *       &lt;property name="destinationType" value="javax.jms.Queue"/&gt;
135:         *       &lt;property name="useRAManagedTransaction" value="true"/&gt;
136:         *     &lt;/bean&gt;
137:         *   &lt;/property&gt;
138:         * &lt;/bean&gt;</pre>
139:         *
140:         * @author Juergen Hoeller
141:         * @since 2.5
142:         * @see javax.resource.spi.ResourceAdapter#endpointActivation
143:         * @see javax.resource.spi.ResourceAdapter#endpointDeactivation
144:         * @see javax.resource.spi.endpoint.MessageEndpointFactory
145:         * @see javax.resource.spi.ActivationSpec
146:         */
147:        public class GenericMessageEndpointManager implements  InitializingBean,
148:                Lifecycle, DisposableBean {
149:
150:            private ResourceAdapter resourceAdapter;
151:
152:            private MessageEndpointFactory messageEndpointFactory;
153:
154:            private ActivationSpec activationSpec;
155:
156:            private boolean autoStartup = true;
157:
158:            private boolean running = false;
159:
160:            private final Object lifecycleMonitor = new Object();
161:
162:            /**
163:             * Set the JCA ResourceAdapter to manage endpoints for.
164:             */
165:            public void setResourceAdapter(ResourceAdapter resourceAdapter) {
166:                this .resourceAdapter = resourceAdapter;
167:            }
168:
169:            /**
170:             * Return the JCA ResourceAdapter to manage endpoints for.
171:             */
172:            public ResourceAdapter getResourceAdapter() {
173:                return this .resourceAdapter;
174:            }
175:
176:            /**
177:             * Set the JCA MessageEndpointFactory to activate, pointing to a
178:             * MessageListener object that the endpoints will delegate to.
179:             * <p>A MessageEndpointFactory instance may be shared across multiple
180:             * endpoints (i.e. multiple GenericMessageEndpointManager instances),
181:             * with different {@link #setActivationSpec ActivationSpec} objects applied.
182:             * @see GenericMessageEndpointFactory#setMessageListener
183:             */
184:            public void setMessageEndpointFactory(
185:                    MessageEndpointFactory messageEndpointFactory) {
186:                this .messageEndpointFactory = messageEndpointFactory;
187:            }
188:
189:            /**
190:             * Return the JCA MessageEndpointFactory to activate.
191:             */
192:            public MessageEndpointFactory getMessageEndpointFactory() {
193:                return this .messageEndpointFactory;
194:            }
195:
196:            /**
197:             * Set the JCA ActivationSpec to use for activating the endpoint.
198:             * <p>Note that this ActivationSpec instance should not be shared
199:             * across multiple ResourceAdapter instances.
200:             */
201:            public void setActivationSpec(ActivationSpec activationSpec) {
202:                this .activationSpec = activationSpec;
203:            }
204:
205:            /**
206:             * Return the JCA ActivationSpec to use for activating the endpoint.
207:             */
208:            public ActivationSpec getActivationSpec() {
209:                return this .activationSpec;
210:            }
211:
212:            /**
213:             * Set whether to auto-start the endpoint activation along with
214:             * this endpoint manager's initialization.
215:             * <p>Default is "true". Turn this flag off to defer the endpoint
216:             * activation until an explicit {#start()} call.
217:             */
218:            public void setAutoStartup(boolean autoStartup) {
219:                this .autoStartup = autoStartup;
220:            }
221:
222:            /**
223:             * Prepares the message endpoint, and automatically activates it
224:             * if the "autoStartup" flag is set to "true".
225:             */
226:            public void afterPropertiesSet() throws ResourceException {
227:                if (getResourceAdapter() == null) {
228:                    throw new IllegalArgumentException(
229:                            "Property 'resourceAdapter' is required");
230:                }
231:                if (getMessageEndpointFactory() == null) {
232:                    throw new IllegalArgumentException(
233:                            "Property 'messageEndpointFactory' is required");
234:                }
235:                ActivationSpec activationSpec = getActivationSpec();
236:                if (activationSpec == null) {
237:                    throw new IllegalArgumentException(
238:                            "Property 'activationSpec' is required");
239:                }
240:
241:                if (activationSpec.getResourceAdapter() == null) {
242:                    activationSpec.setResourceAdapter(getResourceAdapter());
243:                } else if (activationSpec.getResourceAdapter() != getResourceAdapter()) {
244:                    throw new IllegalArgumentException(
245:                            "ActivationSpec ["
246:                                    + activationSpec
247:                                    + "] is associated with a different ResourceAdapter: "
248:                                    + activationSpec.getResourceAdapter());
249:                }
250:
251:                if (this .autoStartup) {
252:                    start();
253:                }
254:            }
255:
256:            /**
257:             * Activates the configured message endpoint.
258:             */
259:            public void start() {
260:                synchronized (this .lifecycleMonitor) {
261:                    if (!this .running) {
262:                        try {
263:                            getResourceAdapter().endpointActivation(
264:                                    getMessageEndpointFactory(),
265:                                    getActivationSpec());
266:                        } catch (ResourceException ex) {
267:                            IllegalStateException wrapped = new IllegalStateException(
268:                                    "Could not activate message endpoint");
269:                            wrapped.initCause(ex);
270:                            throw wrapped;
271:                        }
272:                        this .running = true;
273:                    }
274:                }
275:            }
276:
277:            /**
278:             * Deactivates the configured message endpoint.
279:             */
280:            public void stop() {
281:                synchronized (this .lifecycleMonitor) {
282:                    if (this .running) {
283:                        getResourceAdapter().endpointDeactivation(
284:                                getMessageEndpointFactory(),
285:                                getActivationSpec());
286:                        this .running = false;
287:                    }
288:                }
289:            }
290:
291:            /**
292:             * Return whether the configured message endpoint is currently active.
293:             */
294:            public boolean isRunning() {
295:                synchronized (this .lifecycleMonitor) {
296:                    return this .running;
297:                }
298:            }
299:
300:            /**
301:             * Deactivates the message endpoint, preparing it for shutdown.
302:             */
303:            public void destroy() {
304:                stop();
305:            }
306:
307:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.