How to use Filelock in nifi?

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

How to use Filelock in nifi?

sally

I want to use filelock inside nifi ExecuteScript processor code ( i want to
prevent reading and writing inside xml file by several processor), but i
don't want to get lock exceptions (when two processor wants to read the same
file) Should i use Thread.sleep() or any relative logic which is implemented
in java for it , or I should use any nifi logic?.

Here is severla subjects i am interested in:

is it good practice to use fileLocks inside nifi and if it isn't what should
i use instead of it ?
Here is my code but i always get empty responses, what should i change to
get good response i mean(flowfile in which i have written my config.xml
data)
 import org.apache.commons.io.IOUtils;
    import java.nio.charset.StandardCharsets;
    import  java.io.RandomAccessFile;
    import java.nio.channels.FileLock;
    import java.io.BufferedReader;
    import org.apache.commons.io.IOUtils;
    import java.nio.charset.StandardCharsets;
    File file = new File("C://Users//user//Desktop//2//conf.xml");
                String content = '';
                RandomAccessFile ini = new RandomAccessFile(file, "rws");
               FileLock lock = ini.getChannel().lock();
              BufferedReader  s;

                    try {
                        String sCurrentLine;

                    s= new
BufferedReader(Channels.newReader(ini.getChannel(), "UTF-8"));
                        while ((sCurrentLine = s.readLine()) != null) {
                            content+=sCurrentLine;
                        }
                  FlowFile flowFile=session.create();

                 
flowFile=session.putAttribute(flowFile,"filename","conf.xml");
                    session.write(flowFile, {outputStream ->
                       
outputStream.write(content.getBytes(StandardCharsets.UTF_8));
                    } as OutputStreamCallback);



                       s.close();
                     session.transfer(flowFile,REL_SUCCESS);

                    }


                    catch (FileNotFoundException e) {
                        e.printStackTrace();


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                  finally {
                        lock.release();
                        ini.close();

                    }



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