1
1
/*
2
- * Copyright 2002-2007 the original author or authors.
2
+ * Copyright 2002-2011 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.
48
48
import org .springframework .remoting .support .RemoteInvocation ;
49
49
import org .springframework .remoting .support .RemoteInvocationFactory ;
50
50
import org .springframework .remoting .support .RemoteInvocationResult ;
51
+ import org .springframework .util .ClassUtils ;
51
52
52
53
/**
53
54
* {@link org.aopalliance.intercept.MethodInterceptor} for accessing a
74
75
*/
75
76
public class JmsInvokerClientInterceptor implements MethodInterceptor , InitializingBean {
76
77
78
+ private static final boolean jms11Available = ClassUtils .hasMethod (ConnectionFactory .class , "createConnection" );
79
+
77
80
private ConnectionFactory connectionFactory ;
78
81
79
82
private Object queue ;
@@ -193,7 +196,7 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable {
193
196
}
194
197
195
198
RemoteInvocation invocation = createRemoteInvocation (methodInvocation );
196
- RemoteInvocationResult result = null ;
199
+ RemoteInvocationResult result ;
197
200
try {
198
201
result = executeRequest (invocation );
199
202
}
@@ -255,39 +258,27 @@ protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) thr
255
258
}
256
259
257
260
/**
258
- * Create a new JMS Connection for this JMS invoker,
259
- * ideally a <code>javax.jms.QueueConnection</code>.
260
- * <p>The default implementation uses the
261
- * <code>javax.jms.QueueConnectionFactory</code> API if available,
262
- * falling back to a standard JMS 1.1 ConnectionFactory otherwise.
263
- * This is necessary for working with generic JMS 1.1 connection pools
264
- * (such as ActiveMQ's <code>org.apache.activemq.pool.PooledConnectionFactory</code>).
261
+ * Create a new JMS Connection for this JMS invoker.
265
262
*/
266
263
protected Connection createConnection () throws JMSException {
267
264
ConnectionFactory cf = getConnectionFactory ();
268
- if (cf instanceof QueueConnectionFactory ) {
269
- return (( QueueConnectionFactory ) cf ). createQueueConnection ();
265
+ if (jms11Available ) {
266
+ return cf . createConnection ();
270
267
}
271
268
else {
272
- return cf . createConnection ();
269
+ return (( QueueConnectionFactory ) cf ). createQueueConnection ();
273
270
}
274
271
}
275
272
276
273
/**
277
- * Create a new JMS Session for this JMS invoker,
278
- * ideally a <code>javax.jms.QueueSession</code>.
279
- * <p>The default implementation uses the
280
- * <code>javax.jms.QueueConnection</code> API if available,
281
- * falling back to a standard JMS 1.1 Connection otherwise.
282
- * This is necessary for working with generic JMS 1.1 connection pools
283
- * (such as ActiveMQ's <code>org.apache.activemq.pool.PooledConnectionFactory</code>).
274
+ * Create a new JMS Session for this JMS invoker.
284
275
*/
285
276
protected Session createSession (Connection con ) throws JMSException {
286
- if (con instanceof QueueConnection ) {
287
- return (( QueueConnection ) con ). createQueueSession (false , Session .AUTO_ACKNOWLEDGE );
277
+ if (jms11Available ) {
278
+ return con . createSession (false , Session .AUTO_ACKNOWLEDGE );
288
279
}
289
280
else {
290
- return con . createSession (false , Session .AUTO_ACKNOWLEDGE );
281
+ return (( QueueConnection ) con ). createQueueSession (false , Session .AUTO_ACKNOWLEDGE );
291
282
}
292
283
}
293
284
@@ -352,8 +343,17 @@ protected Message doExecuteRequest(Session session, Queue queue, Message request
352
343
MessageProducer producer = null ;
353
344
MessageConsumer consumer = null ;
354
345
try {
355
- if (session instanceof QueueSession ) {
346
+ if (jms11Available ) {
347
+ // Standard JMS 1.1 API usage...
348
+ responseQueue = session .createTemporaryQueue ();
349
+ producer = session .createProducer (queue );
350
+ consumer = session .createConsumer (responseQueue );
351
+ requestMessage .setJMSReplyTo (responseQueue );
352
+ producer .send (requestMessage );
353
+ }
354
+ else {
356
355
// Perform all calls on QueueSession reference for JMS 1.0.2 compatibility...
356
+ // DEPRECATED but kept around with the deprecated JmsTemplate102 etc classes for the time being.
357
357
QueueSession queueSession = (QueueSession ) session ;
358
358
responseQueue = queueSession .createTemporaryQueue ();
359
359
QueueSender sender = queueSession .createSender (queue );
@@ -362,14 +362,6 @@ protected Message doExecuteRequest(Session session, Queue queue, Message request
362
362
requestMessage .setJMSReplyTo (responseQueue );
363
363
sender .send (requestMessage );
364
364
}
365
- else {
366
- // Standard JMS 1.1 API usage...
367
- responseQueue = session .createTemporaryQueue ();
368
- producer = session .createProducer (queue );
369
- consumer = session .createConsumer (responseQueue );
370
- requestMessage .setJMSReplyTo (responseQueue );
371
- producer .send (requestMessage );
372
- }
373
365
long timeout = getReceiveTimeout ();
374
366
return (timeout > 0 ? consumer .receive (timeout ) : consumer .receive ());
375
367
}
0 commit comments