1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.jms.config;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.BeanFactoryAware;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
26 import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
27 import org.springframework.util.Assert;
28
29
30
31
32
33
34
35
36
37
38 public class JmsListenerEndpointRegistrar implements BeanFactoryAware, InitializingBean {
39
40 private JmsListenerEndpointRegistry endpointRegistry;
41
42 private MessageHandlerMethodFactory messageHandlerMethodFactory;
43
44 private JmsListenerContainerFactory<?> containerFactory;
45
46 private String containerFactoryBeanName;
47
48 private BeanFactory beanFactory;
49
50 private final List<JmsListenerEndpointDescriptor> endpointDescriptors =
51 new ArrayList<JmsListenerEndpointDescriptor>();
52
53
54
55
56
57 public void setEndpointRegistry(JmsListenerEndpointRegistry endpointRegistry) {
58 this.endpointRegistry = endpointRegistry;
59 }
60
61
62
63
64
65 public JmsListenerEndpointRegistry getEndpointRegistry() {
66 return this.endpointRegistry;
67 }
68
69
70
71
72
73
74
75
76
77 public void setMessageHandlerMethodFactory(MessageHandlerMethodFactory messageHandlerMethodFactory) {
78 this.messageHandlerMethodFactory = messageHandlerMethodFactory;
79 }
80
81
82
83
84 public MessageHandlerMethodFactory getMessageHandlerMethodFactory() {
85 return this.messageHandlerMethodFactory;
86 }
87
88
89
90
91
92
93
94 public void setContainerFactory(JmsListenerContainerFactory<?> containerFactory) {
95 this.containerFactory = containerFactory;
96 }
97
98
99
100
101
102
103
104
105 public void setContainerFactoryBeanName(String containerFactoryBeanName) {
106 this.containerFactoryBeanName = containerFactoryBeanName;
107 }
108
109
110
111
112
113 @Override
114 public void setBeanFactory(BeanFactory beanFactory) {
115 this.beanFactory = beanFactory;
116 }
117
118
119 @Override
120 public void afterPropertiesSet() {
121 registerAllEndpoints();
122 }
123
124 protected void registerAllEndpoints() {
125 for (JmsListenerEndpointDescriptor descriptor : this.endpointDescriptors) {
126 this.endpointRegistry.registerListenerContainer(descriptor.endpoint, resolveContainerFactory(descriptor));
127 }
128 }
129
130 private JmsListenerContainerFactory<?> resolveContainerFactory(JmsListenerEndpointDescriptor descriptor) {
131 if (descriptor.containerFactory != null) {
132 return descriptor.containerFactory;
133 }
134 else if (this.containerFactory != null) {
135 return this.containerFactory;
136 }
137 else if (this.containerFactoryBeanName != null) {
138 Assert.state(this.beanFactory != null, "BeanFactory must be set to obtain container factory by bean name");
139 this.containerFactory = this.beanFactory.getBean(
140 this.containerFactoryBeanName, JmsListenerContainerFactory.class);
141 return this.containerFactory;
142 }
143 else {
144 throw new IllegalStateException("Could not resolve the " +
145 JmsListenerContainerFactory.class.getSimpleName() + " to use for [" +
146 descriptor.endpoint + "] no factory was given and no default is set.");
147 }
148 }
149
150
151
152
153
154
155
156 public void registerEndpoint(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> factory) {
157 Assert.notNull(endpoint, "Endpoint must be set");
158 Assert.hasText(endpoint.getId(), "Endpoint id must be set");
159
160 this.endpointDescriptors.add(new JmsListenerEndpointDescriptor(endpoint, factory));
161 }
162
163
164
165
166
167
168
169 public void registerEndpoint(JmsListenerEndpoint endpoint) {
170 registerEndpoint(endpoint, null);
171 }
172
173
174 private static class JmsListenerEndpointDescriptor {
175
176 public final JmsListenerEndpoint endpoint;
177
178 public final JmsListenerContainerFactory<?> containerFactory;
179
180 public JmsListenerEndpointDescriptor(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> containerFactory) {
181 this.endpoint = endpoint;
182 this.containerFactory = containerFactory;
183 }
184 }
185
186 }