Kurukshetran
5/16/2017 - 7:04 PM

Spring Integration SftpPersistenFileListFilter Example

Spring Integration SftpPersistenFileListFilter Example

/*
 * Copyright 2002-2008 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.samples.sftp;

import java.io.IOException;

import org.junit.Test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Oleg Zhurakousky
 *
 */
public class SftpInboundReceiveSample {

	@Test
	public void runDemo() throws IOException{
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
				"/META-INF/spring/integration/SftpInboundReceiveSample-context.xml", this.getClass());
		System.in.read();
		context.close();
	}

}

<?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-sftp="http://www.springframework.org/schema/integration/sftp"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
	xsi:schemaLocation="http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-4.0.xsd
		http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">

	<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
		<property name="host" value="10.0.0.3" />
		<property name="port" value="22" />
		<property name="user" value="ftptest" />
		<property name="password" value="ftptest" />
	</bean>

	<int-sftp:inbound-channel-adapter id="sftpInbondAdapter"
			channel="receiveChannel"
			session-factory="sftpSessionFactory"
			local-directory="/tmp/bar"
			remote-directory="foo/bar"
			auto-create-local-directory="true"
			delete-remote-files="false"
			preserve-timestamp="true"
			filter="compositeFilter"
			local-filter="acceptAll">
		<int:poller fixed-rate="5000" max-messages-per-poll="-1" />
	</int-sftp:inbound-channel-adapter>

	<int:channel id="receiveChannel" />

	<int:service-activator input-channel="receiveChannel" expression="T(java.lang.System).out.println(payload.toString())">
		<int:request-handler-advice-chain>
			<bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
				<property name="onSuccessExpression" value="payload.delete()" />
				<property name="successChannel" ref="afterSuccessDeleteChannel" />
				<property name="onFailureExpression" value="payload.rename('/tmp/bad/' + payload.name)" />
			</bean>
		</int:request-handler-advice-chain>
	</int:service-activator>

	<int:transformer input-channel="afterSuccessDeleteChannel" output-channel="stdout"
		expression="'Removal of ' + inputMessage.payload.absolutePath + ' after transfer ' + (payload ? 'succeeded' : 'failed')" />

	<int-stream:stdout-channel-adapter id="stdout" append-newline="true"/>

 	<bean id="acceptAll" class="org.springframework.integration.file.filters.AcceptAllFileListFilter" />
 
	<bean id="compositeFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
		<constructor-arg>
			<list>
				<bean class="org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter">
					<constructor-arg value="*.txt" />
				</bean>
				<bean class="org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter">
					<constructor-arg name="store" ref="metadataStore"/>
					<constructor-arg value="foo/bar/"/>
				</bean>
			</list>
		</constructor-arg>
	</bean>

	<bean name="metadataStore" class="org.springframework.integration.metadata.PropertiesPersistingMetadataStore">
		<property name="baseDirectory" value="/tmp/"/>
	</bean>

</beans>