0

我有一个从 mongodb 读取并将该信息发送到队列的项目。我的听众从云端获取队列消息。我能够创建一个 .txt 文件,该文件从队列中输入所有信息。我一直在寻找的问题是:如何对 POJO(IBusiness1、IBusiness2、IBusiness3)中的特定字段进行排序并为每个字段创建文件?下面的代码只允许我创建 1 个 txt 文件,它不对字段进行排序:

    public static void main(String[] args) {
        SpringApplication.run(PaymentPortalBatchListenerApplication.class, args);
    }

    private class MessageHandler implements IMessageHandler {
        private final Logger logger = LoggerFactory.getLogger(MessageHandler.class);

        public CompletableFuture<Void> onMessageAsync(IMessage message) {
            System.out.println("received "+message.getBody());
            ObjectMapper om = new ObjectMapper();
            PortalList auditList = null;

            try {
                auditList = om.readValue( message.getBody(), PortalList.class );
                System.out.println( "**Audit Message   " + auditList );
                logger.info( "Creating file");
                String exportFilePath = "C:\\filewriter\\IBusiness1 " + 
                LocalDateTime.now().format(formatter) + ".txt";
                File file = new File(exportFilePath);
                FileWriter writeToFile = new FileWriter(file);
                String exportFileHeader = "CREATE_DTTM|FNAME|LNAME|IBusiness";
                StringHeaderWriter headerWriter = new 
                StringHeaderWriter(exportFileHeader);
                writeToFile.write(exportFileHeader);
                writeToFile.write( String.valueOf( headerWriter));
                writeToFile.write( String.valueOf(auditList));
                writeToFile.flush();

            } catch (IOException e) {
                e.printStackTrace();
            }

//          System.out.println(auditList);
            return CompletableFuture.completedFuture(null);
        }
4

1 回答 1

0

这是我所做的:

PaymentPortalBean = POJO 审计列表 = PPB 的本地副本

门户列表 =

    import lombok.Data;

import java.util.List;

@Data
public class PortalList{

    private List<PaymentPortalBean> portalList;
}

创建文件的答案:

         for(PaymentPortalBean bean: auditList.getPortalList()) {
                if(bean.RxBusiness().contains( "IBusiness")){
                    File file = new File( exportFilePath );
                    FileWriter writeToFile = new FileWriter( file );
                    String exportFileHeader = CREATE_DTTM|FNAME|LNAME|IBusiness";
                    writeToFile.write( exportFileHeader );
                    writeToFile.write( String.valueOf(bean));
                    writeToFile.flush();
                }

为了找到 IBusiness,我为我需要的类型创建了另外两个条件语句。运行良好。Mongo db 能够分离我需要的字段。

于 2019-03-03T21:15:53.017 回答