首页 文章

使用DSL从Spring位置读取文件的Spring集成

提问于
浏览
1

我有一个要求,我需要在unix位置连续查找文件 . 一旦它可用,那么我需要解析它并转换为一些json格式 . 这需要使用Spring集成 - DSL完成 . 以下是我从Spring网站获得的一段代码,但它显示以下异常:

o.s.integration.handler.LoggingHandler: org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application.processFileChannel'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers

以下是代码:

@SpringBootApplication
public class FileReadingJavaApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(FileReadingJavaApplication.class)
            .web(false)
            .run(args);
    }

    @Bean
    public IntegrationFlow fileReadingFlow() {
         return IntegrationFlows
                  .from(s -> s.file(new File("Y://"))
                              .patternFilter("*.txt"),
                          e -> e.poller(Pollers.fixedDelay(1000)))
                  .transform(Transformers.fileToString())
                  .channel("processFileChannel")
                  .get();
        }

}

新代码:

@SpringBootApplication公共类SpringIntegration {

public static void main(String[] args) {
    new SpringApplicationBuilder(SpringIntegration.class)
    .web(false)
    .run(args);
}

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost("ip");
    factory.setPort(port);
    factory.setUser("username");
    factory.setPassword("pwd");
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(factory);
}

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory("remote dir");
    fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.txt"));

    return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource ftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource source =
            new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    source.setLocalDirectory(new File("Local directory"));

    return source;
}

@Bean
@ServiceActivator(inputChannel = "fileInputChannel")
public MessageHandler handler() {
    return new MessageHandler() {


        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            System.out.println("File Name : "+message.getPayload());

        }

    };
}

@Bean public static StandardIntegrationFlow processFileFlow(){return IntegrationFlows .from(“fileInputChannel”) . split() . handle(“fileProcessor”,“process”) . get();

}

@Bean
@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
public MessageSource<File> fileReadingMessageSource() {
    AcceptOnceFileListFilter<File> filters =new AcceptOnceFileListFilter<>();

    FileReadingMessageSource source = new FileReadingMessageSource();
    source.setAutoCreateDirectory(true);
    source.setDirectory(new File("Local directory"));
    source.setFilter(filters);

    return source;
}
@Bean
public FileProcessor fileProcessor() {
    return new FileProcessor();
}


 @Bean
    @ServiceActivator(inputChannel = "fileInputChannel")
    public AmqpOutboundEndpoint amqpOutbound(AmqpTemplate amqpTemplate) {
        AmqpOutboundEndpoint outbound = new AmqpOutboundEndpoint(amqpTemplate);
        outbound.setExpectReply(true);
        outbound.setRoutingKey("foo"); // default exchange - route to queue 'foo'
        return outbound;
    }



    @MessagingGateway(defaultRequestChannel = "amqpOutboundChannel")
    public interface MyGateway {
        String sendToRabbit(String data);

    }

}

FileProcessor:

公共类FileProcessor {

public void process(Message<String> msg) {
    String content = msg.getPayload();
    JSONObject jsonObject ;
    Map<String, String> dataMap = new HashMap<String, String>();
    for(int i=0;i<=content.length();i++){
    String userId = content.substring(i+5,i+16);


    dataMap = new HashMap<String, String>();

    dataMap.put("username", username.trim());


    i+=290; //each record of size 290 in file
     jsonObject = new JSONObject(dataMap);
    System.out.println(jsonObject);

    }

}

}

1 回答

  • 0

    您的代码是正确的,但有一个例外情况告诉您需要从直接通道“processFileChannel”读取消息的内容 .

    请阅读Spring Integration Reference Manual中有关不同 Channels 类型的更多信息 .

    EDIT

    Spring Integration中的一等公民之一是 MessageChannel 抽象 . 有关更多信息,请参见EIP .

    .channel("processFileChannel") 这样的定义意味着声明DirectChannel . 这种通道意味着在发送上接受消息并直接在 send 进程中执行处理 . 在原始Java语言中,它可能听起来像:从另一个服务中调用一个服务 . 如果另一个尚未自动装配,则抛出 NPE .

    因此,如果您使用 DirectChannel 作为输出,则应该在某处声明订阅者 . 我不知道你的逻辑是什么,但这就是它的工作原理,没有别的选择来修复 Dispatcher has no subscribers for channel .

    虽然你可以使用其他一些 MessageChannel 类型 . 但为了这个目的,你应该阅读更多的文档,例如马克费舍尔Spring Integration in Action .

相关问题