Apache camel ApacheCamel:带有sftp组件的自定义sftp配置

Apache camel ApacheCamel:带有sftp组件的自定义sftp配置,apache-camel,Apache Camel,我正在尝试在ApacheCamel中添加一个自定义sftp组件,以将用户名、主机、端口和密码包装到要传递给sftpcomponent的配置对象中 下面是我尝试过的代码: @Configuration class SftpConfig { @Bean("sourceSftp") public SftpComponent getSourceSftpComponent( @Qualifier("sftpConfig") SftpConfi

我正在尝试在ApacheCamel中添加一个自定义sftp组件,以将用户名、主机、端口和密码包装到要传递给sftpcomponent的配置对象中

下面是我尝试过的代码:

@Configuration
class SftpConfig {
    @Bean("sourceSftp")
    public SftpComponent getSourceSftpComponent(
            @Qualifier("sftpConfig")
            SftpConfiguration sftpConfig) throws Exception{
        SftpComponent sftpComponent = new SftpComponent();
        // not getting way to set the configuration
        return sftpComponent;
    }


    @Bean("sftpConfig")
    public SftpConfiguration getSftpConfig(
            @Value("${host}") String host,
            @Value("${port}") int port,
            @Value("${applicationUserName}") String applicationUserName,
            @Value("${password}") String password) {
        SftpConfiguration sftpConfiguration =  new SftpConfiguration();
        sftpConfiguration.setHost(host);
        sftpConfiguration.setPort(port);
        sftpConfiguration.setUsername(applicationUserName);
        sftpConfiguration.setPassword(password);
        return sftpConfiguration;
    }

}
//在其他班级

from("sourceSftp:<path of directory>") ---custom component
from(“sourceSftp:”)---自定义组件

JMSComponent中的类似方法在我为sourcejms创建了一个bean的情况下运行良好,但我无法为sftp执行此操作,因为SftpComponent没有设置对sftpconfiguration的调用。

Camel维护人员似乎不再为单个组件提供“setXXXConfiguration”方法来配置其属性。提供属性的“批准”方法(与SFTP一起使用)是在连接URL上指定它们:

from ("sftp://host:port/foo?username=foo&password=bar")
.to (....)
另一种方法是实例化端点并设置其属性,然后在
from()
调用中使用对端点的引用。有无数种配置Camel的方法——这对我来说适用于基于XML的配置:

<endpoint id="fred" uri="sftp://acme.net/test/">
  <property key="username" value="xxxxxxx"/>
  <property key="password" value="yyyyyyy"/>
</endpoint>
<route>
  <from uri="fred"/>
  <to uri="log:foo"/>
</route>

Camel维护人员似乎不再为单个组件提供“setXXXConfiguration”方法来配置其属性。提供属性的“批准”方法(与SFTP一起使用)是在连接URL上指定它们:

from ("sftp://host:port/foo?username=foo&password=bar")
.to (....)
另一种方法是实例化端点并设置其属性,然后在
from()
调用中使用对端点的引用。有无数种配置Camel的方法——这对我来说适用于基于XML的配置:

<endpoint id="fred" uri="sftp://acme.net/test/">
  <property key="username" value="xxxxxxx"/>
  <property key="password" value="yyyyyyy"/>
</endpoint>
<route>
  <from uri="fred"/>
  <to uri="log:foo"/>
</route>

您可以通过扩展SftpComponent对其进行自定义。这允许您定义多个端点,而无需为每个端点定义提供用户名/密码

步骤1:扩展SftpComponent并为组件提供自定义名称,即customSftp

@Component("customSftp")
public class CustomSftpComponent extends SftpComponent {

    private static final Logger LOG = LoggerFactory.getLogger(CustomSftpComponent.class);

    @Value("${sftp.username}")
    private String username;

    @Value("${sftp.password}")
    private String password;

    @SuppressWarnings("rawtypes")
    protected void afterPropertiesSet(GenericFileEndpoint<SftpRemoteFile> endpoint) throws Exception {
        SftpConfiguration config = (SftpConfiguration) endpoint.getConfiguration();
        config.setUsername(username);
        config.setPassword(password);
    }

}
步骤3:将其放置在application.properties中

camel.springboot.main-run-controller=true

sftp.endpoint1=customSftp://localhost.net/input/1?delay=30s
sftp.endpoint2=customSftp://localhost.net/input/2?delay=30s

sftp.username=sftp_user1_l
sftp.password=xxxxxxxxxxxx
这样,您就不必为每个端点重复用户名/密码


注意:使用这种方法,您将无法在URI端点配置中设置用户名/密码。在URI中设置的任何内容都将在AfterPropertieSet中替换

您可以通过扩展SftpComponent来定制它。这允许您定义多个端点,而无需为每个端点定义提供用户名/密码

步骤1:扩展SftpComponent并为组件提供自定义名称,即customSftp

@Component("customSftp")
public class CustomSftpComponent extends SftpComponent {

    private static final Logger LOG = LoggerFactory.getLogger(CustomSftpComponent.class);

    @Value("${sftp.username}")
    private String username;

    @Value("${sftp.password}")
    private String password;

    @SuppressWarnings("rawtypes")
    protected void afterPropertiesSet(GenericFileEndpoint<SftpRemoteFile> endpoint) throws Exception {
        SftpConfiguration config = (SftpConfiguration) endpoint.getConfiguration();
        config.setUsername(username);
        config.setPassword(password);
    }

}
步骤3:将其放置在application.properties中

camel.springboot.main-run-controller=true

sftp.endpoint1=customSftp://localhost.net/input/1?delay=30s
sftp.endpoint2=customSftp://localhost.net/input/2?delay=30s

sftp.username=sftp_user1_l
sftp.password=xxxxxxxxxxxx
这样,您就不必为每个端点重复用户名/密码


注意:使用这种方法,您将无法在URI端点配置中设置用户名/密码。在URI中设置的任何内容都将在AfterPropertieSet中替换

谢谢。我试试看。谢谢。我试试看。