43
43
import org .springframework .util .StringUtils ;
44
44
45
45
/**
46
- * Bean post-processor that registers methods annotated with @ {@link JmsListener}
46
+ * Bean post-processor that registers methods annotated with {@link JmsListener}
47
47
* to be invoked by a JMS message listener container created under the cover
48
48
* by a {@link org.springframework.jms.config.JmsListenerContainerFactory} according
49
49
* to the parameters of the annotation.
50
50
*
51
- * <p>Annotated methods can use flexible arguments as defined by @ {@link JmsListener}.
51
+ * <p>Annotated methods can use flexible arguments as defined by {@link JmsListener}.
52
52
*
53
53
* <p>This post-processor is automatically registered by Spring's
54
- * {@code <jms:annotation-driven>} XML element, and also by the @ {@link EnableJms}
54
+ * {@code <jms:annotation-driven>} XML element, and also by the {@link EnableJms}
55
55
* annotation.
56
56
*
57
57
* <p>Auto-detect any {@link JmsListenerConfigurer} instances in the container,
58
58
* allowing for customization of the registry to be used, the default container
59
59
* factory or for fine-grained control over endpoints registration. See
60
- * @ {@link EnableJms} Javadoc for complete usage details.
60
+ * {@link EnableJms} Javadoc for complete usage details.
61
61
*
62
62
* @author Stephane Nicoll
63
63
* @since 4.1
68
68
* @see JmsListenerEndpointRegistry
69
69
* @see org.springframework.jms.config.AbstractJmsListenerEndpoint
70
70
* @see MethodJmsListenerEndpoint
71
- * @see MessageListenerFactory
72
71
*/
73
72
public class JmsListenerAnnotationBeanPostProcessor implements BeanPostProcessor , Ordered ,
74
73
ApplicationContextAware , ApplicationListener <ContextRefreshedEvent > {
@@ -78,21 +77,23 @@ public class JmsListenerAnnotationBeanPostProcessor implements BeanPostProcessor
78
77
*/
79
78
static final String DEFAULT_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME = "jmsListenerContainerFactory" ;
80
79
81
- private final AtomicInteger counter = new AtomicInteger ();
82
-
83
- private ApplicationContext applicationContext ;
84
80
85
81
private JmsListenerEndpointRegistry endpointRegistry ;
86
82
87
83
private String containerFactoryBeanName = DEFAULT_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME ;
88
84
89
85
private final JmsHandlerMethodFactoryAdapter jmsHandlerMethodFactory = new JmsHandlerMethodFactoryAdapter ();
90
86
87
+ private ApplicationContext applicationContext ;
88
+
91
89
private final JmsListenerEndpointRegistrar registrar = new JmsListenerEndpointRegistrar ();
92
90
91
+ private final AtomicInteger counter = new AtomicInteger ();
92
+
93
+
93
94
@ Override
94
- public void setApplicationContext ( ApplicationContext applicationContext ) {
95
- this . applicationContext = applicationContext ;
95
+ public int getOrder ( ) {
96
+ return LOWEST_PRECEDENCE ;
96
97
}
97
98
98
99
/**
@@ -105,8 +106,7 @@ public void setEndpointRegistry(JmsListenerEndpointRegistry endpointRegistry) {
105
106
106
107
/**
107
108
* Set the name of the {@link JmsListenerContainerFactory} to use by default.
108
- * <p/>If none is specified, {@value #DEFAULT_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME}
109
- * is assumed to be defined.
109
+ * <p>If none is specified, "jmsListenerContainerFactory" is assumed to be defined.
110
110
*/
111
111
public void setContainerFactoryBeanName (String containerFactoryBeanName ) {
112
112
this .containerFactoryBeanName = containerFactoryBeanName ;
@@ -125,10 +125,11 @@ public void setJmsHandlerMethodFactory(JmsHandlerMethodFactory jmsHandlerMethodF
125
125
}
126
126
127
127
@ Override
128
- public int getOrder ( ) {
129
- return LOWEST_PRECEDENCE ;
128
+ public void setApplicationContext ( ApplicationContext applicationContext ) {
129
+ this . applicationContext = applicationContext ;
130
130
}
131
131
132
+
132
133
@ Override
133
134
public Object postProcessBeforeInitialization (Object bean , String beanName ) throws BeansException {
134
135
return bean ;
@@ -162,17 +163,17 @@ protected void processJmsListener(JmsListener jmsListener, Method method, Object
162
163
catch (NoSuchMethodException ex ) {
163
164
throw new IllegalStateException (String .format (
164
165
"@JmsListener method '%s' found on bean target class '%s', " +
165
- "but not found in any interface(s) for bean JDK proxy. Either " +
166
- "pull the method up to an interface or switch to subclass (CGLIB) " +
167
- "proxies by setting proxy-target-class/proxyTargetClass " +
168
- "attribute to 'true'" , method .getName (), method .getDeclaringClass ().getSimpleName ()));
166
+ "but not found in any interface(s) for bean JDK proxy. Either " +
167
+ "pull the method up to an interface or switch to subclass (CGLIB) " +
168
+ "proxies by setting proxy-target-class/proxyTargetClass " +
169
+ "attribute to 'true'" , method .getName (), method .getDeclaringClass ().getSimpleName ()));
169
170
}
170
171
}
171
172
172
173
MethodJmsListenerEndpoint endpoint = new MethodJmsListenerEndpoint ();
173
174
endpoint .setBean (bean );
174
175
endpoint .setMethod (method );
175
- endpoint .setJmsHandlerMethodFactory (jmsHandlerMethodFactory );
176
+ endpoint .setJmsHandlerMethodFactory (this . jmsHandlerMethodFactory );
176
177
endpoint .setId (getEndpointId (jmsListener ));
177
178
endpoint .setDestination (jmsListener .destination ());
178
179
if (StringUtils .hasText (jmsListener .selector ())) {
@@ -189,12 +190,12 @@ protected void processJmsListener(JmsListener jmsListener, Method method, Object
189
190
String containerFactoryBeanName = jmsListener .containerFactory ();
190
191
if (StringUtils .hasText (containerFactoryBeanName )) {
191
192
try {
192
- factory = applicationContext .getBean (containerFactoryBeanName , JmsListenerContainerFactory .class );
193
+ factory = this . applicationContext .getBean (containerFactoryBeanName , JmsListenerContainerFactory .class );
193
194
}
194
- catch (NoSuchBeanDefinitionException e ) {
195
- throw new BeanInitializationException ("Could not register jms listener endpoint on ["
196
- + method + "], no " + JmsListenerContainerFactory .class .getSimpleName () + " with id '"
197
- + containerFactoryBeanName + "' was found in the application context" , e );
195
+ catch (NoSuchBeanDefinitionException ex ) {
196
+ throw new BeanInitializationException ("Could not register jms listener endpoint on [" +
197
+ method + "], no " + JmsListenerContainerFactory .class .getSimpleName () + " with id '" +
198
+ containerFactoryBeanName + "' was found in the application context" , ex );
198
199
}
199
200
}
200
201
@@ -214,22 +215,20 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
214
215
configurer .configureJmsListeners (registrar );
215
216
}
216
217
217
- registrar .setApplicationContext (this .applicationContext );
218
+ this . registrar .setApplicationContext (this .applicationContext );
218
219
219
- if (registrar .getEndpointRegistry () == null ) {
220
- if (endpointRegistry == null ) {
221
- endpointRegistry = applicationContext
222
- .getBean (AnnotationConfigUtils .JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME ,
223
- JmsListenerEndpointRegistry .class );
220
+ if (this .registrar .getEndpointRegistry () == null ) {
221
+ if (this .endpointRegistry == null ) {
222
+ this .endpointRegistry = this .applicationContext .getBean (
223
+ AnnotationConfigUtils .JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME , JmsListenerEndpointRegistry .class );
224
224
}
225
- registrar .setEndpointRegistry (endpointRegistry );
225
+ this . registrar .setEndpointRegistry (this . endpointRegistry );
226
226
}
227
227
228
228
if (this .containerFactoryBeanName != null ) {
229
- registrar .setContainerFactoryBeanName (this .containerFactoryBeanName );
229
+ this . registrar .setContainerFactoryBeanName (this .containerFactoryBeanName );
230
230
}
231
231
232
-
233
232
// Set the custom handler method factory once resolved by the configurer
234
233
JmsHandlerMethodFactory handlerMethodFactory = registrar .getJmsHandlerMethodFactory ();
235
234
if (handlerMethodFactory != null ) {
@@ -238,10 +237,10 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
238
237
239
238
// Create all the listeners and starts them
240
239
try {
241
- registrar .afterPropertiesSet ();
240
+ this . registrar .afterPropertiesSet ();
242
241
}
243
- catch (Exception e ) {
244
- throw new BeanInitializationException (e . getMessage (), e );
242
+ catch (Exception ex ) {
243
+ throw new BeanInitializationException ("Failed to initialize JmsListenerEndpointRegistrar" , ex );
245
244
}
246
245
}
247
246
@@ -250,11 +249,11 @@ private String getEndpointId(JmsListener jmsListener) {
250
249
return jmsListener .id ();
251
250
}
252
251
else {
253
- return "org.springframework.jms.JmsListenerEndpointContainer#"
254
- + counter .getAndIncrement ();
252
+ return "org.springframework.jms.JmsListenerEndpointContainer#" + counter .getAndIncrement ();
255
253
}
256
254
}
257
255
256
+
258
257
/**
259
258
* An {@link JmsHandlerMethodFactory} adapter that offers a configurable underlying
260
259
* instance to use. Useful if the factory to use is determined once the endpoints
@@ -265,7 +264,7 @@ private class JmsHandlerMethodFactoryAdapter implements JmsHandlerMethodFactory
265
264
266
265
private JmsHandlerMethodFactory jmsHandlerMethodFactory ;
267
266
268
- private void setJmsHandlerMethodFactory (JmsHandlerMethodFactory jmsHandlerMethodFactory ) {
267
+ public void setJmsHandlerMethodFactory (JmsHandlerMethodFactory jmsHandlerMethodFactory ) {
269
268
this .jmsHandlerMethodFactory = jmsHandlerMethodFactory ;
270
269
}
271
270
@@ -275,10 +274,10 @@ public InvocableHandlerMethod createInvocableHandlerMethod(Object bean, Method m
275
274
}
276
275
277
276
private JmsHandlerMethodFactory getJmsHandlerMethodFactory () {
278
- if (jmsHandlerMethodFactory == null ) {
279
- jmsHandlerMethodFactory = createDefaultJmsHandlerMethodFactory ();
277
+ if (this . jmsHandlerMethodFactory == null ) {
278
+ this . jmsHandlerMethodFactory = createDefaultJmsHandlerMethodFactory ();
280
279
}
281
- return jmsHandlerMethodFactory ;
280
+ return this . jmsHandlerMethodFactory ;
282
281
}
283
282
284
283
private JmsHandlerMethodFactory createDefaultJmsHandlerMethodFactory () {
0 commit comments