Can't use renameTo in nifi custom processor code

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Can't use renameTo in nifi custom processor code

sally
One  of the  most  important  part  of my code is  that  it  should put  file
in folder.   i use  this  code  for  renaming ".conf.xml"   to  "conf.xml"
but it  doesn't  work  properly:
 final Path rootDirPath =
get("C://Users//user//Desktop//try2//nifi-1.3.0//1");
                final Path tempCopyFile =
rootDirPath.resolve("."+flowFile.getAttribute(CoreAttributes.FILENAME.key()));
                final Path dotCopyFile =tempCopyFile;
                Path tempDotCopyFile = null;
                tempDotCopyFile = dotCopyFile;

                final Path
copyFile=rootDirPath.resolve(flowFile.getAttribute(CoreAttributes.FILENAME.key()));
                Path finalCopyFile = copyFile;
                if (!Files.exists(rootDirPath)) {
                    if (true) {
                        Files.createDirectories(rootDirPath);
                    }
                }


                final Path finalCopyFileDir = finalCopyFile.getParent();
                if (Files.exists(finalCopyFileDir)) { // check if too many
files already
                    final int numFiles =
finalCopyFileDir.toFile().list().length;

                    if (numFiles >= 10) {
                        flowFile = session.penalize(flowFile);
                        logger.warn("Penalizing {} and routing to 'failure'
because the output directory {} has {} files, which exceeds the "
                                + "configured maximum number of files", new
Object[]{flowFile, finalCopyFileDir, numFiles});
                        session.transfer(flowFile, REL_FAILURE);
                        return;
                    }
                }
                session.exportTo(flowFile,dotCopyFile,false);

                //dotCopyFile.toFile().renameTo(finalCopyFile.toFile());

                logger.info("added {} to flow", new
Object[]{flowFile,flowFile1});
                boolean renamed = false;
                for (int i = 0; i < 10; i++) { // try rename up to 10 times.
                    if
(dotCopyFile.toFile().renameTo(finalCopyFile.toFile())) {
                        renamed = true;
                        break;// rename was successful
                    }
                    Thread.sleep(100L);// try waiting a few ms to let
whatever might cause rename failure to resolve
                }

                if (!renamed) {
                    if (Files.exists(dotCopyFile) &&
dotCopyFile.toFile().delete()) {
                        logger.debug("Deleted dot copy file {}", new
Object[]{dotCopyFile});
                    }
                    throw new ProcessException("Could not rename: " +
dotCopyFile);
                } else {
                    logger.info("Produced copy of {} at location {}", new
Object[]{flowFile, finalCopyFile});
                }



--
Sent from: http://apache-nifi-developer-list.39713.n7.nabble.com/