By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

Access a servlets data in a non-servlet, threading?

P: 25
Hello,

I am looking for a solution to update a .jsp page (kind of progress bar) which shows current progress in a huge simulation when users have to wait for longer periods and the way to do it was have the jsp page refresh often and the page data update by a bean containing the properties file details. The main servlet class continously running on the server regularly updates the properties file.

I am currently using a properties file, which is updated by property IO methods load() and store() which is then accessed by a non-servlet class to read and perform specific actions. Potential problems could be when reading and writing into properties file by two different sources at the same time. Another bigger problem is this works only for one user. This cannot be used by multi-user as the properties file cannot be instantiated/shared unlike a servlet.

The problem is that there cannot be a session or application scoped variable that can be shared between a servlet and non-servlet easily and hence a lack of easier way of communicating with each other.

Any other good solutions to such problems?? Can we add a thread in a servlet which allows us to access a particular variable value in the servlet anywhere else?

Thanks in advance.

Subject: How do we share data between a servlet and a non-servlet when you want to access data that changes in time?
Feb 19 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 785
If more than one program access the properties file at the same time, (and especially if these programs do not run inside the same JVM, but are independent as in your case), you can use file-locks for synchronization.

(If they run inside the same JVM, you could simply declare the static methods "synchronized" or use a semaphore)

How to synchronize access with file-locks:
The writer process first creates an empty file "writeLock" (preferably in a temp directory) and flushes the write-cache. Then it iteratively reads the temp directory until it cannot find any "reading*" files there anymore.Then it modifies the desired properties file. After that it deletes the "writeLock" file.
Each one of the reader processes first searches for the "writeLock" file and waits until it's not there anymore. Then this reader process X creates an empty file "readingX" and starts reading this file. before it finishes, it deletes this file "readingX".

By the way,
you should consider using Ajax instead of a jsp-page-refresh; it's much faster and user-friendly.
Feb 19 '08 #2

P: 25
Thanks you for your suggestion,

Unfortunately, this becomes a single user solution. I am unable to find a way to implement this in a multi-user application. Obviously, I cannot create a temporary file for each new user (new session but same JVM?) and have that new file also synchronized between a servlet and a non-servlet.

I am still looking at other solutions as I know many have already implemented this and I am eager to explore an alternative intuitive approach to this problem of multiple users.

Cheers
Krishna
Mar 24 '08 #3

P: 25
Thanks you for your suggestion,

Unfortunately, this becomes a single user solution. I am unable to find a way to implement this in a multi-user application. Obviously, I cannot create a temporary file for each new user (new session but same JVM?) and have that new file also synchronized between a servlet and a non-servlet.

I am still looking at other solutions as I know many have already implemented this and I am eager to explore an alternative intuitive approach to this problem of multiple users.

All I need to display is the status of the current process, lets say there are a million records of five different files to be processed by a servlet, I have to show the user atleast as a percentage of records processed for each file every couple of seconds while the process is continuing at the server side.


Cheers
Krishna
Mar 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.