Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically change the input directory for inbound channel adapter [INT-4025] #7971

Open
spring-operator opened this issue May 10, 2016 · 8 comments

Comments

@spring-operator
Copy link
Contributor

spring-operator commented May 10, 2016

Varun Maurya opened INT-4025 and commented

Today, when we use a "file:inbound-channel-adapter" the directory it looks into is fixed. Can we add a feature handle of adapter so that we could inject the input directory dynamically.


Reference URL: http://stackoverflow.com/questions/37142937/how-to-assign-dynamic-values-to-directory-in-inbound-file-adapter-spring-integr

Issue Links:

@spring-operator
Copy link
Contributor Author

Artem Bilan commented

I would to know more background.
Let me quote you from StackOverflow:

Now my polling directory have multiple sub-folders and in different conditions i have to poll different subfolder.
...
I want directory value to be provided dynamically

The <int-file: inbound-channel-adapter> is based on the FileReadingMessageSource with a setDirectory(File directory) option. And yes, indeed, you can change that option at runtime and the next poll round will pick up a fresh directory to scan for new files.

The FileReadingMessageSource can be accessed as a bean via adapter id with the .source suffix.

With that you can inject it now to any service which may change the directory according any conditions.

On the other hand you can inject the FileReadingMessageSource into some Advice for the <poller> on the <int-file: inbound-channel-adapter> and yes, change the directory there after each processed message during poll operation on MessageSource.

So, having all that in hands, I'm not sure what you would like to introduce here.
Thanks

@spring-operator
Copy link
Contributor Author

spring-operator commented May 10, 2016

Gary Russell commented

This is similar to #7713; yes, it can be done in a smart poller, but adding expression support would be more convenient.

Obviously, it would not be compatible with the WatchServiceDirectoryScanner however.

@spring-operator
Copy link
Contributor Author

Varun Maurya commented

inbound channel adapter is starting point of my application. The way you suggest to have access of FileReadingMessageSource and setting the directory would change after first run.i.e. after adapter is created with some static directory location with some file. How can i change it even before that so that i could directly get the access to required folder.

@spring-operator
Copy link
Contributor Author

Artem Bilan commented

For the FileReadingMessageSource.setDirectory(File directory) it doesn't matter if your adapter has been started before or not.
You can just inject it as a bean anywhere and change that property on your purpose.

But yes, we definitely may implement it like Gary suggest, via directory-expression, which will be evaluated on each scanInputDirectory().
But yeah... We might won't be able to reach that with the WatchServiceDirectoryScanner. Although its start()/stop() if the directory-expression has changed its result during scanInputDirectory() may be a hook...

@spring-operator
Copy link
Contributor Author

Varun Maurya commented

I am sorry if i am not able to understand you correctly or i am not able to explain the problem - Let me try one more time : the inbound channel adapter with poller is entry-point for my application. when i load my config(through war) the poller starts polling the directory mentioned in "inbound channel adapter" but i want to change that directory everytime poller start to poll directory, based on my criteiria.

@spring-operator
Copy link
Contributor Author

Gary Russell commented

You can update the source on each poll using a smart poller

This method is called before the MessageSource.receive() method. It enables you to examine and or reconfigure the source at this time.

but I think it's a reasonable enhancement to align it with the remote file (S)FTP adapters and add directory-expression that will be evaluated for each poll.

@spring-operator
Copy link
Contributor Author

Artem Bilan commented

You can configure your adapter with some fake dir from the beginning and

but i want to change that directory everytime poller start to poll directory, based on my criteiria.

you can achieve with the Advice like:

public class ChangeDirAdvice implements MethodInterceptor {

		@Autowired
		@Qualifier("myFileReadingAdapter.source")
		private FileReadingMessageSource fileReadingMessageSource;

		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			File newDirectory = ...;
			this.fileReadingMessageSource.setDirectory(newDirectory);
			return invocation.proceed();
		}
}

for the <advice-chain> on the <poller> of <int-file: inbound-channel-adapter>.
But, yes, it is done before each MessageSource.receive() invocation.
If you would like to do that really on each poller's interval, you should take a look to the custom Trigger implementation.

@spring-operator
Copy link
Contributor Author

Gary Russell commented

Also see "Smart" Polling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants