I have one config file which should be processed by several process ( which
will change several parameters in it) i have used filelocks to prevent
working several process on one file at a time but as a result when i try to
start all process at the same time one process (i have 2 process at present
time) can't change file parameters how should i prevent this? i mean how
should i force these process work together in a friendly way ? i mean
(starting and working on file and changin certain parameters also)p.s ( i am
working inside nifi environment) here is my code example:
def File file = new File("adress");
String content ="";
String material ="";
BufferedReader s;
BufferedWriter w;
RandomAccessFile ini = new RandomAccessFile(file, "rwd");
FileLock lock = ini.getChannel().lock();
DocumentBuilder dBuilder;
Document document;
String date="";
String data;
try {
String sCurrentLine;
s = new BufferedReader(Channels.newReader(ini.getChannel(),
"UTF-8"));
while ((sCurrentLine = s.readLine()) != null) {
content += sCurrentLine;
}
ini.seek(0);
def xml = new XmlParser().parseText(content)
for(int i=0;i<names.size();i++) {
date = names.get(i).substring(0, 10);
xml.details.findAll({ p ->
p.runAs[0].text() == "false" && p.start[0].text() ==
date.toString()
}).each({ p ->
p.start[0].value = addDays( p.start[0].text())
p.runAs[0].value = "true"
})
}
def newXml= groovy.xml.XmlUtil.serialize( xml )
data = newXml.toString()
if (!data.isEmpty()) {
ini.setLength(0);
w = new BufferedWriter(Channels.newWriter(ini.getChannel(),
"UTF-8"));
w.write(data);
lock.release();
w.close();
def flowFile1=session.create();
flowFile1=session.putAttribute(flowFile1,'filename','cc');
flowFile1 = session.write(flowFile1, {outputStream ->
outputStream.write(data.getBytes(StandardCharsets.UTF_8))
} as OutputStreamCallback);
session.transfer(flowFile1,REL_SUCCESS);
}
else{
ini.setLength(0);
w = new BufferedWriter(Channels.newWriter(ini.getChannel(),
"UTF-8"));
w.write(data);
lock.release();
w.close();
}
}catch (FileNotFoundException e) {
TimeUnit.SECONDS.sleep(50000);
} catch (IOException e) {
e.printStackTrace();
} catch (OverlappingFileLockException e) {
TimeUnit.SECONDS.sleep(50000);
lock.release(); ;
} catch (Exception e) {
e.printStackTrace();
} finally {
//lock.release();
ini.close();
}
--
Sent from: http://apache-nifi-users-list.2361937.n4.nabble.com/