Spring 集成 - 如何将参数传递到服务激活器
Spring Integration - how to pass parameters into service activator
我想在 SFTP 上传完成后发送电子邮件...
我有一个 SFTP 上传器:
<sftp:outbound-channel-adapter
id="sftpOutboundAdapter"
channel="inputFiles"
charset="UTF-8"
remote-directory="${directory.remote}"
session-factory="sftpSessionFactory">
<sftp:request-handler-advice-chain>
<bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
<property name="onSuccessExpression" value="payload.delete()"/>
<property name="successChannel" ref="successChannel"/>
<property name="onFailureExpression" value="payload.renameTo(payload.absolutePath + '.error')"/>
<property name="failureChannel" ref="failChannel"/>
</bean>
</sftp:request-handler-advice-chain>
</sftp:outbound-channel-adapter>
完成后将有效负载发送到 successChannel:
<int:channel id="successChannel">
<int:interceptors>
<int:wire-tap channel="successLogChannel"/>
</int:interceptors>
</int:channel>
...然后通过窃听将其记录下来。
<int:channel id="successLogChannel"/>
<int:transformer input-channel="successLogChannel" output-channel="logChannel"
expression="'Successfully transferred ' + inputMessage.payload.absolutePath + ' [result=' + payload + ']'"/>
<int:logging-channel-adapter id="logChannel" level="INFO"/>
这就是它的突破点,我认为属性不是使用的正确元素,我基本上需要突破成 java class 并能够传入参数包括我刚刚上传的文件的名称。
<int:service-activator input-channel="successChannel" ref="Email" method="sendSuccessMail">
<property name="to" value="test@gmail.com" />
<property name="from" value="me@me.com" />
<property name="filename" ref="'inputMessage.payload.absolutePath'" />
</int:service-activator>
Java Class:
@Component(value = "Email")
public class Email extends AbstractMessaging<EmailRequest, EmailResponse> {
....
public void sendSuccessMail(String to, String from, String filename){
log.info("--------------------------------------------");
log.info("Success Mail will be sent here to" + to + ", from " + from + " for " + filename);
log.info("--------------------------------------------");
}
}
FactoryBean threw exception on object creation; nested exception is
java.lang.IllegalArgumentException: Target object of type [class ...
.email.Email] has no eligible methods for handling Messages.
如果我不能向我的方法传递参数,是否可以传递参数或只传递有效载荷 sendSuccessMail
?
使用<header-enricher/>
在headers中创建您需要的值;那么...
public void sendSuccessMail(@Header("to") String to, @Header("from") String from, @Payload String filename){
...
}
<int:chain ...>
<int:header-enricher>
<int:header name="to" value="test@gmail.com">
...
<int:header name="inputMessage" expression="inputMessage">
</int:header-enricher>
<int:transformer expression="headers['inputMessage'].payload.absolutePath">
<int:service-activator ... />
</int:chain>
我想在 SFTP 上传完成后发送电子邮件...
我有一个 SFTP 上传器:
<sftp:outbound-channel-adapter
id="sftpOutboundAdapter"
channel="inputFiles"
charset="UTF-8"
remote-directory="${directory.remote}"
session-factory="sftpSessionFactory">
<sftp:request-handler-advice-chain>
<bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
<property name="onSuccessExpression" value="payload.delete()"/>
<property name="successChannel" ref="successChannel"/>
<property name="onFailureExpression" value="payload.renameTo(payload.absolutePath + '.error')"/>
<property name="failureChannel" ref="failChannel"/>
</bean>
</sftp:request-handler-advice-chain>
</sftp:outbound-channel-adapter>
完成后将有效负载发送到 successChannel:
<int:channel id="successChannel">
<int:interceptors>
<int:wire-tap channel="successLogChannel"/>
</int:interceptors>
</int:channel>
...然后通过窃听将其记录下来。
<int:channel id="successLogChannel"/>
<int:transformer input-channel="successLogChannel" output-channel="logChannel"
expression="'Successfully transferred ' + inputMessage.payload.absolutePath + ' [result=' + payload + ']'"/>
<int:logging-channel-adapter id="logChannel" level="INFO"/>
这就是它的突破点,我认为属性不是使用的正确元素,我基本上需要突破成 java class 并能够传入参数包括我刚刚上传的文件的名称。
<int:service-activator input-channel="successChannel" ref="Email" method="sendSuccessMail">
<property name="to" value="test@gmail.com" />
<property name="from" value="me@me.com" />
<property name="filename" ref="'inputMessage.payload.absolutePath'" />
</int:service-activator>
Java Class:
@Component(value = "Email")
public class Email extends AbstractMessaging<EmailRequest, EmailResponse> {
....
public void sendSuccessMail(String to, String from, String filename){
log.info("--------------------------------------------");
log.info("Success Mail will be sent here to" + to + ", from " + from + " for " + filename);
log.info("--------------------------------------------");
}
}
FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Target object of type [class ... .email.Email] has no eligible methods for handling Messages.
如果我不能向我的方法传递参数,是否可以传递参数或只传递有效载荷 sendSuccessMail
?
使用<header-enricher/>
在headers中创建您需要的值;那么...
public void sendSuccessMail(@Header("to") String to, @Header("from") String from, @Payload String filename){
...
}
<int:chain ...>
<int:header-enricher>
<int:header name="to" value="test@gmail.com">
...
<int:header name="inputMessage" expression="inputMessage">
</int:header-enricher>
<int:transformer expression="headers['inputMessage'].payload.absolutePath">
<int:service-activator ... />
</int:chain>