[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

Servlet calling thread

Is it possible to call a thread from a servlet and note the thread in a log file and then return the control of the servlet with the thread still running?
And later if I want to stop the thread get the thread id from the log file and stop the thread if I want to?
0
PraKash
Asked:
PraKash
  • 9
  • 8
1 Solution
 
objectsCommented:
SOunds like it would be easier to store the thread in the session.
0
 
aozarovCommented:
If you put your thread in the session (hence this thread is related to a specific user session) then you need
to make sure that when the session is expired that the thread will be stopped. One way to achive it is to put the
Thread in an Object that implements HttpSessionBindingListener and bind that object to the session (instead of the Thread itself).
When the session will expire it will call the valueUnbound of that object where you can stop the running thread.
see http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSessionBindingListener.html#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)

>> And later if I want to stop the thread get the thread id from the log file and stop the thread if I want to?
The problem with this aproach is how to identify the Thread in order to stop it.
One way to achive that is to have some static map where you hold
all your created threads with an id as the key and the Thread as the value.
There is no much point saving such id to disk because that thread will not live beyond the application.
0
 
PraKashAuthor Commented:
I want to run a background process that works similar to a scheduler which updates a bunch of log files and starts processes by itself. This thread will run forever unless I log into the servlet again and decide to stop the process. So storing the thread id in a session would not help me in this logic.

So it is possible to run a thread from the servlet and store it in a log file and forget abt the servlet totally???
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
aozarovCommented:
>> So it is possible to run a thread from the servlet and store it in a log file and forget abt the servlet totally???
Did you read the second part of my answer?
You can run a Thread in your servlet and that Thread can write to a log file.
As I said you will need a way to get back a handle to this thread (when you want to stop it) and that can be
done by storing this thread as a static variable or inside the applicaiton context. (log file will not help you here).
0
 
PraKashAuthor Commented:
aozarov,

What do u mean by application context. The servlet will run the thread and store the thread id in a log file. This thread will run forever. If I want to stop it, I will open the same servlet or a differnet on, get the thread id from the log file and then try to stop it.

Can this be done? I understand it can be done through a static variable, but I dont want the servlet that starts this thread to be open as long as I expect the thread to be running.

I hope I am clear enough. The thing is am new to the servlet and thread thingy, so I hope I presented it properly.
0
 
aozarovCommented:
>> What do u mean by application context
Application/servlet context is a place to put data that will live for the whole duration of the application
see http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#setAttribute(java.lang.String, java.lang.Object)
and http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getAttribute(java.lang.String)

>> store the thread id in a log
What kind of an id?

>> get the thread id from the log file and then try to stop it
How will you find that thread? even when given some id.

>> Can this be done?
Yes, one way is as I described above.

>>  understand it can be done through a static variable, but I dont want the servlet that starts this thread to be open as long as
Storing that thread in some static variable of any accesible class (including the servlet) will not cause the servlet request to "be open".
It has nothing to do with the life cycle of that servlet.

>> I hope I am clear enough
You are clear, I just want to make it clear that to access the same Thread you will need to store it
in some accessible place (e.g servlet context or static variable) leaving it without any reference and having some id in a log file will not help you.
0
 
PraKashAuthor Commented:
Thanks for your help.
I thought when u create a thread u get an ID or something to identify this thread from elsewhere.

Ok all that am developing right now is a servlet and a java program with while(true) loop.

So where is that I can store this application context? Can it be stored in the application server (I am using jboss)?
0
 
PraKashAuthor Commented:
And also if I need to run my whole thing as an application on a jboss, can you tell me a site wherein I can see how an application can be invoked in jboss and then use multiple servlets requests to the application on my jboss application server to open threads?
0
 
aozarovCommented:
>> I thought when u create a thread u get an ID or something to identify this thread from elsewhere.
No you don't you just get the Thread itself.


>> So where is that I can store this application context?
you can do that from your servlet
see: http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/GenericServlet.html#getServletContext()

Thread thread = getServletContext().getAttribute("MyThread");
if (thread == null)
{
thread = new Thread(....)
getServletContext().setAttribute("MyThread", thread);
}

...
0
 
aozarovCommented:
jboss application server comes embeded with tomcat as its web container  (since 3.2.X)  (so you can use servlets/jsp)
see this: http://www.roseindia.net/jboss/buildingwebapplicationwithant.shtml (how to write and deploy servlet to jboss)
or look here http://www.roseindia.net/jboss/ (for more information ant/ ejb)
For jboss doc: http://www.huihoo.com/jboss/online_manual/3.0/ 
0
 
PraKashAuthor Commented:
am getting this error. cannot convert object to thread when I do a getAttribute for the thread name
0
 
aozarovCommented:
Sorry (you need a cast):
Thread thread = (Thread) getServletContext().getAttribute("MyThread");
0
 
PraKashAuthor Commented:
ok this is what happened.

I start the thread from the doGet method on the servlet and the page hangs and the processor shoots to a 100%. The servlet is not even opened.

the thread contains this. while(true)
This thread code will check on a log file every minute. Does this mean that this thread will consume 100% of the processor just to check on that?
0
 
PraKashAuthor Commented:
I got it.
I used thread.run instead of start. Anyways is there a way to specify how much CPU a thread can take?
0
 
PraKashAuthor Commented:
Last 2 questions:
1. getServletContext() does each servlet contain different context or does it have only one context for one war file?
2. Is there a way to specify the CPU percentage to use for each thread?
0
 
aozarovCommented:
>>  used thread.run instead of start
thread.run will not run that the logic inside run in a seperate thread. it will block your servlet.
For invoking the logic inside run in parallel you MUST call thread.start(); // see Thread start javadoc for it.

>>Anyways is there a way to specify how much CPU a thread can take?
No, the best you can do is assign priority to that thread from 1-10 where 1 is the highest priority.

>> 1. getServletContext() does each servlet contain different context or does it have only one context for one war file?
getServletContext() will return a ServletContext object that represents the web container and is the same for all your servlets
in that web application

>> 2. Is there a way to specify the CPU percentage to use for each thread?
No. answered above.
0
 
PraKashAuthor Commented:
Thank you for your reply.
0
 
aozarovCommented:
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now