如何使用ExpressionEvaluatingRequestHandlerAdvice移动文件 [英] How to move files using ExpressionEvaluatingRequestHandlerAdvice
问题描述
在ExpressionEvaluatingRequestHandlerAdvice手册中,它清楚地写为A typical use case for this advice might be with an <ftp:outbound-channel-adapter/>, perhaps to move the file to one directory if the transfer was successful, or to another directory if it fails
.
In the manual for ExpressionEvaluatingRequestHandlerAdvice, it clearly says, A typical use case for this advice might be with an <ftp:outbound-channel-adapter/>, perhaps to move the file to one directory if the transfer was successful, or to another directory if it fails
.
但是我无法弄清楚将有效载荷从当前目录移动到另一个目录的表达式.
But I cannot figure out expression to move payload from current directory to another one.
此示例仅删除或重命名文件:
This example just deletes or renames the file:
<bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
<property name="onSuccessExpression" value="payload.delete()" />
<property name="successChannel" ref="afterSuccessDeleteChannel" />
<property name="onFailureExpression" value="payload.renameTo(new java.io.File(payload.absolutePath + '.failed.to.send'))" />
<property name="failureChannel" ref="afterFailRenameChannel" />
</bean>
如何实现?
修改
根据加里(Gary)的建议,这是新尝试:
As per Gary's suggestion, this is the new try:
设法将表达式更改为"T(java.nio.file.Files).move(payload.path, new java.io.File(new java.io.File('sent'), payload.name).path, T(java.nio.file.StandardCopyOption).REPLACE_EXISTING)"
,
但仍然出现错误Method move(java.lang.String,java.lang.String,java.nio.file.StandardCopyOption) cannot be found on java.nio.file.Files type
Managed to change the expression to "T(java.nio.file.Files).move(payload.path, new java.io.File(new java.io.File('sent'), payload.name).path, T(java.nio.file.StandardCopyOption).REPLACE_EXISTING)"
,
but still get the error Method move(java.lang.String,java.lang.String,java.nio.file.StandardCopyOption) cannot be found on java.nio.file.Files type
代码是
@Bean
@ServiceActivator(inputChannel = "toSftpChannel", adviceChain = "expressionAdvice")
public MessageHandler uploadHandler() {
SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
handler.setRemoteDirectoryExpression(new LiteralExpression(outRemoteDirectory));
handler.setFileNameGenerator(new FileNameGenerator() {
@Override
public String generateFileName(Message<?> message) {
if (message.getPayload() instanceof File) {
return ((File) message.getPayload()).getName();
} else {
throw new IllegalArgumentException("File expected as payload.");
}
}
});
return handler;
}
@MessagingGateway()
public interface UploadGateway {
@Gateway(requestChannel = "toSftpChannel")
void upload(File file);
}
@Bean
public String onUploadSuccessExpression() {
return "T(java.nio.file.Files).move(payload.path, new java.io.File(new java.io.File('sent'), payload.name).path, T(java.nio.file.StandardCopyOption).REPLACE_EXISTING)";
}
@Bean
public String onUploadFailedExpression() {
return "payload";
}
@Bean
public Advice expressionAdvice() {
ExpressionEvaluatingRequestHandlerAdvice expressionEvaluatingRequestHandlerAdvice = new ExpressionEvaluatingRequestHandlerAdvice();
expressionEvaluatingRequestHandlerAdvice.setOnSuccessExpressionString(onUploadSuccessExpression());
expressionEvaluatingRequestHandlerAdvice.setSuccessChannelName("uploadSuccessChannel");
expressionEvaluatingRequestHandlerAdvice.setOnFailureExpressionString(onUploadFailedExpression());
expressionEvaluatingRequestHandlerAdvice.setFailureChannelName("uploadFailedChannel");
expressionEvaluatingRequestHandlerAdvice.setTrapException(true);
expressionEvaluatingRequestHandlerAdvice.setPropagateEvaluationFailures(true);
return expressionEvaluatingRequestHandlerAdvice;
}
调用UploadGateway
中的upload
方法.
堆栈跟踪为
"main@1" prio=5 tid=0x1 nid=NA runnable
java.lang.Thread.State: RUNNABLE
at org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.evaluateSuccessExpression(ExpressionEvaluatingRequestHandlerAdvice.java:241)
at org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.doInvoke(ExpressionEvaluatingRequestHandlerAdvice.java:214)
at org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice.invoke(AbstractRequestHandlerAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy81.handleRequestMessage(Unknown Source:-1)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.doInvokeAdvisedRequestHandler(AbstractReplyProducingMessageHandler.java:127)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:112)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:423)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:373)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:143)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:135)
at org.springframework.integration.gateway.MessagingGatewaySupport.send(MessagingGatewaySupport.java:392)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:481)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:433)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:424)
at org.springframework.integration.gateway.GatewayCompletableFutureProxyFactoryBean.invoke(GatewayCompletableFutureProxyFactoryBean.java:65)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy87.upload(Unknown Source:-1)
推荐答案
如果新目录与旧目录位于同一磁盘上,则在"onSuccessExpression"中,简单地使用payload.renameTo(...)
,类似于示例中的操作. onFailureExpression
.
If the new directory is on the same disk as the old one, in the 'onSuccessExpression', simply use payload.renameTo(...)
similar to the way the sample does in the onFailureExpression
.
`payload.renameTo(new java.io.File(new File('newDir'), payload.name))`
使用有效负载名称创建文件到目录newDir
(必须存在).
Creates a file with the payload's name to a directory newDir
(which must exist).
如果您是JDK 7或更高版本,请使用...
If you are JDK 7 or above use...
T(java.nio.file.Files).move(payload.path, new java.io.File(new File('newDir'), payload.name).path)
...相反.
这将处理新目录位于不同磁盘上的情况(简单的File.renameTo()
不会).
This will handle the situation of the new directory being on a different disk (which a simple File.renameTo()
will not).
如果您仍在JDK 6上,并且新目录可能位于其他磁盘上,则需要使用onSuccessExpression=payload
并将服务激活器订阅到successChannel
来操纵文件本身,也许使用Spring的FileCopyUtils
If you are still on JDK 6 and the new directory might be on a different disk you will need to use onSuccessExpression=payload
and subscribe a service activator to the successChannel
to manipulate the file itself, perhaps using Spring's FileCopyUtils
.
这篇关于如何使用ExpressionEvaluatingRequestHandlerAdvice移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!