INT-4085: Access ARPMH from RequestHandlerAdvice
JIRA: https://jira.spring.io/browse/INT-4085 Polishing - PR Comments
This commit is contained in:
committed by
Artem Bilan
parent
2a66f39df1
commit
d0008c368e
@@ -138,6 +138,18 @@ public abstract class AbstractReplyProducingMessageHandler extends AbstractMessa
|
||||
protected abstract Object handleRequestMessage(Message<?> requestMessage);
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of this interface is used to wrap the
|
||||
* {@link AbstractReplyProducingMessageHandler#handleRequestMessage(Message)}
|
||||
* method. Also allows access to the underlying
|
||||
* {@link AbstractReplyProducingMessageHandler} to obtain properties.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.2
|
||||
*
|
||||
* @see #getAdvisedHandler()
|
||||
*
|
||||
*/
|
||||
public interface RequestHandler {
|
||||
|
||||
Object handleRequestMessage(Message<?> requestMessage);
|
||||
@@ -145,6 +157,18 @@ public abstract class AbstractReplyProducingMessageHandler extends AbstractMessa
|
||||
@Override
|
||||
String toString();
|
||||
|
||||
/**
|
||||
* Utility method, intended for use in message handler advice classes to get
|
||||
* information about the advised object. For example:
|
||||
* <p>
|
||||
* {@code ((AbstractReplyProducingMessageHandler.RequestHandler)
|
||||
* invocation.getThis()).getAdvisedHandler().getComponentName()}
|
||||
* @return the outer class instance.
|
||||
*
|
||||
* @since 4.3.2
|
||||
*/
|
||||
AbstractReplyProducingMessageHandler getAdvisedHandler();
|
||||
|
||||
}
|
||||
|
||||
private class AdvisedRequestHandler implements RequestHandler {
|
||||
@@ -159,6 +183,11 @@ public abstract class AbstractReplyProducingMessageHandler extends AbstractMessa
|
||||
return AbstractReplyProducingMessageHandler.this.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractReplyProducingMessageHandler getAdvisedHandler() {
|
||||
return AbstractReplyProducingMessageHandler.this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -132,6 +132,8 @@ public class AdvisedMessageHandlerTests {
|
||||
return "baz";
|
||||
}
|
||||
};
|
||||
String componentName = "testComponentName";
|
||||
handler.setComponentName(componentName);
|
||||
QueueChannel replies = new QueueChannel();
|
||||
handler.setOutputChannel(replies);
|
||||
Message<String> message = new GenericMessage<String>("Hello, world!");
|
||||
@@ -153,6 +155,17 @@ public class AdvisedMessageHandlerTests {
|
||||
|
||||
List<Advice> adviceChain = new ArrayList<Advice>();
|
||||
adviceChain.add(advice);
|
||||
final AtomicReference<String> compName = new AtomicReference<String>();
|
||||
adviceChain.add(new AbstractRequestHandlerAdvice() {
|
||||
|
||||
@Override
|
||||
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
|
||||
compName.set(((AbstractReplyProducingMessageHandler.RequestHandler) target).getAdvisedHandler()
|
||||
.getComponentName());
|
||||
return callback.execute();
|
||||
}
|
||||
|
||||
});
|
||||
handler.setAdviceChain(adviceChain);
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
@@ -163,6 +176,8 @@ public class AdvisedMessageHandlerTests {
|
||||
assertNotNull(reply);
|
||||
assertEquals("baz", reply.getPayload());
|
||||
|
||||
assertEquals(componentName, compName.get());
|
||||
|
||||
Message<?> success = successChannel.receive(1000);
|
||||
assertNotNull(success);
|
||||
assertEquals("Hello, world!", ((AdviceMessage<?>) success).getInputMessage().getPayload());
|
||||
|
||||
Reference in New Issue
Block a user