Merge pull request #201 from markfisher/INT-2235 which builds upon PR #181 from olegz/INT-2235

fixed polling period and renamed test class

  ElementFilter now CollectionFilter
This commit is contained in:
Mark Fisher
2011-11-22 14:09:11 -05:00
13 changed files with 567 additions and 5 deletions

View File

@@ -53,6 +53,7 @@ public class IntegrationNamespaceHandler extends AbstractIntegrationNamespaceHan
registerBeanDefinitionParser("claim-check-in", new ClaimCheckInParser());
registerBeanDefinitionParser("claim-check-out", new ClaimCheckOutParser());
registerBeanDefinitionParser("inbound-channel-adapter", new MethodInvokingInboundChannelAdapterParser());
registerBeanDefinitionParser("resource-inbound-channel-adapter", new ResourceInboundChannelAdapterParser());
registerBeanDefinitionParser("outbound-channel-adapter", new MethodInvokingOutboundChannelAdapterParser());
registerBeanDefinitionParser("logging-channel-adapter", new LoggingChannelAdapterParser());
registerBeanDefinitionParser("gateway", new GatewayParser());

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2002-2011 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.config.xml;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.resource.ResourceMessageSource;
import org.w3c.dom.Element;
/**
* Parser for 'resource-inbound-channel-adapter'
*
* @author Oleg Zhurakousky
* @since 2.1
*/
public class ResourceInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
@Override
protected BeanMetadataElement parseSource(Element element, ParserContext parserContext) {
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ResourceMessageSource.class);
sourceBuilder.addConstructorArgValue(element.getAttribute("pattern"));
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(sourceBuilder, element, "pattern-resolver");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(sourceBuilder, element, "filter");
return sourceBuilder.getBeanDefinition();
}
}

View File

@@ -16,9 +16,6 @@
package org.springframework.integration.endpoint;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.context.NamedComponent;
@@ -36,8 +33,6 @@ import org.springframework.util.Assert;
* @author Oleg Zhurakousky
*/
public class SourcePollingChannelAdapter extends AbstractPollingEndpoint implements TrackableComponent {
private final Log logger = LogFactory.getLog(this.getClass());
private volatile MessageSource<?> source;

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2002-2011 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.resource;
import java.util.Arrays;
import java.util.Collection;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.integration.MessagingException;
import org.springframework.integration.endpoint.AbstractMessageSource;
import org.springframework.integration.util.CollectionFilter;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
/**
* Implementation of {@link MessageSource} based on {@link ResourcePatternResolver} which will
* attempt to resolve {@link Resource}s based on the pattern specified.
*
* @author Oleg Zhurakousky
* @author Mark Fisher
* @since 2.1
*/
public class ResourceMessageSource extends AbstractMessageSource<Resource[]> implements ApplicationContextAware, InitializingBean {
private final String pattern;
private volatile ApplicationContext applicationContext;
private volatile ResourcePatternResolver patternResolver;
private volatile CollectionFilter<Resource> filter;
public ResourceMessageSource(String pattern) {
Assert.hasText(pattern, "pattern must not be empty");
this.pattern = pattern;
}
public void setPatternResolver(ResourcePatternResolver patternResolver) {
this.patternResolver = patternResolver;
}
public void setFilter(CollectionFilter<Resource> filter) {
this.filter = filter;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public void afterPropertiesSet() {
if (this.patternResolver == null) {
if (this.applicationContext instanceof ResourcePatternResolver) {
this.patternResolver = this.applicationContext;
}
}
Assert.notNull(this.patternResolver, "no 'patternResolver' available");
}
@Override
protected Resource[] doReceive() {
try {
Resource[] resources = this.patternResolver.getResources(this.pattern);
if (this.filter != null && !ObjectUtils.isEmpty(resources)) {
Collection<Resource> filteredResources = this.filter.filter(Arrays.asList(resources));
if (CollectionUtils.isEmpty(filteredResources)) {
resources = null;
}
else {
resources = filteredResources.toArray(new Resource[0]);
}
}
return resources;
}
catch (Exception e) {
throw new MessagingException("Attempt to retrieve Resources failed", e);
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2002-2011 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.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
/**
* An implementation of {@link CollectionFilter} which will queue all items that have been seen until
* the queue reaches its capacity after which one item from the queue will be purged to make room for a
* new item to be added. Note that however unlikely the removed item will now appear as unprocessed
* so it is highly recommended to move/delete resources which correspond to the underlying items once processing
* is done to eliminate duplicate processing.
*
* @author Oleg Zhurakousky
* @author Mark Fisher
* @since 2.1
*/
public class AcceptOnceUntilPurgedCollectionFilter<T> implements CollectionFilter<T> {
private final Log logger = LogFactory.getLog(this.getClass());
private final Queue<T> seenItems;
private final Object seenQueueMonitor = new Object();
public AcceptOnceUntilPurgedCollectionFilter() {
this(Integer.MAX_VALUE);
}
public AcceptOnceUntilPurgedCollectionFilter(int maxCapacity) {
this.seenItems = new LinkedBlockingQueue<T>(maxCapacity);
}
public Collection<T> filter(Collection<T> unfilteredElements) {
Assert.notNull(unfilteredElements, "'unfilteredElements' must not be null");
List<T> filteredElements = new ArrayList<T>();
if (unfilteredElements.size() > 0) {
for (T element : unfilteredElements) {
if (this.accept(element)) {
filteredElements.add(element);
}
}
}
return filteredElements;
}
private boolean accept(T item) {
synchronized (this.seenQueueMonitor) {
boolean accepted = false;
if (!this.seenItems.contains(item)) {
accepted = this.seenItems.offer(item);
if (!accepted){
logger.warn("'seenQueueMonitor' queue of AcceptOnceUntilPurgedElementFilter is at the capacity, " +
"evicting one item to make room for another");
this.seenItems.poll();
accepted = this.seenItems.offer(item);
}
}
return accepted;
}
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2002-2011 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.util;
import java.util.Collection;
/**
* Base strategy for filtering out a subset of a Collection of elements.
*
* @author Oleg Zhurakousky
* @author Mark Fisher
* @since 2.1
*/
public interface CollectionFilter<T> {
Collection<T> filter(Collection<T> unfilteredElements);
}

View File

@@ -716,6 +716,78 @@
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="resource-inbound-channel-adapter">
<xsd:annotation>
<xsd:documentation>
Defines a Channel Adapter that receives Resource(s) and sends them to a
MessageChannel identified via 'channel' attribute.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:sequence>
<xsd:element name="poller" type="basePollerType" />
</xsd:sequence>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Component identifier
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="channel" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Channel where Message will be sent to
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="filter" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Reference to the implementation of org.springframework.integration.util.ElementFilter.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.integration.util.ElementFilter" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="auto-startup" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Lifecycle attribute signaling if this component should be started during Application Context startup.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="pattern" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Location pattern expression (e.g., "/**/*.txt")
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="pattern-resolver" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Reference to a org.springframework.core.io.support.ResourcePatternResolver.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="send-timeout" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Maximum amount of time in milliseconds to wait when sending a message to the channel if such channel may block.
For example, a Queue Channel can block until space is available if its maximum capacity has been reached.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="outbound-channel-adapter">
<xsd:annotation>

View File

@@ -0,0 +1,136 @@
/*
* Copyright 2002-2011 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.resource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.integration.Message;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.util.CollectionFilter;
import org.springframework.util.CollectionUtils;
/**
* @author Oleg Zhurakousky
* @since 2.1
*/
public class ResourceInboundChannelAdapterParserTests {
@Test
public void testDefaultConfig(){
ApplicationContext context = new ClassPathXmlApplicationContext("ResourcePatternResolver-config.xml", this.getClass());
SourcePollingChannelAdapter resourceAdapter = context.getBean("resourceAdapterDefault", SourcePollingChannelAdapter.class);
ResourceMessageSource source = TestUtils.getPropertyValue(resourceAdapter, "source", ResourceMessageSource.class);
assertNotNull(source);
boolean autoStartup = TestUtils.getPropertyValue(resourceAdapter, "autoStartup", Boolean.class);
assertFalse(autoStartup);
assertEquals("/**/*", TestUtils.getPropertyValue(source, "pattern"));
assertEquals(context, TestUtils.getPropertyValue(source, "patternResolver"));
}
@Test(expected=BeanCreationException.class)
public void testDefaultConfigNoLocationPattern(){
new ClassPathXmlApplicationContext("ResourcePatternResolver-config-fail.xml", this.getClass());
}
@Test
public void testCustomPatternResolver(){
ApplicationContext context = new ClassPathXmlApplicationContext("ResourcePatternResolver-config-custom.xml", this.getClass());
SourcePollingChannelAdapter resourceAdapter = context.getBean("resourceAdapterDefault", SourcePollingChannelAdapter.class);
ResourceMessageSource source = TestUtils.getPropertyValue(resourceAdapter, "source", ResourceMessageSource.class);
assertNotNull(source);
assertEquals(context.getBean("customResolver"), TestUtils.getPropertyValue(source, "patternResolver"));
}
@SuppressWarnings("unchecked")
@Test
public void testUsage() throws Exception{
File baseDir = new File(System.getProperty("java.io.tmpdir"));
for (int i = 0; i < 10; i++) {
File f = new File(baseDir, "testUsage"+i);
f.createNewFile();
}
ApplicationContext context = new ClassPathXmlApplicationContext("ResourcePatternResolver-config-usage.xml", this.getClass());
QueueChannel resultChannel = context.getBean("resultChannel", QueueChannel.class);
Message<Resource[]> message = (Message<Resource[]>) resultChannel.receive(3000);
assertNotNull(message);
Resource[] resources = message.getPayload();
for (Resource resource : resources) {
assertTrue(resource.getURI().toString().contains("testUsage"));
}
}
@SuppressWarnings("unchecked")
@Test
public void testUsageWithCustomResourceFilter() throws Exception{
File baseDir = new File(System.getProperty("java.io.tmpdir"));
for (int i = 0; i < 10; i++) {
File f = new File(baseDir, "testUsageWithRf"+i);
f.createNewFile();
}
ApplicationContext context = new ClassPathXmlApplicationContext("ResourcePatternResolver-config-usagerf.xml", this.getClass());
SourcePollingChannelAdapter resourceAdapter = context.getBean("resourceAdapterDefault", SourcePollingChannelAdapter.class);
ResourceMessageSource source = TestUtils.getPropertyValue(resourceAdapter, "source", ResourceMessageSource.class);
assertNotNull(source);
assertEquals(context.getBean("rlFilter"), TestUtils.getPropertyValue(source, "filter"));
QueueChannel resultChannel = context.getBean("resultChannel", QueueChannel.class);
Message<Resource[]> message = (Message<Resource[]>) resultChannel.receive(1000);
assertNotNull(message);
message = (Message<Resource[]>) resultChannel.receive(1000);
assertNull(message);
}
public static class OneItemAndNeverAgainResourceListFilter implements CollectionFilter<Resource> {
private volatile boolean once = false;
public Collection<Resource> filter(Collection<Resource> unfilteredResources) {
if (!once && !CollectionUtils.isEmpty(unfilteredResources)) {
once = true;
return Collections.singletonList(unfilteredResources.iterator().next());
}
return null;
}
}
}

View File

@@ -0,0 +1,20 @@
<?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"
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-2.1.xsd">
<int:resource-inbound-channel-adapter id="resourceAdapterDefault" channel="resultChannel" pattern="/**/*"
auto-startup="false" pattern-resolver="customResolver">
<int:poller fixed-rate="1000"/>
</int:resource-inbound-channel-adapter>
<bean id="customResolver" class="org.springframework.core.io.support.PathMatchingResourcePatternResolver"/>
<int:channel id="resultChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,17 @@
<?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"
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-2.1.xsd">
<int:resource-inbound-channel-adapter id="resourceAdapterDefault" channel="resultChannel" auto-startup="false">
<int:poller fixed-rate="1000"/>
</int:resource-inbound-channel-adapter>
<int:channel id="resultChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,18 @@
<?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"
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-2.1.xsd">
<int:resource-inbound-channel-adapter id="resourceAdapterDefault" channel="resultChannel"
pattern="file:#{T(java.lang.System).getProperty('java.io.tmpdir') + 'testUsage*'}">
<int:poller fixed-rate="2000"/>
</int:resource-inbound-channel-adapter>
<int:channel id="resultChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,21 @@
<?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"
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-2.1.xsd">
<int:resource-inbound-channel-adapter id="resourceAdapterDefault" channel="resultChannel"
pattern="file:#{T(java.lang.System).getProperty('java.io.tmpdir') + 'testUsage*'}"
filter="rlFilter" auto-startup="true">
<int:poller fixed-rate="500"/>
</int:resource-inbound-channel-adapter>
<bean id="rlFilter" class="org.springframework.integration.resource.ResourceInboundChannelAdapterParserTests.OneItemAndNeverAgainResourceListFilter"/>
<int:channel id="resultChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,17 @@
<?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"
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-2.1.xsd">
<int:resource-inbound-channel-adapter id="resourceAdapterDefault" channel="resultChannel" pattern="/**/*" auto-startup="false">
<int:poller fixed-rate="1000"/>
</int:resource-inbound-channel-adapter>
<int:channel id="resultChannel">
<int:queue/>
</int:channel>
</beans>