INT-2235
added support for resource-inbound-channel-adapter removed pre-fetching logic changed ResourcePatternResolvingMessageSource to return multiple Resources made filter optional
This commit is contained in:
committed by
Mark Fisher
parent
5cac946cf3
commit
193f3d2439
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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="5000"/>
|
||||
</int:resource-inbound-channel-adapter>
|
||||
|
||||
<bean id="rlFilter" class="org.springframework.integration.resource.ResourcePatternResolverParserTests.OneItemAndNeverAgainResourceListFilter"/>
|
||||
|
||||
<int:channel id="resultChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -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>
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.io.File;
|
||||
|
||||
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.ElementFilter;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class ResourcePatternResolverParserTests {
|
||||
|
||||
@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 ElementFilter<Resource> {
|
||||
|
||||
private volatile boolean once = false;
|
||||
|
||||
public Resource filter(Resource unfilteredElement) {
|
||||
|
||||
if (!once){
|
||||
once = true;
|
||||
return unfilteredElement;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user