INT-1263, added more changes and tests related valdating MessageHistory in every module

This commit is contained in:
Oleg Zhurakousky
2010-09-22 18:44:51 -04:00
parent dee91f6871
commit b6e058c59b
46 changed files with 367 additions and 114 deletions

View File

@@ -2,8 +2,10 @@ package org.springframework.integration.file;
import org.springframework.core.io.Resource;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.file.entries.*;
import java.io.File;
@@ -27,7 +29,7 @@ import java.util.regex.Pattern;
*
* @author Josh Long
*/
public abstract class AbstractInboundRemoteFileSystemSynchronizingMessageSource<Y, T extends AbstractInboundRemoteFileSystemSychronizer<Y>> extends AbstractEndpoint implements MessageSource<File> {
public abstract class AbstractInboundRemoteFileSystemSynchronizingMessageSource<Y, T extends AbstractInboundRemoteFileSystemSychronizer<Y>> extends MessageProducerSupport implements MessageSource<File> {
/**
* Extension used when downloading files. We change it right after we know it's downloaded
*/
@@ -74,7 +76,8 @@ public abstract class AbstractInboundRemoteFileSystemSynchronizingMessageSource<
this.remotePredicate = remotePredicate;
}
private EntryListFilter<File> buildFilter() {
@SuppressWarnings("unchecked")
private EntryListFilter<File> buildFilter() {
FileEntryNamer fileEntryNamer = new FileEntryNamer();
Pattern completePattern = Pattern.compile("^.*(?<!" + INCOMPLETE_EXTENSION + ")$");
return new CompositeEntryListFilter<File>(
@@ -83,33 +86,42 @@ public abstract class AbstractInboundRemoteFileSystemSynchronizingMessageSource<
}
@Override
protected void onInit() throws Exception {
if (this.remotePredicate != null) {
this.synchronizer.setFilter(this.remotePredicate);
}
protected void onInit() {
try {
if (this.remotePredicate != null) {
this.synchronizer.setFilter(this.remotePredicate);
}
if (this.autoCreateDirectories) {
if ((this.localDirectory != null) && !this.localDirectory.exists() && this.localDirectory.getFile().mkdirs())
logger.debug("the localDirectory " + this.localDirectory + " doesn't exist");
}
if (this.autoCreateDirectories) {
if ((this.localDirectory != null) && !this.localDirectory.exists() && this.localDirectory.getFile().mkdirs())
logger.debug("the localDirectory " + this.localDirectory + " doesn't exist");
}
/**
* Handles making sure the remote files get here in one piece
*/
this.synchronizer.setLocalDirectory(this.localDirectory);
this.synchronizer.setTaskScheduler(this.getTaskScheduler());
this.synchronizer.setBeanFactory(this.getBeanFactory());
this.synchronizer.setPhase(this.getPhase());
this.synchronizer.setBeanName(this.getComponentName());
/**
* Handles making sure the remote files get here in one piece
*/
this.synchronizer.setLocalDirectory(this.localDirectory);
this.synchronizer.setTaskScheduler(this.getTaskScheduler());
this.synchronizer.setBeanFactory(this.getBeanFactory());
this.synchronizer.setPhase(this.getPhase());
this.synchronizer.setBeanName(this.getComponentName());
/**
* Handles forwarding files once they ultimately appear in the {@link #localDirectory}
*/
this.fileSource = new FileReadingMessageSource();
this.fileSource.setFilter(buildFilter());
this.fileSource.setDirectory(this.localDirectory.getFile());
this.fileSource.afterPropertiesSet();
this.synchronizer.afterPropertiesSet();
/**
* Handles forwarding files once they ultimately appear in the {@link #localDirectory}
*/
this.fileSource = new FileReadingMessageSource();
this.fileSource.setFilter(buildFilter());
this.fileSource.setDirectory(this.localDirectory.getFile());
this.fileSource.afterPropertiesSet();
this.synchronizer.afterPropertiesSet();
} catch (Exception e) {
if (e instanceof RuntimeException){
throw (RuntimeException)e;
} else {
throw new MessagingException("Failure during initialization of " + this.getComponentName(), e);
}
}
}
public Message<File> receive() {

View File

@@ -15,21 +15,25 @@
*/
package org.springframework.integration.file;
import java.io.File;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.PriorityBlockingQueue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.aggregator.ResequencingMessageGroupProcessor;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.file.entries.EntryListFilter;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.util.Assert;
import java.io.File;
import java.util.*;
import java.util.concurrent.PriorityBlockingQueue;
/**
* {@link MessageSource} that creates messages from a file system directory. To prevent messages for certain files, you
@@ -52,8 +56,9 @@ import java.util.concurrent.PriorityBlockingQueue;
*
* @author Iwein Fuld
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class FileReadingMessageSource implements MessageSource<File>, InitializingBean {
public class FileReadingMessageSource extends IntegrationObjectSupport implements MessageSource<File>{
private static final int DEFAULT_INTERNAL_QUEUE_CAPACITY = 5;
private static final Log logger = LogFactory.getLog(FileReadingMessageSource.class);
private volatile File directory;
@@ -178,8 +183,7 @@ public class FileReadingMessageSource implements MessageSource<File>, Initializi
this.scanEachPoll = scanEachPoll;
}
@SuppressWarnings({"ResultOfMethodCallIgnored"})
public final void afterPropertiesSet() {
protected void onInit() {
Assert.notNull(directory, "'directory' must not be set before initialization");
if (!this.directory.exists() && this.autoCreateDirectory) {
@@ -254,4 +258,8 @@ public class FileReadingMessageSource implements MessageSource<File>, Initializi
logger.debug("Sent: " + sentMessage);
}
}
public String getComponentType() {
return "file:inbound-channel-adapter";
}
}

View File

@@ -21,6 +21,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.file.DefaultDirectoryScanner;
@@ -47,7 +48,12 @@ public class FileInboundChannelAdapterParserTests {
private ApplicationContext context;
@Autowired
// @Qualifier("inputDirPoller")
private FileReadingMessageSource source;
// @Autowired
// @Qualifier("inputDirPollerWithChannel")
// private FileReadingMessageSource sourceWithChannel;
private DirectFieldAccessor accessor;
@@ -59,6 +65,7 @@ public class FileInboundChannelAdapterParserTests {
@Test
public void channelName() throws Exception {
Object adapter = context.getBean("inputDirPoller");
AbstractMessageChannel channel = context.getBean("inputDirPoller", AbstractMessageChannel.class);
assertEquals("Channel should be available under specified id", "inputDirPoller", channel.getComponentName());
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2002-2009 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.file.config;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Properties;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.test.util.TestUtils;
/**
* @author Oleg Zhurakousky
*
*/
public class FileMessageHistoryTest {
@Test
public void testMessageHistory() throws Exception{
ApplicationContext context = new ClassPathXmlApplicationContext("file-message-history-context.xml", this.getClass());
File file = new File("input/FileMessageHistoryTest.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("hello");
out.close();
PollableChannel outChannel = context.getBean("outChannel", PollableChannel.class);
Message<?> message = outChannel.receive(1000);
MessageHistory history = MessageHistory.read(message);
assertNotNull(history);
Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, "fileAdapter", 0);
assertNotNull(componentHistoryRecord);
assertEquals("file:inbound-channel-adapter", componentHistoryRecord.get("type"));
}
}

View File

@@ -0,0 +1,23 @@
<?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:int-file="http://www.springframework.org/schema/integration/file"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-2.0.xsd">
<int:message-history/>
<int-file:inbound-channel-adapter id="fileAdapter" directory="input"
auto-startup="true"
channel="outChannel"
auto-create-directory="true">
<int:poller fixed-rate="100"/>
</int-file:inbound-channel-adapter>
<int:channel id="outChannel">
<int:queue/>
</int:channel>
</beans>