Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing servlets to talk to persistent java application
    primarykey
    data
    text
    <p><strong>See Update Below</strong></p> <p>I've been stumped and have tried searching the forums for possible solutions. I'm sure there is a solution out there, but perhaps I don't know the proper terminology just yet.</p> <p>So here is my problem: I have a java application that does a pretty complicated numerical computation, expands nicely based on the number of cores in the environment and etc... However, I am now trying to expose this computation to an internet interface.</p> <p>First stab: I tried just putting it in a servlet but realized this is a highly frowned upon solution, especially considering multiple servlets can be called producing multiple instances. I want only ONE instance of this calculation running in the background as the multiple users will only be using the same data set.</p> <p>What I want: 1. A user makes a request to a servlet that "starts" this computation. This computation takes several minutes to run. 2. The "start" request gets returned so that now using ajax, I can query the progress. 3. If the computation is already running and another user tries to start the computation, it won't.</p> <p>My prior searching resulted in this, which I tried to mimic the design: <a href="https://stackoverflow.com/questions/15788989/status-of-the-process-triggered-by-request-in-java">Status of the process triggered by request in java</a></p> <p>Thank you!</p> <p><strong>UPDATE:</strong> Okay, so I was able to find a simple example that has 95% of what I want: <a href="https://stackoverflow.com/questions/1722558/ajax-prototype-java-getting-partial-status-updates-during-execution">AJAX (prototype/java) getting partial status updates during execution</a></p> <pre><code>package main; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Main extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Found post request!"); processPostRequest(request, response); } /** * Handles http get requests by mapping them to processRequest() */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Found get request!"); processPostRequest(request, response); } public void processPostRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LongProcess longProcess = null; if(request.getSession().getAttribute("longProcess") != null) { longProcess = (LongProcess) request.getSession().getAttribute("longProcess"); System.out.println("Found longProcess object in JVM!"); response.getWriter().write(String.valueOf(longProcess.getProgress()) + "%"); } else { longProcess = new LongProcess(); //longProcess.setDaemon(true); longProcess.start(); request.getSession().setAttribute("longProcess", longProcess); response.getWriter().write("The long process has started!"); } } } class LongProcess extends Thread { private int progress; public void run() { while (progress &lt; 100) { try { sleep(1000); } catch (InterruptedException ignore) {} progress++; } } public int getProgress() { return progress; } } </code></pre> <p><strong>What my problem now is</strong>: I am storing the separate thread object "longProcess" in the request's context. While this works for periodically querying the status of the process, i.e. (I'll get 1%....5%....9%.... all the way up to 100%), when I start the request from one browser, then make the request from another browser, I get two separate instances running. This I think due to storing it in the request's context.</p> <p>Is there a way to store the thread object instead in the JVM so that requests from all over can access the same object? I know I'll need synchronization etc... but I want to be able to see the the same object from different request contexts. </p> <p>I tried looking at System.setProperty() but that only seems to be applicable for strings. Is there an equivalent to: <strong>request.getSession().setAttribute("longProcess", longProcess);</strong> That instead stores it in the JVM for all requests to access?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload