Calling a thread from a Java servlet
I'm working on an application which uses servlets to get and set data for the JSPs used by end users. All of the data is manipulated by the servlets in session state before being written to database or after being retrieved from the database.
But some of these database operations are quite lengthy where there's a lot of IO going on involving dynamic creation of tables or insertion of data into, and retreival of data from, tables etc. This process also requires various third party products like Weka and Lucene; so there is a significant processor load. I expect some of these could take up to a minute or two and lock the sessions at this time.
The servlets generally call static methods in classes for these operations which then return the data types necessary. So this is my question; is this sensible or would it be better to implement these operations as Java threads called directly from the servlets or would I need a thread manager? I am a thread newbie.
An example of code for开发者_高级运维 the above in a servlet is something like this:
// Create Data_Miner.
Data_Miner dm = Data_Miner_ReaderWriter.write(activeUser.getUser(), rssfodm);
// Create output.
if (rssfodm.getDataMinerOutputType() == Data_Miner_Output_Type.DECISION_TREE) {
Decision_Tree dt = Decision_Tree_ReaderWriter.write(dm);
Both method calls are to static methods in classes containing methods to read / write the relevant objects.
Thanks
Mr Morgan.
Use asynchronous processing and something like a callback handler and a mailbox where users can come back and get results when they're ready.
Since you're using Java, you can also consider using a JMS message-driven POJO (Spring) or EJB (EJB standard) to accept requests for longer-running transactions.
精彩评论