java - Invoking asynchronous method from servlet -


context:

i have done alot of reading, found this semi-relevant.

i have servlet calls method java class, while passing session-sensitive data. running on tomcat server.

it looks this:

@webservlet(urlpatterns = {"/myservlet", "/"}) public class myservlet extends httpservlet {      protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception{          httpsession session = request.getsession(true); //start session         string userid = session.getid();                //get unique user id         myclass cls = new myclass();                    //initialize class         string shortid = cls.newid(userid);         //call method class         system.out.println("your short id is: " + shortid); //print result     } } 

and class i'm calling looks this:

public class myclass {      public string newid(string userid){         string shortid;             ... //method shortening userid             return(shortid);     } } 

the method of course requires more processing shown in trivial example.

question 1:

now, current understanding when n users call myservlet simultaneously, tomcat creates n threads in doget method. when method newid called synchronously, tomcat queues threads , and allows them execute 1 after another. correct?

question 2:

the problem arises when have large number of users, n th user have wait newid method completed n-1 times, might take while. therefore want call newid method asynchronously in order run n threads in parallel, increase throughput.

how can achieve this?

would recommend using reflections, or wrapping newid method inside runnable?

does tomcat take care of executorservice, or need implement too?

any appreciated. example code useful too!


question 1:

the servlet container keeps pool of threads uses handle requests. keeps single instance of servlet class. when request comes in, dispatches 1 of threads hits corresponding doxxx method. threads aren't queued. work in parallel. requests come in @ same time being handled parallelly (to degree, read here.

question 2:

say you've configured servlet container have pool of 10 threads handle requests. @ point 15 requests. yes, 5 requests queued awaiting available thread. in case, can use servlet 3.0 async support (read1, read2) have request handled in separate thread (that container manages). when processing complete, pool thread re-dispatched finish request processing.

read information how manage threads.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -