Java servlets for computation or just conntrol?
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 :
Everytime i get a Shortest path query, should I
a. read the database, construct a graph, calculate shortest path a开发者_StackOverflownd 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.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 ?
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.
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.
精彩评论