1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .jms .connection ;
18
18
19
- import java .lang .reflect .InvocationHandler ;
20
- import java .lang .reflect .InvocationTargetException ;
21
- import java .lang .reflect .Method ;
22
- import java .lang .reflect .Proxy ;
19
+ import javax .jms .CompletionListener ;
23
20
import javax .jms .Destination ;
24
21
import javax .jms .JMSException ;
25
22
import javax .jms .Message ;
29
26
import javax .jms .Topic ;
30
27
import javax .jms .TopicPublisher ;
31
28
32
- import org .springframework .util .ClassUtils ;
33
- import org .springframework .util .ReflectionUtils ;
34
-
35
29
/**
36
30
* JMS MessageProducer decorator that adapts calls to a shared MessageProducer
37
31
* instance underneath, managing QoS settings locally within the decorator.
41
35
*/
42
36
class CachedMessageProducer implements MessageProducer , QueueSender , TopicPublisher {
43
37
44
- // Various JMS 2.0 MessageProducer methods, if available
45
-
46
- private static final Method setDeliveryDelayMethod =
47
- ClassUtils .getMethodIfAvailable (MessageProducer .class , "setDeliveryDelay" , long .class );
48
-
49
- private static final Method getDeliveryDelayMethod =
50
- ClassUtils .getMethodIfAvailable (MessageProducer .class , "getDeliveryDelay" );
51
-
52
- private static Class <?> completionListenerClass ;
53
-
54
- private static Method sendWithCompletionListenerMethod ;
55
-
56
- private static Method sendWithDestinationAndCompletionListenerMethod ;
57
-
58
- static {
59
- try {
60
- completionListenerClass = ClassUtils .forName (
61
- "javax.jms.CompletionListener" , CachedMessageProducer .class .getClassLoader ());
62
- sendWithCompletionListenerMethod = MessageProducer .class .getMethod (
63
- "send" , Message .class , int .class , int .class , long .class , completionListenerClass );
64
- sendWithDestinationAndCompletionListenerMethod = MessageProducer .class .getMethod (
65
- "send" , Destination .class , Message .class , int .class , int .class , long .class , completionListenerClass );
66
- }
67
- catch (Exception ex ) {
68
- // No JMS 2.0 API available
69
- completionListenerClass = null ;
70
- }
71
- }
72
-
73
-
74
38
private final MessageProducer target ;
75
39
76
40
private Boolean originalDisableMessageID ;
@@ -120,15 +84,15 @@ public boolean getDisableMessageTimestamp() throws JMSException {
120
84
return this .target .getDisableMessageTimestamp ();
121
85
}
122
86
123
- public void setDeliveryDelay (long deliveryDelay ) {
87
+ public void setDeliveryDelay (long deliveryDelay ) throws JMSException {
124
88
if (this .originalDeliveryDelay == null ) {
125
- this .originalDeliveryDelay = ( Long ) ReflectionUtils . invokeMethod ( getDeliveryDelayMethod , this .target );
89
+ this .originalDeliveryDelay = this .target . getDeliveryDelay ( );
126
90
}
127
- ReflectionUtils . invokeMethod ( setDeliveryDelayMethod , this .target , deliveryDelay );
91
+ this .target . setDeliveryDelay ( deliveryDelay );
128
92
}
129
93
130
- public long getDeliveryDelay () {
131
- return ( Long ) ReflectionUtils . invokeMethod ( getDeliveryDelayMethod , this .target );
94
+ public long getDeliveryDelay () throws JMSException {
95
+ return this .target . getDeliveryDelay ( );
132
96
}
133
97
134
98
@ Override
@@ -196,6 +160,31 @@ public void send(Destination destination, Message message, int deliveryMode, int
196
160
this .target .send (destination , message , deliveryMode , priority , timeToLive );
197
161
}
198
162
163
+ @ Override
164
+ public void send (Message message , CompletionListener completionListener ) throws JMSException {
165
+ this .target .send (message , this .deliveryMode , this .priority , this .timeToLive , completionListener );
166
+ }
167
+
168
+ @ Override
169
+ public void send (Message message , int deliveryMode , int priority , long timeToLive ,
170
+ CompletionListener completionListener ) throws JMSException {
171
+
172
+ this .target .send (message , deliveryMode , priority , timeToLive , completionListener );
173
+ }
174
+
175
+ @ Override
176
+ public void send (Destination destination , Message message , CompletionListener completionListener ) throws JMSException {
177
+ this .target .send (destination , message , this .deliveryMode , this .priority , this .timeToLive , completionListener );
178
+ }
179
+
180
+ @ Override
181
+ public void send (Destination destination , Message message , int deliveryMode , int priority ,
182
+ long timeToLive , CompletionListener completionListener ) throws JMSException {
183
+
184
+ this .target .send (destination , message , deliveryMode , priority , timeToLive , completionListener );
185
+
186
+ }
187
+
199
188
@ Override
200
189
public void send (Queue queue , Message message ) throws JMSException {
201
190
this .target .send (queue , message , this .deliveryMode , this .priority , this .timeToLive );
@@ -238,7 +227,7 @@ public void close() throws JMSException {
238
227
this .originalDisableMessageTimestamp = null ;
239
228
}
240
229
if (this .originalDeliveryDelay != null ) {
241
- ReflectionUtils . invokeMethod ( setDeliveryDelayMethod , this .target , this .originalDeliveryDelay );
230
+ this .target . setDeliveryDelay ( this .originalDeliveryDelay );
242
231
this .originalDeliveryDelay = null ;
243
232
}
244
233
}
@@ -248,54 +237,4 @@ public String toString() {
248
237
return "Cached JMS MessageProducer: " + this .target ;
249
238
}
250
239
251
-
252
- /**
253
- * Build a dynamic proxy that reflectively adapts to JMS 2.0 API methods, if necessary.
254
- * Otherwise simply return this CachedMessageProducer instance itself.
255
- */
256
- public MessageProducer getProxyIfNecessary () {
257
- if (completionListenerClass != null ) {
258
- return (MessageProducer ) Proxy .newProxyInstance (CachedMessageProducer .class .getClassLoader (),
259
- new Class <?>[] {MessageProducer .class , QueueSender .class , TopicPublisher .class },
260
- new Jms2MessageProducerInvocationHandler ());
261
- }
262
- else {
263
- return this ;
264
- }
265
- }
266
-
267
-
268
- /**
269
- * Reflective InvocationHandler which adapts to JMS 2.0 API methods that we
270
- * cannot statically compile against while preserving JMS 1.1 compatibility
271
- * (due to the new {@code javax.jms.CompletionListener} type in the signatures).
272
- */
273
- private class Jms2MessageProducerInvocationHandler implements InvocationHandler {
274
-
275
- @ Override
276
- public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
277
- try {
278
- if (method .getName ().equals ("send" ) && args != null &&
279
- completionListenerClass == method .getParameterTypes ()[args .length - 1 ]) {
280
- switch (args .length ) {
281
- case 2 : // send(message, completionListener)
282
- return sendWithCompletionListenerMethod .invoke (
283
- target , args [0 ], deliveryMode , priority , timeToLive , args [1 ]);
284
- case 3 : // send(destination, message, completionListener)
285
- return sendWithDestinationAndCompletionListenerMethod .invoke (
286
- target , args [0 ], args [1 ], deliveryMode , priority , timeToLive , args [2 ]);
287
- case 5 : // send(message, deliveryMode, priority, timeToLive, completionListener)
288
- return sendWithCompletionListenerMethod .invoke (target , args );
289
- case 6 : // send(destination, message, deliveryMode, priority, timeToLive, completionListener)
290
- return sendWithDestinationAndCompletionListenerMethod .invoke (target , args );
291
- }
292
- }
293
- return method .invoke (CachedMessageProducer .this , args );
294
- }
295
- catch (InvocationTargetException ex ) {
296
- throw ex .getTargetException ();
297
- }
298
- }
299
- }
300
-
301
240
}
0 commit comments