Search code examples
javaspringapache-camelsftpcamel-ftp

Apache Camel SFTPConfiguration with Spring XML


I am working on a legacy app which is using Apache Camel with SFTP endpoints. The Camel context is defined using Spring Xml Dsl e.g.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:camel="http://camel.apache.org/schema/spring"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camel:camelContext id="myCtx" errorHandlerRef="eventDLQ"
                    xmlns="http://camel.apache.org/schema/spring"
                    autoStartup="true">

  <camel:endpoint id="sftp1" uri="sftp://ftp1@example"/>
  <camel:endpoint id="sftp2" uri="sftp://ftp2@example"/>                                            

</camel:camelContext>
</beans>

I need to configure Camel SFTP using the SFTPConfiguration object but don't know how to wire it up as I am using Spring.

Can I just create the bean in the XML file using Spring and will Camel auto-detect it?


Solution

  • Well, Yes if routes are available in same scope i.e. in same context in your case. I hope you know if you are using SFTP then you would require to import import SFTPs Certificate in java keystore as well. Remote File Configuration should be declared as options as parameter in endpoint.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
    
      <camel:camelContext id="myCtx" errorHandlerRef="eventDLQ"
                        xmlns="http://camel.apache.org/schema/spring"
                        autoStartup="true">
    
      <camel:endpoint id="sftp1" uri="sftp://ftp1@example"/>
      <camel:endpoint id="sftp2" uri="sftp://ftp2@example"/>                                            
    
        <route>
            <from ref="sftp1"/>
            <to uri="mock:result"/>
        </route>
    
         <route>
            <from ref="sftp2"/>
            <to uri="mock:result"/>
        </route>
    </camel:camelContext>
    </beans>
    

    You can create bean and pass it to camel-endpoint as well as reference.

       <bean class="org.apache.camel.component.file.remote.SftpConfiguration" id="sftpConfig">
        <property name="jschLoggingLevel" value="WARN"/>
        <property name="strictHostKeyChecking" value="no"/>
    </bean>
    

    NOTE: You could use all options available in this - Link

    <bean class="org.apache.camel.component.file.remote.SftpEndpoint" id="sftpEndpoint">
        <property name="configuration" ref="sftpConfig" />
    </bean>
    

    I am not sure which version of camel you are using you can see FTPCompnent src and accordingly pass sftpEndpoint reference to it.