INT-2235-updated
added default filter as AcceptOnceCollectionFilter, chanhged parser to allow filter= to allow null filter
This commit is contained in:
committed by
Mark Fisher
parent
6fbdd71aae
commit
34c51eb635
@@ -20,6 +20,9 @@ import org.springframework.beans.BeanMetadataElement;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.resource.ResourceRetrievingMessageSource;
|
||||
import org.springframework.integration.util.AcceptOnceCollectionFilter;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
@@ -36,7 +39,17 @@ public class ResourceInboundChannelAdapterParser extends AbstractPollingInboundC
|
||||
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ResourceRetrievingMessageSource.class);
|
||||
sourceBuilder.addConstructorArgValue(element.getAttribute("pattern"));
|
||||
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(sourceBuilder, element, "pattern-resolver");
|
||||
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(sourceBuilder, element, "filter");
|
||||
boolean hasFilter = element.hasAttribute("filter");
|
||||
if (hasFilter){
|
||||
String filterValue = element.getAttribute("filter");
|
||||
if (StringUtils.hasText(filterValue)){
|
||||
sourceBuilder.addPropertyReference("filter", filterValue);
|
||||
}
|
||||
}
|
||||
else {
|
||||
BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder.genericBeanDefinition(AcceptOnceCollectionFilter.class);
|
||||
sourceBuilder.addPropertyValue("filter", filterBuilder.getBeanDefinition());
|
||||
}
|
||||
return sourceBuilder.getBeanDefinition();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An implementation of {@link CollectionFilter} that remembers the elements passed in
|
||||
* the previous invocation in order to avoid returning those elements more than once.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 2.1
|
||||
*/
|
||||
public class AcceptOnceCollectionFilter<T> implements CollectionFilter<T> {
|
||||
|
||||
private volatile Collection<T> lastSeenElements = Collections.emptyList();
|
||||
|
||||
public synchronized Collection<T> filter(Collection<T> unfilteredElements) {
|
||||
List<T> filteredElements = new ArrayList<T>();
|
||||
for (T element : unfilteredElements) {
|
||||
if (!this.lastSeenElements.contains(element)) {
|
||||
filteredElements.add(element);
|
||||
}
|
||||
}
|
||||
this.lastSeenElements = unfilteredElements;
|
||||
return filteredElements;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user