首页 文章

我如何通过Java 1.7中的Spring DSL在Sftp Inbound适配器中动态传输消息

提问于
浏览
1

我有一个Sftp入站流,我从DefaultSftpSessionFactory获取了会话信息 . 但我需要动态实现多个会话信息,我将从数据库表中获取 . 这意味着我需要在集成流程中实现多个Sftp服务器详细信息 . 现在我已经完成了从单一来源到单个目的地的文件传输,但我需要将多个源实现到多个目标 . 所以任何人都可以提供一些指针 .

这是我的会话工厂...这里我有一个Sftp服务器信息,但如何配置多个服务器详细信息 .

@Autowired
    private DefaultSftpSessionFactory sftpSessionFactory;

    @Bean
    public DefaultSftpSessionFactory sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(
                true);
        factory.setHost("111.11.12.143");
        factory.setPort(22);
        factory.setUser("sftp");
        factory.setPassword("*******");         
        return factory;
    }

这是我的Sftp Inbound流程..

@Bean
    public IntegrationFlow sftpInboundFlow() {
    System.out.println("enter sftpInboundFlow....."
            + sftpSessionFactory.getSession());     
    return IntegrationFlows
            .from(Sftp.inboundAdapter(this.sftpSessionFactory)
                    .preserveTimestamp(true).remoteDirectory(remDir)
                    .regexFilter(".*\\.txt$")
                    .localFilenameExpression("#this.toUpperCase()")
                    .localDirectory(new File(localDir))
                    .remoteFileSeparator("/"),
                    new Consumer<SourcePollingChannelAdapterSpec>() {
                        @Override
                        public void accept(SourcePollingChannelAdapterSpec e) {
                            e.id("sftpInboundAdapter")
                                    .autoStartup(true)
                                    .poller(Pollers.fixedRate(1000)
                                            .maxMessagesPerPoll(1));
                        }
                    })
            //.channel(MessageChannels.queue("sftpInboundResultChannel"))
                    .channel(sftpInboundResultChannel())
            .get();
}

正如加里所建议我正在编辑我的帖子....

嗨加里,我正在参考Github dynamic FTP example .

通过ChannelResolver类,我需要调用上面的DSL类 . 并在不使用XML的情况下在context属性中设置动态值 .

在我的ChannelResolver类中,我想要这样的东西

StandardEnvironment env = new StandardEnvironment();
Properties props = new Properties();
props.setProperty("inbound.host", host);    //I am getting the value of 'host' from a DB table.
PropertiesPropertySource pps = new PropertiesPropertySource("sftpprop", props);
env.getPropertySources().addLast(pps);
context.setEnvironment(env); 

And my DSL class I need to use like this.
@Value("${inbound.host}")
private String host;

So in this way can I set dynamic value for String 'host' ?

我正在编辑原帖...........

In my Outbound dynamic resolver class I am doing like this


    StandardEnvironment env = new StandardEnvironment();
    Properties props = new Properties();        
    props.setProperty("outbound.host", host);
    props.setProperty("outbound.port", String.valueOf(port));
    props.setProperty("outbound.user", user);
    props.setProperty("outbound.password", password);
    props.setProperty("outbound.remote.directory", remoteDir);
    props.setProperty("outbound.local.directory", localDir);        

    PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
    env.getPropertySources().addLast(pps);
    ctx.setEnvironment(env);


And this is my dsl class....

@Autowired
private DefaultSftpSessionFactory sftpSessionFactory;

@Bean
public DefaultSftpSessionFactory sftpSessionFactory(@Value("${outbound.host}") String host, @Value("${outbound.port}") int port,
        @Value("${outbound.user}") String user, @Value("${outbound.password}") String password
        ) {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(host);
    factory.setPort(port);
    factory.setUser(user);
    factory.setPassword(password);      
    return factory;
}


@Bean
public IntegrationFlow fileInboundFlow(@Value("${outbound.local.directory}") String localDir)
{
    return IntegrationFlows
            .from(Files.inboundAdapter(new File(localDir)),
                    new Consumer<SourcePollingChannelAdapterSpec>() {

                        @Override
                        public void accept(SourcePollingChannelAdapterSpec e) {
                            e.autoStartup(true).poller(
                                    Pollers.fixedDelay(5000)
                                            .maxMessagesPerPoll(1));
                        }
                    })
                    .channel(sftpSendChannel())
                    .get();
}

@Bean
public IntegrationFlow sftpOutboundFlow(@Value("${outbound.remote.directory}") String remDir) {    
    return IntegrationFlows
            .from(sftpSendChannel())
            .handle(Sftp.outboundAdapter(this.sftpSessionFactory)                       
                    .useTemporaryFileName(false)
                    .remoteDirectory(remDir))
                    .get();
}

@Bean
public MessageChannel sftpSendChannel() {
    return new DirectChannel();
}

@Bean
public static PropertySourcesPlaceholderConfigurer configurer1() {
    return new PropertySourcesPlaceholderConfigurer();      
}


And this the error log from console...

org.apache.catalina.core.StandardContext listenerStart SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException的侦听器实例的异常:创建名为'sftpOutBoundDsl'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.springframework.integration.sftp.session.DefaultSftpSessionFactory com.tcs.iux.ieg.sftp.dynamic.SftpOutBoundDsl.sftpSessionFactory;嵌套异常是java.lang.IllegalArgumentException:无法解析org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)中字符串值“$ ”中的占位符'outbound.host'在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)在org.springframework.beans.factory .support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton( DefaultSingletonBeanRegistry.java:229)org.springframework.beans.factory.support.AbstractBeanFa位于org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons的Org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)中的ctory.doGetBean(AbstractBeanFactory.java:298)(DefaultListableBeanFactory.java: 725)org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)atg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)at org.springframework.web.context.ContextLoader .configureAndRefreshWebApplicationContext(ContextLoader.java:403)org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)at org . apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)at org.apache.catalina.core.StandardContext.startInternal(Standar) dContext.java:5467)org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)org.apache .catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)at java.util.concurrent.FutureTask.run(FutureTask.java:262)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145 )java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)at java.lang.Thread.run(Thread.java:744)引起:org.springframework.beans.factory.BeanCreationException:无法autowire字段:private org.springframework.integration.sftp.session.DefaultSftpSessionFactorycom.tcs.iux.ieg.sftp.dynamic.SftpOutBoundDsl.sftpSessionFactory;嵌套异常是java.lang.IllegalArgumentException:无法在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java)中解析字符串值“$ ”中的占位符'outbound.host' . 555)org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)... 22更多原因by:java.lang.IllegalArgumentException:无法在org.springframework的org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)的字符串值“$ ”中解析占位符'outbound.host' . util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(Abstract) PropertyResolver.java:204)在org.springframework的org.springframework.context.support.PropertySourcesPlaceholderConfigurer $ 2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)的org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178) . bean.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:800)位于org.springframework.beans.factory.support.DefaultListableBeanFactory的org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:917) . 在org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:743)的org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815)的resolveDependency(DefaultListableBeanFactory.java:904)在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(Constru ctorResolver.java:466)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1113)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1008)at org org.springframework.beans.factory.support上的.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) .AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry . java:298)org.springframework.beans.factory.support.AbstractBeanFactory位于org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006)的org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1088)中的.getBean(AbstractBeanFactory.java:193) )org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527)... 24更多

1 回答

  • 1

    它目前不受支持 .

    我们有一个open JIRA to add support for dynamic server selection但是不太可能及时完成即将推出的4.2版本 .

    您可以通过编写自己的自定义委派会话工厂来解决此问题,该工厂使用某些条件(例如 ThreadLocal )来确定要使用的委托工厂 .

    EDIT:

    与XML一样,您需要一个 PropertySourcesPlaceholderConfigurer bean .

    您还应该使用工厂方法注入,因为 @Configuration 类创建得太早而无法注入 @Value ...

    @Configuration
    public class FooConfig {
    
        @Bean
        public DefaultSftpSessionFactory factory(
                @Value("${inbound.host}") String host, 
                @Value("${inbound.port}") int port) {
            DefaultSftpSessionFactory sf = new DefaultSftpSessionFactory();
            sf.setHost(host);
            sf.setPort(port);
            return sf;
        }
    
        @Bean
        public PropertySourcesPlaceholderConfigurer configurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    
    }
    

    .

    public class Testing {
    
        @Test
        public void test() {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
            context.register(FooConfig.class);
            StandardEnvironment env = new StandardEnvironment();
            Properties props = new Properties();
            props.setProperty("inbound.host", "bar");
            props.setProperty("inbound.port", "23");
            PropertiesPropertySource pps = new PropertiesPropertySource("sftpprop", props);
            env.getPropertySources().addLast(pps);
            context.setEnvironment(env);
            context.refresh();
            DefaultSftpSessionFactory sessionFactory = context.getBean(DefaultSftpSessionFactory.class);
            assertEquals("bar", TestUtils.getPropertyValue(sessionFactory, "host"));
            context.close();
        }
    
    }
    

    顺便说一句,the delegating session factory will be in 4.2 after all .

    EDIT2

    你可以避免配置类的早期实例化并使用全局 @Value 注入,只要你创建PSPC bean static ...

    @Configuration
    public class FooConfig {
    
        @Value("${foo}")
        public String foo;
    
        @Bean
        public String earlyFoo() {
            return this.foo;
        }
    
        @Bean
        public String foo(@Value("${foo}") String foo) {
            return foo;
        }
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer configurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    
    }
    

    在这种情况下,将按预期填充 earlyFoo .

相关问题