Nifi:is it possible to use File Lock in nifi?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Nifi:is it possible to use File Lock in nifi?

sally
I  want  to use File lock  and Thread sleep  in nifi processor( ExecuteScript
processor code)  Can   you   link me  any article  or example   of  relative
cases, now  i  use  simple   java  file  lock  and  thread  sleeps    but
when  several (ExecuteScipt  processor   works at the  same  time)  neither
file  lock   nor sleep  logic    works   properly i  mean  several
processor  can get access  to  file at the same  time  and  Thread  sleep
can  kill processSession  and  turn  of  processor at all .  what  should  i
change? here  is my  code  example :
import java.nio.charset.StandardCharsets
import org.apache.commons.io.IOUtils
import org.apache.nifi.processor.io.StreamCallback
import groovy.xml.MarkupBuilder
import groovy.xml.StreamingMarkupBuilder
import org.w3c.dom.Document
import org.xml.sax.SAXException
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.ParserConfigurationException
import javax.xml.transform.Transformer
import javax.xml.transform.TransformerConfigurationException
import javax.xml.transform.TransformerException
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
import java.nio.channels.Channels
import java.nio.channels.FileLock
import java.nio.channels.OverlappingFileLockException
import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit
import java.io.*
import java.util.*
import groovy.util.XmlParser

 def static addDays(def date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(date));
        c.add(Calendar.DATE, 1);  // number of days to add
        String m = sdf.format(c.getTime());  // dt is now the new date
        return m;
    }
def  flowFile=session.get()
ArrayList<String> value = new ArrayList<>();
if(flowFile!=null){
  value.add(flowFile.getAttribute('filename');
  session.remove(flowFile);
                }
 File file = new
File("C://Users//user//Desktop//try2//nifi-1.3.0//4//conf2.xml");
        String content = "";
        String material = "";
        BufferedReader s;
        BufferedWriter w;
        RandomAccessFile ini = new RandomAccessFile(file, "rwd");
        FileLock lock = ini.getChannel().lock();
        DocumentBuilder dBuilder;
        Document document, doc;
        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<value.size();i++) {
                date = value.get(i).substring(0, 10);
                xml.rs.borderCross.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 stringWriter = new StringWriter()
            new XmlNodePrinter(new PrintWriter(stringWriter)).print(xml)
            def newXml = stringWriter.toString()
            TransformerFactory transformerFactory =
TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            InputStream stream1 = new
ByteArrayInputStream(newXml.toString().getBytes("UTF-8"));
            DocumentBuilderFactory dbFactory1 =
DocumentBuilderFactory.newInstance();
            dBuilder = dbFactory1.newDocumentBuilder();
            document = dBuilder.parse(stream1);
            DOMSource source=new DOMSource(document);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            StreamResult result1 = new StreamResult(bos);
            try {
                transformer.transform(source, result1);
            } catch (TransformerException e) {
                e.printStackTrace();
            }
            byte[] array = bos.toByteArray();
            data = bos.toString();
            if (!data.isEmpty()) {
                 ini.setLength(0);
                w = new BufferedWriter(Channels.newWriter(ini.getChannel(),
"UTF-8"));
                w.write(data);
                lock.release();
                w.close();
            }
            else{
                ini.setLength(0);
                w = new BufferedWriter(Channels.newWriter(ini.getChannel(),
"UTF-8"));
                w.write(content);
                lock.release();
                w.close();

            }
        }catch (FileNotFoundException e) {
            TimeUnit.SECONDS.sleep(50000);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException 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-developer-list.39713.n7.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Nifi:is it possible to use File Lock in nifi?

Joe Witt
Sally,

I'm glad you're finding NIFi useful and engaging with the community.
A couple observations to share

Please subscribe to the mailing lists you are most interested in.
This can be done as shown here
https://nifi.apache.org/mailing_lists.html

It is probably best to avoid sharing code, or at least large blocks of
it, in these mailing list posts.  The formatting is problematic and it
makes actually reviewing/responding more difficult.  Alternatively you
could send a link to the code in a place like Github where it makes it
a bit easier to look at/respond to.

Lastly, please avoid posting the same or very similar questions in a
number of different forums at once.  It makes providing
responses/closure more difficult and if people have already responded
in once place not another, like these mailing lists, then it might
give people the impression we're not responsive which is certainly not
the case.

Thanks
Joe


On Wed, Oct 18, 2017 at 10:08 AM, sally <[hidden email]> wrote:

> I  want  to use File lock  and Thread sleep  in nifi processor( ExecuteScript
> processor code)  Can   you   link me  any article  or example   of  relative
> cases, now  i  use  simple   java  file  lock  and  thread  sleeps    but
> when  several (ExecuteScipt  processor   works at the  same  time)  neither
> file  lock   nor sleep  logic    works   properly i  mean  several
> processor  can get access  to  file at the same  time  and  Thread  sleep
> can  kill processSession  and  turn  of  processor at all .  what  should  i
> change? here  is my  code  example :
> import java.nio.charset.StandardCharsets
> import org.apache.commons.io.IOUtils
> import org.apache.nifi.processor.io.StreamCallback
> import groovy.xml.MarkupBuilder
> import groovy.xml.StreamingMarkupBuilder
> import org.w3c.dom.Document
> import org.xml.sax.SAXException
> import javax.xml.parsers.DocumentBuilder
> import javax.xml.parsers.DocumentBuilderFactory
> import javax.xml.parsers.ParserConfigurationException
> import javax.xml.transform.Transformer
> import javax.xml.transform.TransformerConfigurationException
> import javax.xml.transform.TransformerException
> import javax.xml.transform.TransformerFactory
> import javax.xml.transform.dom.DOMSource
> import javax.xml.transform.stream.StreamResult
> import java.nio.channels.Channels
> import java.nio.channels.FileLock
> import java.nio.channels.OverlappingFileLockException
> import java.text.SimpleDateFormat
> import java.util.concurrent.TimeUnit
> import java.io.*
> import java.util.*
> import groovy.util.XmlParser
>
>  def static addDays(def date) {
>         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
>         Calendar c = Calendar.getInstance();
>         c.setTime(sdf.parse(date));
>         c.add(Calendar.DATE, 1);  // number of days to add
>         String m = sdf.format(c.getTime());  // dt is now the new date
>         return m;
>     }
> def  flowFile=session.get()
> ArrayList<String> value = new ArrayList<>();
> if(flowFile!=null){
>   value.add(flowFile.getAttribute('filename');
>   session.remove(flowFile);
>                 }
>  File file = new
> File("C://Users//user//Desktop//try2//nifi-1.3.0//4//conf2.xml");
>         String content = "";
>         String material = "";
>         BufferedReader s;
>         BufferedWriter w;
>         RandomAccessFile ini = new RandomAccessFile(file, "rwd");
>         FileLock lock = ini.getChannel().lock();
>         DocumentBuilder dBuilder;
>         Document document, doc;
>         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<value.size();i++) {
>                 date = value.get(i).substring(0, 10);
>                 xml.rs.borderCross.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 stringWriter = new StringWriter()
>             new XmlNodePrinter(new PrintWriter(stringWriter)).print(xml)
>             def newXml = stringWriter.toString()
>             TransformerFactory transformerFactory =
> TransformerFactory.newInstance();
>             Transformer transformer = transformerFactory.newTransformer();
>             InputStream stream1 = new
> ByteArrayInputStream(newXml.toString().getBytes("UTF-8"));
>             DocumentBuilderFactory dbFactory1 =
> DocumentBuilderFactory.newInstance();
>             dBuilder = dbFactory1.newDocumentBuilder();
>             document = dBuilder.parse(stream1);
>             DOMSource source=new DOMSource(document);
>             ByteArrayOutputStream bos = new ByteArrayOutputStream();
>             StreamResult result1 = new StreamResult(bos);
>             try {
>                 transformer.transform(source, result1);
>             } catch (TransformerException e) {
>                 e.printStackTrace();
>             }
>             byte[] array = bos.toByteArray();
>             data = bos.toString();
>             if (!data.isEmpty()) {
>                  ini.setLength(0);
>                 w = new BufferedWriter(Channels.newWriter(ini.getChannel(),
> "UTF-8"));
>                 w.write(data);
>                 lock.release();
>                 w.close();
>             }
>             else{
>                 ini.setLength(0);
>                 w = new BufferedWriter(Channels.newWriter(ini.getChannel(),
> "UTF-8"));
>                 w.write(content);
>                 lock.release();
>                 w.close();
>
>             }
>         }catch (FileNotFoundException e) {
>             TimeUnit.SECONDS.sleep(50000);
>         } catch (IOException e) {
>             e.printStackTrace();
>         } catch (TransformerConfigurationException e) {
>             e.printStackTrace();
>         } catch (ParserConfigurationException e) {
>             e.printStackTrace();
>         } catch (SAXException 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-developer-list.39713.n7.nabble.com/