简体   繁体   中英

Is it possible to add Advices programmatically to request-handler-advice-chain of an outboundgateway

I'm trying to see if its possible to programmatically add advisers to an outboundgateway ( amqp:outbound-gateway ) defined in xml configuration.

I was exploring the possibilities using BeanFactoryPostProcessor but could not find a way.

I noticed the bean is of the type EventDrivenConsumer when it is retrieved by bean name of the outbound gateway. But the method to add advisers is defined in AbstractReplyProducingMessageHandler . Is there a way to access this method or any other means of achieving this?

Thanks in advance for any help.

Update with the working code based on the accepted answer below:

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    // retrieve the gateway bean definition by its name
    BeanDefinition gatewayBeanDefinition = beanFactory
            .getBeanDefinition("myOutboundgateway");

    if (gatewayBeanDefinition != null) {

        while (gatewayBeanDefinition.getOriginatingBeanDefinition() != null) {
            gatewayBeanDefinition = gatewayBeanDefinition.getOriginatingBeanDefinition();
        }

        PropertyValue handler = gatewayBeanDefinition.getPropertyValues().getPropertyValue("handler");

        if (handler != null) {

            String handlerBeanName = ((RuntimeBeanReference) handler.getValue()).getBeanName();

            BeanDefinition handlerBeanDefinition = beanFactory.getBeanDefinition(handlerBeanName);
            while (handlerBeanDefinition.getOriginatingBeanDefinition() != null) {
                handlerBeanDefinition = handlerBeanDefinition.getOriginatingBeanDefinition();
            }

            ManagedList adviceChain = new ManagedList();
            // myAdvice is the advice that extend AbstractRequestHandlerAdvice as recommended and registered as a bean
            adviceChain.add(new RuntimeBeanReference("myAdvice"));
            // this code simply sets the adviceChain, but other situations might require adding an adviser to an already existing list.
            handlerBeanDefinition.getPropertyValues().add("adviceChain", adviceChain);
        }

    }
}

With a handler that produces replies AbstractReplyProducingMessageHandler (ARPMH), we have to apply the advices internally to the handleRequestMessage method; the reason being that we don't want to advise the downstream flow, just this endpoint.

With handlers that don't produce output, the entire handler is advised.

You can't change the advice chain in an ARPMH after it has been initialized - the chain is applied during afterPropertiesSet() .

Yes, you can modify the bean definition in a BFPP but finding the bean name of the handler can be tricky ( ClassName + #n where n is the parse order) - it is registered with an alias endpoint.handler but you can't find a bean definition by its alias.

You could get a reference to the bean definition via the handler property on a bean definition for a ConsumerEndpointFactoryBean ; once you have a reference to the BD, you can modify the advice chain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM