Servlet that starts a thread only once for every visitor - java

Hey I want to implement a Java Servlet that starts a thread only once for every single user. Even on refresh it should not start again. My last approach brought me some trouble so no code^^. Any Suggestions for the layout of the servlet?
public class LoaderServlet extends HttpServlet {
// The thread to load the needed information
private LoaderThread loader;
// The last.fm account
private String lfmaccount;
public LoaderServlet() {
super();
lfmaccount = "";
}
#Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if (loader != null) {
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.write(loader.getStatus());
out.flush();
out.close();
} else {
loader = new LoaderThread(lfmaccount);
loader.start();
request.getRequestDispatcher("WEB-INF/pages/loader.jsp").forward(
request, response);
}
}
#Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if (lfmaccount.isEmpty()) {
lfmaccount = request.getSession().getAttribute("lfmUser")
.toString();
}
request.getRequestDispatcher("WEB-INF/pages/loader.jsp").forward(
request, response);
}
}
The jsp uses ajax to regularly post to the servlet and get the status. The thread just runs like 3 minutes, crawling some last.fm data.

What you need here is Session listener. The method sessionCreated() will be called only once for every browser session. So, even if the user refreshes the page, there will be no issues.
You can then go ahead and start the thread for every sessionCreated() method call.

Implement javax.servlet.SingleThreadModel => the service method will not be executed concurrently.
See the servlets specification.

Hypothetically it could be implemented by creating a Map<String,Thread> and then your servlet gets called it tries to look up the map with the sessionId.
Just a sketch:
public class LoaderServlet extends HttpServlet {
private Map<String,Thread> threadMap = new HashMap<>();
protected void doPost(..) {
String sessionId = request.getSesion().getId();
Thread u = null;
if(threadMap.containsKey()) {
u = threadMap.get(sessionId);
} else {
u = new Thread(...);
threadMap.put(sessionId, u);
}
// use thread 'u' as you wish
}
}
Notes:
this uses session id's, not users to associate threads
have a look at ThreadPools, they are great
as a commenter pointed out: synchronization issues are not considered in this sketch

Your first task is to figure out how to identify users uniquely, for instance how would you discern different users behind a proxy/SOHO gateway?
Once you have that down it's basically just having a singleton object serving a user<->thread map to your servlet.
And then we get into the scalability issue as #beny23 mentions in a comment above... I absolutely concur with the point made - your approach is not sound scalability-wise!
Cheers,

As I understand, you want to avoid parallel processing of requests from the same user. I'd suggest you other approach: associate lock with each user and store it in session. And before start processing of users request - try to get that lock. So current thread will wait while other requests from this user are handling. (Use session listener to store lock, when session is created)

Related

Tomcat background processing

I am using Tomcat 9.0 to run Java code on some data from a database.
I have a single servlet that is invoked by directly accessing x.x.x.x:8080/myapp/myservlet.
The length of time to complete is between 5 seconds - 1 minute.
The servlet returns a response right away, leaving it to continue processing in the background. I am not sure Tomcat is supposed to be used like this.
The problem is until the processing has actually finished, the web client cannot access x.x.x.x:8080/myapp/myservlet.
Each new web client can connect and invoke the servlet fine.
I simply want to invoke my java code as a background process in a fire and forget manner. Is this possible with Tomcat?
Any guidance would be great
Move you code in a Thread or an executor and use the servlet only to start and monitor the execution.
public class myservlet extends HttpServlet
{
...
Thread t = null;
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/plain;charset=UTF-8");
PrintWriter out = response.getWriter();
if(t == null || !t.isAlive())
{
t = new Thread(new Runner());
t.setDaemon(true);
t.start();
out.write("Process started.\n");
}
else
out.write("Process running...\n");
}
public static class Runner implements Runnable
{
public void run()
{
// put your code here
}
}
}

Run high computation task in async with feedback

I am using spring for out web application. One API does a lot of computation which looks like this.
void serviceMethod(){
fetchFromDB();
veryLongComputation1(); //1
veryLongComputation2(); //2
veryLongComputation3(); //3
}
My API takes a lot of time to run. Steps 1,2 and 3 takes lot of time because they have lot of computation and lot of IO (to db) too.
What I want is to return the response and run the 1,2,3 in a thread. But problem with that approach is if my application crashes, this code will never be executed.
Can someone suggest some approaches to encounter this problem? One thing to remember, there will be many instances of the application.
Java provides an async servlet to handle requests which takes long time to complete. The basic idea is Http thread in the servlet container triggers the computation and returns immediately whereas the response is sent only when the computation is complete. See the sample below
#WebServlet(urlPatterns={"/asyncservlet"}, asyncSupported=true)
public class AsyncServlet extends HttpServlet {
/* ... Same variables and init method as in SyncServlet ... */
#Override
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
final AsyncContext acontext = request.startAsync();
acontext.start(new Runnable() {
public void run() {
String param = acontext.getRequest().getParameter("param");
String result = resource.process(param);
HttpServletResponse response = acontext.getResponse();
/* ... print to the response ... */
acontext.complete();
}
}
The same thing in spring
#GetMapping(value = "/asyncNonBlockingRequestProcessing")
public CompletableFuture<String> asyncNonBlockingRequestProcessing(){
ListenableFuture<String> listenableFuture = getRequest.execute(new AsyncCompletionHandler<String>() {
#Override
public String onCompleted(Response response) throws Exception {
logger.debug("Async Non Blocking Request processing completed");
return "Async Non blocking...";
}
});
return listenableFuture.toCompletableFuture();
}

Passing Session parameters to Thread

I wanted to do two tasks simultaneously in web project in the Servlet once the user clicks on submit button
1. Run a code to trigger some backend activity
2. Display a webpage to the user.
I tried with the code sample here
As I have few session attributes being set I need to set this in one of the thread. I tried putting point one in one thread and point two in second but variables are not getting resolved to the thread from doPost() method.
Servlet:
public class JOBRUN extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AESASNewOpenPeriod=request.getParameter("AESASNewOpenPeriod");
ScriptRunOption = Integer.parseInt(request.getParameter("AESASJOBRUNOPTION"));
HttpSession session=request.getSession();
String Stream="aaaa";
session.setAttribute("AEStream", Stream);
//Do Job 1 (Update table)
//Do Job 2 (Display webpage to user)
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
You can create an anonymous thread (if you don't want a dedicated Thread class for ) Job 1.
new Thread(new Runnable() {
Session localSession = session;// assign the session object to thread variable.
public void run() {
// you can access localSession here. and do the JOB 1
}
}).start();// this will run asynchrously(non blocking).
Also if you want to pass only some attributes to do the Job 1(i,e if u don't want to change the session), you can pass relevant attributes only.For example
String threadStream = session.setAttribute("AEStream");//local memeber variable inside anonymous thread
Then from the next line after thread, you can do Job 2.
Note: If you mean something else- running an asychrounous worker thread with request , you start wit Servlet 3.x AsyncContext

Can i override doPost method of servlet many times

I am calling another servlet from main servlet,It would have been easy by implementing jsp ,but my aim for this experiment is to use only servlet,pls help
You can't override a method more than once in a class, so you can't override the doPost several times.
If you mean overload it, there's not a good reason for doing that. In the end, only one of those methods will be called by the Servlet Container.
If you want to handle more than 1 kind of requests using a single Servlet, you can send a parameter indicating the action you will perform. For example:
#WebServlet("/person")
public class PersonCRUDServlet extends HttpServlet {
private static final String ADD = "add";
private static final String DELETE = "delete";
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
//using if assuming you work with Java SE 6
if (ADD.equals(action)) {
add(request, response);
} else
if (DELETE.equals(action)) {
delete(request, response);
} else {
//submitted action can't be interpreted
//or no action was submitted
errorForward(request, response);
}
}
private void add(HttpServletRequest request, HttpServletResponse response) {
//handle logic for add operation...
}
private void delete(HttpServletRequest request, HttpServletResponse response) {
//handle logic for delete operation...
}
private void errorForward(HttpServletRequest request, HttpServletResponse response) {
//handle logic for delete operation...
}
}
Note that this is a lot of work to handle manually (this is a reason why Java Web MVC frameworks exists). You can also refer to
Java EE web development, where do I start and what skills do I need?
What to learn for making Java web applications in Java EE 6?

Java Exception handling and HttpSession

As part of exception handling, I want to print data from HTTP session like below:
try{
//business logic
} catch(Exception ex){
String user = session.get("userId"); //get user from HTTP Session.
log.info("Exception when processign the user "+user);
}
My question is do I get the correct UserId for which exception occurred since there will be mulitple threads updating the session?
The HttpSession is not shared among clients. So that part is safe already. The remnant depends on your own code as to obtaining and handling the HttpSession instance. If you're for example assinging the HttpSession as an instance variable of an application wide class, like the servlet itself, then it is indeed not thread safe as it might be overridden by another request at the moment you're accessing it.
public class SomeServlet extends HttpServlet {
private HttpSession session;
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
session = request.getSession();
// ...
Object object = session.getAttribute("foo"); // Not threadsafe!
}
See also:
How do servlets work? Instantiation, sessions, shared variables and multithreading
Session will be unique for each user (if code is according to standard).
Here is sun tutorial on how session works

Categories