java - File issues with threading in tomcat -



java - File issues with threading in tomcat -

i have tomcat server , have controller writes in file, info coming in request. uncertainty whether multiple threads within server can write same file @ same time , cause issues?

my requirement requests appends info same file. not using threading end.

my code follows:

file file = new file(filename); seek { if(!file.exists()) { file.createnewfile(); } inputstream inputstream = request.getinputstream(); filewriter filewriter = new filewriter(filename,true); bufferedwriter bufferwriter = new bufferedwriter(filewriter); bufferwriter.write(ioutils.tostring(inputstream)); bufferwriter.flush(); bufferwriter.close(); }

there standard solution such issue.

you have create singleton class, shared between threads.

this singleton have blockingqueue (e.g. linkedblockingqueue) in threads set messages writing single file.

this singleton self thread , within run() method take values queue , sequentially write needed file.

java tomcat servlets spring-mvc

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -