|
1 | 1 | /*
|
2 |
| - * Copyright 2021 the original author or authors. |
| 2 | + * Copyright 2021-2022 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.rabbit.stream.listener;
|
18 | 18 |
|
| 19 | +import org.aopalliance.aop.Advice; |
19 | 20 | import org.apache.commons.logging.Log;
|
20 | 21 | import org.apache.commons.logging.LogFactory;
|
21 | 22 |
|
22 | 23 | import org.springframework.amqp.core.Message;
|
23 | 24 | import org.springframework.amqp.core.MessageListener;
|
24 | 25 | import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
|
25 | 26 | import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
|
| 27 | +import org.springframework.aop.framework.ProxyFactory; |
| 28 | +import org.springframework.aop.support.DefaultPointcutAdvisor; |
26 | 29 | import org.springframework.beans.factory.BeanNameAware;
|
27 | 30 | import org.springframework.lang.Nullable;
|
28 | 31 | import org.springframework.rabbit.stream.support.StreamMessageProperties;
|
@@ -62,6 +65,10 @@ public class StreamListenerContainer implements MessageListenerContainer, BeanNa
|
62 | 65 |
|
63 | 66 | private MessageListener messageListener;
|
64 | 67 |
|
| 68 | + private StreamMessageListener streamListener; |
| 69 | + |
| 70 | + private Advice[] adviceChain; |
| 71 | + |
65 | 72 | /**
|
66 | 73 | * Construct an instance using the provided environment.
|
67 | 74 | * @param environment the environment.
|
@@ -154,6 +161,18 @@ public void setAutoStartup(boolean autoStart) {
|
154 | 161 | public boolean isAutoStartup() {
|
155 | 162 | return this.autoStartup;
|
156 | 163 | }
|
| 164 | + |
| 165 | + /** |
| 166 | + * Set an advice chain to apply to the listener. |
| 167 | + * @param advices the advice chain. |
| 168 | + * @since 2.4.5 |
| 169 | + */ |
| 170 | + public void setAdviceChain(Advice... advices) { |
| 171 | + Assert.notNull(advices, "'advices' cannot be null"); |
| 172 | + Assert.noNullElements(advices, "'advices' cannot have null elements"); |
| 173 | + this.adviceChain = advices; |
| 174 | + } |
| 175 | + |
157 | 176 | @Override
|
158 | 177 | @Nullable
|
159 | 178 | public Object getMessageListener() {
|
@@ -183,26 +202,46 @@ public synchronized void stop() {
|
183 | 202 |
|
184 | 203 | @Override
|
185 | 204 | public void setupMessageListener(MessageListener messageListener) {
|
186 |
| - this.messageListener = messageListener; |
| 205 | + adviseIfNeeded(messageListener); |
187 | 206 | this.builder.messageHandler((context, message) -> {
|
188 |
| - if (messageListener instanceof StreamMessageListener) { |
189 |
| - ((StreamMessageListener) messageListener).onStreamMessage(message, context); |
| 207 | + if (this.streamListener != null) { |
| 208 | + this.streamListener.onStreamMessage(message, context); |
190 | 209 | }
|
191 | 210 | else {
|
192 | 211 | Message message2 = this.streamConverter.toMessage(message, new StreamMessageProperties(context));
|
193 |
| - if (messageListener instanceof ChannelAwareMessageListener) { |
| 212 | + if (this.messageListener instanceof ChannelAwareMessageListener) { |
194 | 213 | try {
|
195 |
| - ((ChannelAwareMessageListener) messageListener).onMessage(message2, null); |
| 214 | + ((ChannelAwareMessageListener) this.messageListener).onMessage(message2, null); |
196 | 215 | }
|
197 | 216 | catch (Exception e) { // NOSONAR
|
198 | 217 | this.logger.error("Listner threw an exception", e);
|
199 | 218 | }
|
200 | 219 | }
|
201 | 220 | else {
|
202 |
| - messageListener.onMessage(message2); |
| 221 | + this.messageListener.onMessage(message2); |
203 | 222 | }
|
204 | 223 | }
|
205 | 224 | });
|
206 | 225 | }
|
207 | 226 |
|
| 227 | + private void adviseIfNeeded(MessageListener messageListener) { |
| 228 | + this.messageListener = messageListener; |
| 229 | + if (messageListener instanceof StreamMessageListener) { |
| 230 | + this.streamListener = (StreamMessageListener) messageListener; |
| 231 | + } |
| 232 | + if (this.adviceChain != null && this.adviceChain.length > 0) { |
| 233 | + ProxyFactory factory = new ProxyFactory(messageListener); |
| 234 | + for (Advice advice : this.adviceChain) { |
| 235 | + factory.addAdvisor(new DefaultPointcutAdvisor(advice)); |
| 236 | + } |
| 237 | + factory.setInterfaces(messageListener.getClass().getInterfaces()); |
| 238 | + if (this.streamListener != null) { |
| 239 | + this.streamListener = (StreamMessageListener) factory.getProxy(getClass().getClassLoader()); |
| 240 | + } |
| 241 | + else { |
| 242 | + this.messageListener = (MessageListener) factory.getProxy(getClass().getClassLoader()); |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + |
208 | 247 | }
|
0 commit comments