public
Last active

BeanPostProcessor To Apply a Handler Advice

  • Download Gist
AdvisePolledFlow-context.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
 
<int:channel id="input">
<int:queue/>
</int:channel>
<int:channel id="output">
<int:queue/>
</int:channel>
<int:service-activator id="myService" input-channel="input" output-channel="output">
<bean class="org.springframework.integration.aop.AdvisePolledFlow$Service" />
<int:poller fixed-rate="500" />
</int:service-activator>
 
<bean class="org.springframework.integration.aop.AdvisePolledFlow$ApplyAdviceToHandler">
<constructor-arg value="myService.handler" />
</bean>
</beans>
AdvisePolledFlow.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.aop;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
 
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
 
/**
* @author Gary Russell
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class AdvisePolledFlow {
 
private static Object received;
 
@Autowired
private MessageChannel input;
 
@Autowired
private PollableChannel output;
 
@Test
public void test() {
input.send(new GenericMessage<String>("foo"));
assertNotNull(output.receive(1000));
assertNotNull(received);
assertEquals("foo", ((Message<?>) received).getPayload());
}
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* B(F)PP that applies an advice to the handler embedded in an endpoint.
*
* @author Gary Russell
*
*/
public static class ApplyAdviceToHandler implements BeanFactoryPostProcessor, BeanPostProcessor, BeanClassLoaderAware {
 
private String beanName;
 
private ClassLoader beanClassLoader;
 
public ApplyAdviceToHandler(String beanName) {
Assert.notNull(beanName);
this.beanName = beanName;
}
 
/**
* Convert the 'endpoint.handler' alias to the actual bean name
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String[] aliases = beanFactory.getAliases(this.beanName);
if (aliases.length > 0) {
this.beanName = aliases[0];
}
}
 
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
 
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (!(bean instanceof FactoryBean) && beanName.equals(this.beanName)) {
if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {
Class<?> targetClass = AopUtils.getTargetClass(bean);
MyAdvice advice = new MyAdvice();
NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice);
handlerAdvice.addMethodName("handleMessage");
if (AopUtils.canApply(handlerAdvice.getPointcut(), targetClass)) {
((Advised) bean).addAdvice(advice);
}
}
else {
Assert.isInstanceOf(MessageHandler.class, bean, "Can only advise message handlers");
ProxyFactory proxyFactory = new ProxyFactory(bean);
proxyFactory.addAdvice(new MyAdvice());
return proxyFactory.getProxy(this.beanClassLoader);
}
}
return bean;
}
 
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
 
}
 
/**
* Test advice
* @author Gary Russell
*
*/
public static class MyAdvice implements MethodInterceptor {
 
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
received = invocation.getArguments()[0];
return invocation.proceed();
}
 
}
 
/**
* Test Service.
* @author Gary Russell
*
*/
public static class Service {
 
public String handleMessage(String msg) {
return msg;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.