我有一个项目,该项目从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);
}
答案 0 :(得分:0)
这是我所做的:
PaymentPortalBean = POJO auditlist = PPB的本地副本
PortalList =
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能够分隔我所需的字段。