Java servlet 用于计算还是仅用于控制?

发布于 2024-10-04 05:07:24 字数 550 浏览 3 评论 0原文

我正在制作一个网站。它基本上计算具有动态变化权重的图上的最短路径。

我打算使用jsp进行开发

原始权重存储在数据库中。一旦我的网站启动并运行,人们就可以登录并执行可能导致边权重发生变化的任务。他们可能会请求从任何节点到任何节点的最短路径。我的问题是:

  1. 每次我收到最短路径查询时,我应该

    a.读取数据库,构造图,计算最短路径并返回结果?或者;
    b.如果只要服务器启动,图表就保留在内存中,则仅在网站启动时从数据库构建图表一次。每当遇到 updateWeight 查询时,权重都会在内存和数据库中更新。

  2. 如果b.那么我如何在内存中维护这个动态图呢? servlet 是处理这个问题的正确技术吗?我正在想象一个场景,我的 jsp 页面将请求 servlet 进行两种查询......当 servlet 第一次启动时,它会读取数据库,并构造图形,然后每当 jsp 页面请求时最短路径,它直接计算,无需查阅数据库。

Servlet 用于控制“进程” - 所以我已经读过...所以 servlet 不适合这个..如果不是那么什么?豆子 ?

i am making a website. it basically calculates shortest paths on a graph with dynamically changing weights.

I am planning to use jsp for development

The original weights are stored in the database. Once my website is up and running, people may log in and perform tasks that may cause the edge weights to change. They may request shortest paths from any node to any node. My question is :

  1. Everytime i get a Shortest path query, should I

    a. read the database, construct a graph, calculate shortest path and return the result ? Or;
    b. Should the graph be maintained in the memory as long as the server is up, the graph is constructed from the database only once when the website is up. Whenever an updateWeight query is encountered, the weights are updated in both the memory as well as database.

  2. If b. then how do i maintain this dynamic graph in memory ? Is a servlet the right technology to handle this? I am imagining a scenario where my jsp pages will request the servlet for both kind of queries .... When the servlet is started for the very first time, it reads the database, and constructs the graph, and then whenever a jsp page requests a shortest path, it calculates directly without having to consult the database.

The Servlet is used for control the 'process' - so i have read... so is servlet not the right thing for this .. if not then what ? beans ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-10-11 05:07:24

选择取决于您:如果您同意该用户等待很长时间的结果,那么您可以只询问数据库,计算路径,然后返回结果。但是如果您幸运的话,并且您的 servlet 运行在具有大量内存的服务器上,您当然可以将图形加载到内存中并在那里维护它。

Servlet技术在这里就可以了。只是不要在 doGet() 或 doPost() 方法中编写整个计算代码。 Servlet 实际上应该充当控制器并仅从其他类获取结果。将代码移至另一个线程并在计算器类中完成工作。

The choice is up to you: if you ok with that user will wait for the result for some long time, then you can just ask the database, calculate the path and then return the result. But if you're lucky, and your servlet is running on server with large amount of memory you can of course load the graph to the memory and maintain it there.

Servlet technology is ok here. Just don't write the whole calculating code inside doGet() or doPost() methods. Servlet should really act as a controller and just obtain result from other classes. Move the code to another thread and do the work within calculators classes.

楠木可依 2024-10-11 05:07:24

a.读取数据库,构造图,计算最短路径并返回结果?或者;
b.如果只要服务器启动,图表就保留在内存中,则仅在网站启动时从数据库构建图表一次。每当遇到 updateWeight 查询时,权重都会在内存和数据库中更新。

取决于数据的大小、处理成本和服务器的硬件容量。我们无法可靠地回答这个问题。只有在接近生产环境的环境中自行运行的探查器才能提供可行的信息。

如果b.那么我如何在内存中维护这个动态图呢? servlet 是处理这个问题的正确技术吗?我正在想象一个场景,我的 jsp 页面将请求 servlet 进行两种查询......当 servlet 第一次启动时,它会读取数据库,并构造图形,然后每当 jsp 页面请求时最短路径,它直接计算,无需查阅数据库。

您可以这样做,但在设计良好的 MVC 方法中,servlet 应该只充当请求/响应控制器。在域/操作对象中执行业务/计算工作(servlet 应该将工作委托给它们)。要进行应用程序范围的初始化,最好是实现一个 ServletContextListener ,它将数据放入应用程序范围内。然后,Servlet 可以从应用程序范围获取它,JSP 也可以通过 EL 访问它。但是,您必须仔细考虑对应用程序范围数据的修改的同步/并发。 DB 更适合于此。

a. read the database, construct a graph, calculate shortest path and return the result ? Or;
b. Should the graph be maintained in the memory as long as the server is up, the graph is constructed from the database only once when the website is up. Whenever an updateWeight query is encountered, the weights are updated in both the memory as well as database.

Depends on the size of the data, the cost of the process and hardware capacity of the server. We can't reliably answer this. Only a profiler which is run by yourself on an environment which is close to production environment can give viable information.

If b. then how do i maintain this dynamic graph in memory ? Is a servlet the right technology to handle this? I am imagining a scenario where my jsp pages will request the servlet for both kind of queries .... When the servlet is started for the very first time, it reads the database, and constructs the graph, and then whenever a jsp page requests a shortest path, it calculates directly without having to consult the database.

You can do so, but in a well designed MVC approach, a servlet should only act as request/response controller. Do the business/computation job in domain/action objects (the servlet should just delegate the job to them). To do application-wide initialization best is to implement a ServletContextListener which puts the data in the application scope. The servlet can then just obtain it from the application scope and the JSP can access it by EL as well. You however have to take synchronization/concurrency of modifications to application wide data carefully into account. A DB is better suited for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文