首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >中的JSON变换及JSON标签"filePattern“中的文件模式动态设置

中的JSON变换及JSON标签"filePattern“中的文件模式动态设置
EN

Stack Overflow用户
提问于 2016-11-30 02:21:55
回答 2查看 817关注 0票数 0

我引用了这个答案,并试图传递动态文件模式。

applicationContext设置环境变量和调用SI网关

代码语言:javascript
复制
public class TestSpringIntegration {
public TestSpringIntegration() {
    super();
}

public static void main(String[] args) {

    TestSpringIntegration testSpringIntegration = new TestSpringIntegration();
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("SI-filters_files.xml");

    setEnvironment(context);
//  context.refresh();


    ServiceGateway gateway =
     context.getBean("inboundGateway",ServiceGateway.class);


}

private static void setEnvironment(final ConfigurableApplicationContext ctx) {
    String name = "server_text1";
    final StandardEnvironment env = new StandardEnvironment();
    final Properties props = new Properties();
    props.setProperty("filename.pattern", "'" + name + "'");
    // props.setProperty("ftp.local.dir", ftpMetaData.getLocalDirectory());

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

这是我的SI-filters_files.xml,我在上面设置了环境属性以获得文件适配器中的动态filePattern。

代码语言:javascript
复制
<int:channel id="requestChannel" />
<int:channel id="deadLetterChannel" />
<int:channel id="outboundChannel" />

<int:gateway id="inboundGateway" service-interface="test.ServiceGateway"
    default-request-channel="requestChannel" default-reply-timeout="5000" />



<file:inbound-channel-adapter id="filesIn"
    directory="file://<IP_ADDRESS>/sharedLocation/" filename-pattern="${filename.pattern}"
    channel="requestChannel" auto-startup="true">
    <int:poller id="poller" fixed-delay="5000" />
</file:inbound-channel-adapter>

<int:service-activator input-channel="requestChannel"
    output-channel="outboundChannel" ref="handler" />

<bean id="handler" class="test.FileHandler" />

<file:outbound-channel-adapter id="outboundChannel"
    directory="file://E:/" />

以下是我得到的例外:

2016-11-30 10:17:59 INFO ClassPathXmlApplicationContext:581 -刷新org.springframework.context.support.ClassPathXmlApplicationContext@dcf3e99:启动日期Wed 11月30日10:17:59 CST 2016;上下文层次的根2016-11-30 10:17:59 INFO XmlBeanDefinitionReader:317 -从类路径资源SI-filters_files.xml加载XML定义属性2016-11-30 10:17:59信息集成注册官:330-没有明确定义名为'integrationHeaderChannelRegistry‘的bean。因此,将创建一个默认的DefaultHeaderChannelRegistry。2016-11-30 10:17:59 INFO DefaultListableBeanFactory:818 -用不同定义取代[泛型bean:类org.springframework.integration.channel.DirectChannel;scope=;abstract=false;lazyInit=false;autowireMode=0;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=null;factoryMethodName=null;initMethodName=null;destroyMethodName=null];泛型bean: org.springframework.integration.channel.DirectChannel;scope=类;abstract=false;lazyInit=false;lazyInit=false;scope=;scope=;;#en21 20#;;org.springframework.beans.factory.BeanDefinitionStoreException:[2016-30 10:17:59警告ClassPathXmlApplicationContext:549 -上下文初始化过程中遇到的异常-取消刷新尝试:org.springframework.beans.factory.BeanDefinitionStoreException:无效'org.springframework.integration.file.config.FileListFilterFactoryBean#0‘定义的名称为null:无法解析字符串值“${filename.pattern}”中的占位符'filename.pattern’;嵌套异常为java.lang.IllegalArgumentException:无法解析字符串值"${filename.pattern}“异常中的占位符”java.lang.IllegalArgumentException“中的”java.lang.IllegalArgumentException无效bean定义“,其名称为java.lang.IllegalArgumentException:无法解析字符串值”${filename.pattern}中的占位符'filename.pattern‘“;嵌套异常是java.lang.IllegalArgumentException:无法解析字符串值"${filename.pattern}“中的占位符”${filename.pattern}“在org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:180) at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:152) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)在org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at test.TestSpringIntegration.main(未知来源)导致: org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174):无法解析字符串值"${filename.pattern}“中的占位符'filename.pattern‘在org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:219) at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:193) at org。springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:209) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82) at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:220)

EN

回答 2

Stack Overflow用户

发布于 2016-11-30 13:52:30

使用该构造函数,在添加属性源之前,将自动刷新上下文。

使用no-arg构造函数,将XML传递到setConfigLocations,设置属性源;调用context.refresh()

或者,使用此构造函数:

代码语言:javascript
复制
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh)

有关示例,请参见动态ftp样本

票数 0
EN

Stack Overflow用户

发布于 2016-12-01 03:04:37

applicationContext.xml

代码语言:javascript
复制
    ServiceGateway gateway = context.getBean("inboundGateway", ServiceGateway.class);
    String json = " {\"vesselVisitCode\": \"XV213443\", \"fileName\": \"server_text1.txt\",\"originalFilename\": \"server.txt\" }";
    String json1 = "{\"fileName\": \"server_text1.txt\"}";
    gateway.post(json1);

早些时候我做过这样的事(我想是个工作).

代码语言:javascript
复制
<context:property-placeholder />

<context:component-scan base-package="test"></context:component-scan>

<int:channel id="requestChannel" />
<int:channel id="deadLetterChannel" />
<int:channel id="outboundChannel" />

<int:gateway id="inboundGateway" service-interface="test.ServiceGateway"
    default-request-channel="requestChannel" default-reply-timeout="5000" />

<int:splitter input-channel="requestChannel"
    output-channel="processingChannel" />


<int:transformer id="tx" input-channel="processingChannel" 
    ref="jsonHandler" method="jsonBreak" output-channel="loggingChannel" />

<int:channel id="loggingChannel" />

<int:channel id="processingChannel">
    <int:dispatcher task-executor="executor" />
</int:channel>

<task:executor id="executor" pool-size="5" />

<file:inbound-channel-adapter id="filesIn" 
    directory="file://170.197.229.119/ftp/" channel="loggingChannel" >
    <int:poller id="poller" fixed-delay="100"/>

</file:inbound-channel-adapter>

代码语言:javascript
复制
<bean id="jsonHandler" class="test.JSONHandler" />
<bean id="handler" class="test.SelectMessage" />

<file:outbound-channel-adapter id="outboundChannel"
    directory="file://E:/" />

JSONHandler.java

代码语言:javascript
复制
public class JSONHandler {
@Autowired
SelectMessage message;

public String jsonBreak(String json) {

    Gson gson = new Gson();
    JsonElement element = gson.fromJson(json, JsonElement.class);
    JsonObject jsonObj = element.getAsJsonObject();
    JsonElement jsonElement = jsonObj.get("fileName");
    String asString = jsonElement.getAsString();
    message.setFileName(asString);

    System.out.println("in Handler----------------------------------" + json);

    return asString;
}

}

SelectMessage.java

代码语言:javascript
复制
public class SelectMessage implements MessageSelector {
@Autowired
private ApplicationContext appContext;

private String fileName;

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public boolean accept(Message<?> message) {
    try {
        if (message.getPayload() instanceof File && ((File) message.getPayload()).getName().startsWith((getFileName()))) {
            System.out.println(" --------------------return file on true match --------------------");
            return true;
        }
        return false;
    } catch (Exception e) {
        return false;
    }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40879219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档