简体   繁体   中英

Accessing Mule flowVars/Variable from a Java method

I have created a Mule flow which determines whether or not to download a group of files via SFTP, provided they are all present.

I determine this using a Java method which reads the contents of a folder and then returns either true or false .

The structure of the Mule flow looks like this:

<spring:beans>
    <spring:bean class="com.test.FileCheck" name="invokeFileChecks"> 
</spring:bean>
</spring:beans>


<flow name="check-files" processingStrategy="synchronous">
<set-variable variableName="allFilesPresent" value="False" doc:name="Variable - all files present boolean"/>

        <invoke object-ref="invokeFileChecks" 
                method="checkFiles" 
                doc:name="Invoke folder scan" 
                methodArguments="#[flowVars.sftpHost], #[flowVars.sftpPort], #[flowVars.sftpUserName], #[flowVars.sftpPassword], #[flowVars.sftpRequestedFiles]"/>
        <echo-component doc:name="Echo"/> 

The Java method looks like this:

public boolean checkFiles(String hostname, int port, String username, String password, String fileList){        
    boolean filesPresentTrueOrFalse;

//logic to list folder contents of SFTP:
//If all files are present, return true, otherwise false.

return filesPresentTrueOrFalse;
}

How can I assign allFilesPresent the value of filesPresentTrueOrFalse ?

Mule variables can be accessed via setProperty / getProperty methods from the MuleMessage class.

An instance of MuleMessage can be obtained from MuleEventContext via getMessage method. The event context itself gets injected by implementing method onCall of Callable interface.

So, start with implementing the Callable interface in you bean.

Wrap the invoke in an enricher. This way the result will go to the target var and not overwrite the payload. It will also keep your java decoupled from mule internals:

<enricher target="#[flowVars.allFilesPresent]">
            <invoke object-ref="invokeFileChecks" 
                    method="checkFiles" 
                    doc:name="Invoke folder scan" 
                    methodArguments="#[flowVars.sftpHost], #[flowVars.sftpPort], #[flowVars.sftpUserName], #[flowVars.sftpPassword], #[flowVars.sftpRequestedFiles]"/>
</enricher>

Enricher Can only have one processor so anytime thing else needed in there wrap in a processor-chain or a flow and use flow-ref.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM