简体   繁体   English

使用Servlet与持久性Java应用程序对话

[英]Using servlets to talk to persistent java application

See Update Below 请参阅下面的更新

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. 我确定那里有解决方案,但也许我现在还不知道正确的术语。

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. 所以这是我的问题:我有一个Java应用程序,它执行非常复杂的数值计算,并根据环境中的核数等很好地进行扩展。但是,我现在正尝试将此计算公开给Internet接口。

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. 第一步:我只是尝试将其放在servlet中,但意识到这在解决方案上是个头绪,特别是考虑到可以将多个servlet称为产生多个实例。 I want only ONE instance of this calculation running in the background as the multiple users will only be using the same data set. 我只希望该计算的一个实例在后台运行,因为多个用户将只使用同一数据集。

What I want: 1. A user makes a request to a servlet that "starts" this computation. 我想要的是:1.用户向“开始”此计算的servlet发出请求。 This computation takes several minutes to run. 此计算需要几分钟才能运行。 2. The "start" request gets returned so that now using ajax, I can query the progress. 2.将返回“开始”请求,以便现在可以使用ajax查询进度。 3. If the computation is already running and another user tries to start the computation, it won't. 3.如果计算已经在运行,并且另一个用户尝试开始计算,则不会。

My prior searching resulted in this, which I tried to mimic the design: Status of the process triggered by request in java 我之前的搜索结果是这样,我试图模仿该设计: java中的请求触发的进程状态

Thank you! 谢谢!

UPDATE: Okay, so I was able to find a simple example that has 95% of what I want: AJAX (prototype/java) getting partial status updates during execution 更新:好的,所以我能够找到一个简单示例,该示例具有我想要的95%的示例: AJAX(原型/ java)在执行过程中获取部分状态更新

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 < 100) {
            try { sleep(1000); } catch (InterruptedException ignore) {}
            progress++;
        }
    }

    public int getProgress() {
        return progress;
    }
}

What my problem now is : I am storing the separate thread object "longProcess" in the request's context. 我现在的问题是 :我正在请求的上下文中存储单独的线程对象“ longProcess”。 While this works for periodically querying the status of the process, ie (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. 虽然这可以定期查询流程的状态,例如(我将得到1%.... 5%.... 9%....一直到100%),但是当我开始请求时从一个浏览器,然后从另一个浏览器发出请求,我得到两个单独的实例运行。 This I think due to storing it in the request's context. 我认为这是由于将其存储在请求的上下文中。

Is there a way to store the thread object instead in the JVM so that requests from all over can access the same object? 有没有一种方法可以将线程对象存储在JVM中,从而使来自各地的请求都可以访问同一对象? I know I'll need synchronization etc... but I want to be able to see the the same object from different request contexts. 我知道我将需要同步等...但是我希望能够从不同的请求上下文中看到相同的对象。

I tried looking at System.setProperty() but that only seems to be applicable for strings. 我尝试查看System.setProperty(),但这似乎仅适用于字符串。 Is there an equivalent to: request.getSession().setAttribute("longProcess", longProcess); 是否有一个等效项: request.getSession()。setAttribute(“ longProcess”,longProcess); That instead stores it in the JVM for all requests to access? 而是将其存储在JVM中以供所有请求访问?

You can simply store it in a field of the servlet itself, since the same servlet instance is used to serve all the requests. 您可以简单地将其存储在servlet本身的字段中,因为同一servlet实例用于处理所有请求。

If you want it to be available by several different servlets, then store it as an attribute of the ServletContext, which is also known as the "application scope". 如果希望几个不同的Servlet都可以使用它,则将其存储为ServletContext的属性,该属性也称为“应用程序范围”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM