INT-1187 added bean resolution support for expression based filters

This commit is contained in:
Mark Fisher
2010-06-17 20:25:26 +00:00
parent 708218277c
commit b62b8f0f80
4 changed files with 76 additions and 22 deletions

View File

@@ -16,6 +16,9 @@
package org.springframework.integration.filter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.convert.ConversionService;
import org.springframework.integration.core.Message;
import org.springframework.integration.handler.AbstractMessageProcessor;
@@ -29,7 +32,7 @@ import org.springframework.util.Assert;
*
* @author Mark Fisher
*/
abstract class AbstractMessageProcessingSelector implements MessageSelector {
abstract class AbstractMessageProcessingSelector implements MessageSelector, BeanFactoryAware {
private final MessageProcessor messageProcessor;
@@ -46,6 +49,12 @@ abstract class AbstractMessageProcessingSelector implements MessageSelector {
}
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (this.messageProcessor instanceof BeanFactoryAware) {
((BeanFactoryAware) this.messageProcessor).setBeanFactory(beanFactory);
}
}
public final boolean accept(Message<?> message) {
Object result = this.messageProcessor.processMessage(message);
Assert.notNull(result, "result must not be null");

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.filter;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
@@ -91,6 +92,9 @@ public class MessageFilter extends AbstractReplyProducingMessageHandler {
if (this.selector instanceof AbstractMessageProcessingSelector) {
((AbstractMessageProcessingSelector) this.selector).setConversionService(this.getConversionService());
}
if (this.selector instanceof BeanFactoryAware) {
((BeanFactoryAware) this.selector).setBeanFactory(this.getBeanFactory());
}
}
@Override

View File

@@ -1,22 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
<channel id="input"/>
<channel id="positives">
<queue/>
</channel>
<channel id="discards">
<channel id="negatives">
<queue/>
</channel>
<filter input-channel="input" expression="payload > 0" output-channel="positives" discard-channel="discards"/>
<channel id="evens">
<queue/>
</channel>
<channel id="odds">
<queue/>
</channel>
<filter input-channel="simpleInput" expression="payload > 0" output-channel="positives" discard-channel="negatives"/>
<filter input-channel="beanResolvingInput" expression="@testBean.isEven(payload)" output-channel="evens" discard-channel="odds"/>
<beans:bean id="testBean" class="org.springframework.integration.filter.SpelFilterIntegrationTests$TestBean"/>
</beans:beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@@ -23,7 +23,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.GenericMessage;
@@ -37,28 +36,60 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
public class SpelFilterIntegrationTests {
@Autowired @Qualifier("input")
private MessageChannel input;
@Autowired
private MessageChannel simpleInput;
@Autowired @Qualifier("positives")
@Autowired
private PollableChannel positives;
@Autowired @Qualifier("discards")
private PollableChannel discards;
@Autowired
private PollableChannel negatives;
@Autowired
private MessageChannel beanResolvingInput;
@Autowired
private PollableChannel evens;
@Autowired
private PollableChannel odds;
@Test
public void filter() {
this.input.send(new GenericMessage<Integer>(1));
this.input.send(new GenericMessage<Integer>(0));
this.input.send(new GenericMessage<Integer>(99));
this.input.send(new GenericMessage<Integer>(-99));
public void simpleExpressionBasedFilter() {
this.simpleInput.send(new GenericMessage<Integer>(1));
this.simpleInput.send(new GenericMessage<Integer>(0));
this.simpleInput.send(new GenericMessage<Integer>(99));
this.simpleInput.send(new GenericMessage<Integer>(-99));
assertEquals(new Integer(1), positives.receive(0).getPayload());
assertEquals(new Integer(99), positives.receive(0).getPayload());
assertEquals(new Integer(0), discards.receive(0).getPayload());
assertEquals(new Integer(-99), discards.receive(0).getPayload());
assertEquals(new Integer(0), negatives.receive(0).getPayload());
assertEquals(new Integer(-99), negatives.receive(0).getPayload());
assertNull(positives.receive(0));
assertNull(discards.receive(0));
assertNull(negatives.receive(0));
}
@Test
public void beanResolvingExpressionBasedFilter() {
this.beanResolvingInput.send(new GenericMessage<Integer>(1));
this.beanResolvingInput.send(new GenericMessage<Integer>(2));
this.beanResolvingInput.send(new GenericMessage<Integer>(9));
this.beanResolvingInput.send(new GenericMessage<Integer>(22));
assertEquals(new Integer(1), odds.receive(0).getPayload());
assertEquals(new Integer(9), odds.receive(0).getPayload());
assertEquals(new Integer(2), evens.receive(0).getPayload());
assertEquals(new Integer(22), evens.receive(0).getPayload());
assertNull(odds.receive(0));
assertNull(evens.receive(0));
}
@SuppressWarnings("unused")
private static class TestBean {
public boolean isEven(int number) {
return number % 2 == 0;
}
}
}