Thread management in servlet
Here is the problem that i am trying to solve - and get better understanding of issues to look out for.
1) Problem to solve
Clients sends an http request to an distributor server. The request contains a list of tasks. Each such task can be sent to a particular handling server (out of a known set) which can handle the request. Each task has a task type, which identifies which server the request needs to be passed to. The responses from the handler servers are then to be collected and passed back the client as a consolidated response.
On the distributor server, the servlet thread that is handling the request will
a. need to spread the requests for various handling servers
b. send the requests in parallel to handling servers
b. get all the responses , consolidate and send back
2)Proposed solution
I was thinking of having a thread pool - one thread for each handling server, initialized within servlet . When the distributor server receives a request - the tasks would be spread to these handling threads. The distributor thread would then wait, until the handling threads notify.
When a notification is received - the distributor thread will need to check if all responses are received. If not - it would need to wait (till a max_timeout) else return the response to client
3)Questions
- This involves waiting on the thread created by serlvet container (the distributor thread) and creating a threadpool. Thread creation and management by application in web-container is generally dissuaded - but given this issue - i see no other alternative
suggestions ? Its been a long email - thanks for considering
Fred
How many clients for the entry server do you presume for the application ? I ask this because it is very possible that the server to remain without available threads and give errors. If you use different servers for the final requests I suppose that are some very time consuming processes. What kind of application are running on these servers so that you want to use this kind of distributed computing ?
Why did you choose as an entry point a servlet container and http request for this kind of problem ? Do you need a graphic response in the browser ? If the response time is too long, the browser (Internet Explorer) will show exception and will not wait for the response (for this you must set some variabiles on client). Also the J2EE server must be set so that not to throw a time-out exception.
To prevent these, you could do so that not to block the distributor thread and let it send the answer back to the client. Then you can tell to client browser to query the server from time to time to check the status of the execution (you can do that from javascript). When the execution is ready you can retrun the response to the client. Doing this way you will not block the application server threads.
Now in the back end, you must somehow to take some identificators from the processes and sent them back to the client.
You can use a database to make the bridge between the central server and the other processes (here you can put the processes identificators). When the processes end up they update their results in the database.
The client, when will make the repetitive requests, will check the status of the processes in the database with given identificators.
Sounds like an interesting project.
Similar Messages
-
Hi, I'm thinking on some issues related to Weblogic thread management.
Suppose I start a thread (either on an startup class or from a servlet) that
I want to perform some background work (I have readed it's out of the specs
to start transactions from a thread launched in a servlet... yea I know it
;) ) well the problem is. How do I know when the Weblogic server is being
shutting down?.
I have seen on Weblogic's console that when an script like the
./stopWeblogic.sh one is executed it calls a Weblogic method in order to do
that... then the Weblogic starts to destroy servlets, undeploys EJBs and, at
the end a message like "Waiting 20 seconds for threads to stop" or something
like that is written to the console.
The question is, how does Weblogic notify the running threads that they
should stop?, will it work for a thread launched in a startup class or from
a servlet?, does the thread has to extend from an special Weblogic class?.
If this isn't possible, would it work overriding the destroy() method of the
servlet to call a method of the thread that stops it properly?. I know it
will be called anytime the webapp is redeployed or if the servlet is hot
reloaded, but it's fine to me.
Thanks in advance, regards
Ignacio.Hi, I'm thinking on some issues related to Weblogic thread management.
Suppose I start a thread (either on an startup class or from a servlet) that
I want to perform some background work (I have readed it's out of the specs
to start transactions from a thread launched in a servlet... yea I know it
;) ) well the problem is. How do I know when the Weblogic server is being
shutting down?.
I have seen on Weblogic's console that when an script like the
./stopWeblogic.sh one is executed it calls a Weblogic method in order to do
that... then the Weblogic starts to destroy servlets, undeploys EJBs and, at
the end a message like "Waiting 20 seconds for threads to stop" or something
like that is written to the console.
The question is, how does Weblogic notify the running threads that they
should stop?, will it work for a thread launched in a startup class or from
a servlet?, does the thread has to extend from an special Weblogic class?.
If this isn't possible, would it work overriding the destroy() method of the
servlet to call a method of the thread that stops it properly?. I know it
will be called anytime the webapp is redeployed or if the servlet is hot
reloaded, but it's fine to me.
Thanks in advance, regards
Ignacio. -
Thread pool in servlet container
Hello all,
I'm working on this webapp that has some bad response times and I've identified an area were we could shave off a considerable amount of time. The app is invoking a component that causes data to be catched for subsequently targeted apps in the environemnt. Our app does not need to wait for a response so I'd like to make this an asyncronous call. So, how best to implement this?...I considered JMS, but started working on a solution using the Java 1.4 backport of JSR 166 (java.util.concurrent).
I've been testing the use of a ThreadPoolExecutor, using an ArrayBlockingQueue. The work that each Runnable will perform involves a lot of waiting (the component we call invokes a web service, among a couple other distributed calls). So I figure the pool will be much larger than the queue. Our container has 35 execute threads, so I've been testing with a thread pool size of 25, and a queue of 10.
Any thoughts on this approach? I understand that some of this work could be simplified by JMS, but if I don't need to be tied to the container, I'd prefer not to. The code if much easier to unit test, and plays nicely with our continious build integration (which runs our junit test for us and notifies on errors).
Any thoughts are greatly appreciated...Thanks!!Well, if it works, that's by far the best way to go - but note that creating threads in a servlet container means those threads are outside of the container's control. Many containers will refuse to give the new threads access to the JNDI context, even, and some may prevent you from creating threads at all.
-
Hello Buddies,
We are facing performance problem in our J2EE application. For our application, Native Thread Management is enabled(EnableNativeIO=true).
But, while starting the weblogic server(managed server), we are getting the following message.
####<Oct 16, 2008 12:40:25 PM BST> <Warning> <Socket>
<wrohstsco10> <gc3-wrohstsco10>
<ListenThread.Default> <<WLS Kernel>> <>
<BEA-000402> <There are: 9 active sockets, but the maximum
number of socket reader threads allowed by the configuration is: 8. You
may want to alter your configuration.>
Can any one suggest on this?
Regards
Senthil Nathan Myou know that Native IO is enabled if you see "Native IO Enabled" in the server log at startup. This means that it founds the native IO shared libraries at startup.
You've probably already seen this, but this is from the WebLogic docs, at <http://e-docs.bea.com/wls/docs100/messages/Socket.html>
<strong>BEA-000402</strong>
<strong><em>Warning: </em></strong>There are: <em>numSockets</em> active sockets, but the maximum number of socket reader threads allowed by the configuration is: <em>getMaxSocketReaders</em>. You may want to alter your configuration.
<strong>Description</strong>
A non-native IO is being used, and the number of
socket reader threads is less than the number of active sockets.
Performance may degrade significantly. It is recommended that you
change the socket reader settings to improve the performance.
<strong>Cause</strong>
When non-native IO is used, IO is performed in a
blocking manner. Therefore, if the number of socket reader threads is
less than the number of active sockets, performance may degrade
significantly.
<strong>Action</strong>
Increase ThreadPoolPercentSocketReaders and/or
ThreadPoolSize, and/or decrease SocketReaderTimeoutMinMillis and/or
SocketReaderTimeoutMaxMillis.
This would imply what you understand. If you are seeing "Native IO Enabled" in your server log, but you are getting that warning, then I would first simply follow the advice in the message description that applies if you are using non-native IO. After experimenting with that (whether it works or not), I would file a support case asking first why you're getting that warning if you have native IO enabled, and whether following this advice for non-native IO should resolve your problem. -
Threads spawning in Servlets allowed?
Hello to all servlet afficionados,
I am following thread discussions already a while and there's always
discussion whether to user threads or not (I am not talking about a
servlet thread but about threads manually instantiated for other
reasons like asynchroneous handling of tasks for loose coupling or for
long running purposes).
So here's my question:
Is it definitely allowed to spawn threads from a servlet (and even put
the reference into the application context to allow others to later on
access that thread)?
Does the jdk specify on this?
Or Is it even specific for each servlet engine? What is BEA saying
about this?
And even further: Is this different on application servers that
include an ejb-container? Again, does the specification say something
about this? What does BEA say about this?
Thanks for trying to find out the real truth :-)
regards
Stefan
Stefan,
There is nothing in the Servlet spec that specifically prohibits the use
of threads within a servlet, but most will tell you that you should avoid it
if possible. Having said that, I've seen quite a few implimentations that
use background threads for tasks such as log queue processing and have not
really had any issues. To me it's a matter of degree. As long as your
architecture makes sense and you are careful to avoid potential deadlocks,
you should be ok. FWIW, BEA recommends not creating threads within a web
application.
-chris
"Stefan Hoehn" <[email protected]> wrote in message
news:[email protected]...
> Hello to all servlet afficionados,
>
> I am following thread discussions already a while and there's always
> discussion whether to user threads or not (I am not talking about a
> servlet thread but about threads manually instantiated for other
> reasons like asynchroneous handling of tasks for loose coupling or for
> long running purposes).
>
> So here's my question:
>
> Is it definitely allowed to spawn threads from a servlet (and even put
> the reference into the application context to allow others to later on
> access that thread)?
>
> Does the jdk specify on this?
>
> Or Is it even specific for each servlet engine? What is BEA saying
> about this?
>
> And even further: Is this different on application servers that
> include an ejb-container? Again, does the specification say something
> about this? What does BEA say about this?
>
>
> Thanks for trying to find out the real truth :-)
>
> regards
> Stefan
-
Starting new threads from a servlet
Hi
I read somewhere that starting new threads from a servlet is not supported in the future releases of J2EE .
Any one aware of this.
I have a listener program started as a new thread from the init method of a servlet which is set to load on startup.
I have to think about another way to do this if this is not suppported in the future releases
any commentsDr clap
Actually I was getting some warning messages in my websphere server console when I tried to make JDBC connections from the thread I opened in the servlet.
Everthing works fine but with the warning messages,
Then someone from IBM answered me in one of the forums quote
Cause: These messages are being produced due to an
"unsupported" environment. When application is
spinning its own threads from an EJB, spinning a
thread and accessing a database is not supported (per
the J2EE specification). One exception to this rule is
if a Servlet is spinning its own threads and accessing
a database. The J2EE specification is not clear on
this, so WebSphere Application Server 5.0 must allow
for it at this time. IBM is working with Sun to
clarify this in the specification, so eventually (i.e.
J2EE 1.4) spun threads from a Servlet accessing a
database outside of a transaction will not be
supported either.
Since we don't want to promote the usage of these
"naked" threads accessing databases, we are producing
the warning messages. Although they may be filling up
the logs, we feel that these are necessary to warn the
user that they are doing something not in-line with
the intended usage. Customer should consider changing
their application to comply with the
J2EE specification.
""" -
1. Is there any limit on maximum no of threads that would be created for a servlet by default? (I'm using Tomcat 3.2) If yes, what is the value of this limit?
2. How can I change this limit in Tomcat 3.2? Is there any difference in the configuration of this limit for Tomcat 3 and Tomcat 4?
3. If I increase the maximum no of threads, what OS level limitation would I need to take care of? Like default socket close timeout or what else ...
AnkitLook in the server.xml file. If you are running tomcat in standalone on port 8080 search the server.xml file for '8080'. Here you will find the configuration options for the connector. The maxProcessors and minProcessors setting set the number of threads that tomcat will use for requests on this connector.
For more information check the tomcat documentation. -
I am involved in building an application using using Oracle9i App server, IBM MQSeries message queueing and Enterprise Java beans (to name a few things).
The question I need to ask is:
Does the Oracle 9i AS manage threading of things like JavaBeans?
I need to find out if I can set a maximum number of threads so that even when thausands of messages are on the queue, it will only launch a certain number of threads to deal with them.
Thanks to anyone who can shed some light!I am involved in building an application using using Oracle9i App server, IBM MQSeries message queueing and Enterprise Java beans (to name a few things).
The question I need to ask is:
Does the Oracle 9i AS manage threading of things like JavaBeans?
I need to find out if I can set a maximum number of threads so that even when thausands of messages are on the queue, it will only launch a certain number of threads to deal with them.
Thanks to anyone who can shed some light! -
Hi,
I am using OC4J Server.
I know the session in servlet can be managed with cookies. In addition, the session can be managed in HTTP session of OC4J. And How about the URL rewriting? Does it mean there are 3 methods to keep track of the servlet session. What are the difference of them. I am quite confused.
Thanks in advanceI see. Thanks.
So it means there are two kinds of cookies, permanent cookies and session cookies.
session cookies are used to store the session information.
I have another question. I want to write a permanent cookies to the browser and get it again even if the browser is closed.
The source code is like:
Cookie cookie = new Cookie(cookieName, cookieValue);
response.addCookie(cookie);
Then I try to write some cookies in the browser, close the browser and open the browser again and I try to retreive the cookies using the following codes but the cookies disappear.
Cookie[] cookies = request.getCookies()
if (cookies != null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies;
Is this the correct way to store and retrieve permanent cookies?
thanks in advance -
Hi All,
I want to have a servlet or jsp page which has several buttons named 'Start'.
On clicking a 'Start' button, it should generate a thread (in another class) and run a process and the button will become 'stop'.
On clicking the stop button, the thread should stop. I can click any button at any time and start the thread and stop in at any time.
Can anyone tell me any technique for this problem?
Regards,
Pradhipi can't find why a nullPointerException occurs if i don,t see the code!
-
Hi all,
I am facing a big problem while doing a concurrent testing of my web application.
I am testing with 60 concurrent users.
I have set the database connection pool minimum capacity to 50, capacity increment
to 10 and maximum capacity to 100.
While there is no problem in getting database connection, the session variables
are getting mixed up (i.e. the session variable for one user is coming up as that
for another user!!!).
Does it have something to do with configuring some pool for servlets threads or
something?
Please help.
Thanks a lot in advance and best regards,
Jaya
Hi all,
I am facing a big problem while doing a concurrent testing of my web application.
I am testing with 60 concurrent users.
I have set the database connection pool minimum capacity to 50, capacity increment
to 10 and maximum capacity to 100.
While there is no problem in getting database connection, the session variables
are getting mixed up (i.e. the session variable for one user is coming up as that
for another user!!!).
Does it have something to do with configuring some pool for servlets threads or
something?
Please help.
Thanks a lot in advance and best regards,
Jaya
-
Where can we tell OC4J to load multiple threads of a servlet?
I configured a web site on OC4J that uses a servlet. Everything is working fine.
Now, I want to start, lets say, 50 instances of this servlet when the web site
is started by OC4J. How can I do this? Which configuration files should
I play with? Please note that my servlet is single-threaded and that I want
all threads running in the same JVM.
I want to do something similar than this parameter in JServ:
singleThreadModelServlet.initialCapacity=100
Thanksoc4j/orion is the fastest servlet engine on the planet. Those
guys in Sweden don't let you mess around with the number of
servlet instances available. The message here is that you do not
have to resort to any tricks that other servlet engines use to
make sure your servlet will be available to be served up...it
will.
The only thing that slows it down are lack of memory or lack of
resources (the database isn't replying fast enough). You can
solve these problems by clustering your servers on several
machines.
regards,
the elephantwalker
www.elephantwalker.com -
Single Threaded Model JSP- Servlet
Servelts can be set to have thread safe access (Single thread model). However, when a JSP is converted in to a servlet, is the servlet tagged as to use the single thread model?
As of JSP specification 1.2 the default value for the "isThreadSafe" attribute of the page directive is true indicating that the container can direct multiple requests to the page simultaneously. If this value is set to false then the requests will be sent one at a time.
Good Luck!
Eshwar Rao
Developer Technical Support
Sun microsystems inc
http://www.sun.com/developers/support -
Restricting Thread Pool for Servlet instances
I am using WebLogic5.1.0 version wherein I am registering a Servlet which
would be called by all the clients connecting
to my aplication. The servlet's service methods is taking some time to
execute. Since the servlet is following a SingleThreadModel, when there are
a large number of Clients connected, the number instances of Servlets
created are very high and the number of thread in the Application Server
process becomes very high affecting the performance of the other
applications.
Is it possible to restrict the number of instances of the servlets that get
created in the Application server ?
What are the setting required to achieve this ?
SandeepHi.
I'm not aware of a property setting that allows you to restrict the number of
servlet instances in WLS for the SingleThreadModel. The number of execute
threads is fixed - it does not grow depending on load. You can set this value
in the weblogic.properties file by setting weblogic.system.executeThreadCount.
This value is 15 by default.
It does sound like you are reaching the max capability of your
server/hardware. You should try tuning WLS.
Here are a couple of suggestions:
1. Try adjusting the executeThreadCount value to a higher value. Start by
setting it to 30 - see if that makes any difference.
2. Check out the following tuning guide:
http://www.weblogic.com/docs51/admindocs/tuning.html
3. Here is a list of admin properties that might aid in tuning:
http://www.weblogic.com/docs51/adminhelp/AdminPropertyHelp.html
4. If you are still having problems you might do better by posting this issue
to the performance newsgroup.
Regards,
Michael
Sandeep Rajpathak wrote:
I am using WebLogic5.1.0 version wherein I am registering a Servlet which
would be called by all the clients connecting
to my aplication. The servlet's service methods is taking some time to
execute. Since the servlet is following a SingleThreadModel, when there are
a large number of Clients connected, the number instances of Servlets
created are very high and the number of thread in the Application Server
process becomes very high affecting the performance of the other
applications.
Is it possible to restrict the number of instances of the servlets that get
created in the Application server ?
What are the setting required to achieve this ?
Sandeep--
Michael Young
Developer Relations Engineer
BEA Support -
A Thread manages a connection from the outside--help me to finish it
**RUN THIS CODE AND HELP ME--- THANKS A LOT**
EchoClient is thread which manage a connection from the outside.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class EchoClient extends Thread {
private ServerSocket listenSocket = null;
private Socket manageSocket = null;
private int[] port = new int[9999];
private BufferedReader in;
private PrintWriter out;
private int line;
private int count;
// No needed to mention
//private ClientNode[] clientArray = new ClientNode[9999];
private ManageClient[] manageClient;
private final int CONNECTED = 1;
private final int CONNECTING = 11;
private final int DISCONECTED = 2;
public void run() {
try {
manageClient = new ManageClient[9999];
listenSocket = new ServerSocket(903);
manageSocket = listenSocket.accept();
while (true) {
in = new BufferedReader(new InputStreamReader(manageSocket
.getInputStream()));
out = new PrintWriter(manageSocket.getOutputStream());
line = in.read();
// if Client send a variable(CONNECTING)
// Server will send to Client a variable(CONNECTED) and port
//to open a ChatFrame with that port
if (line == CONNECTING) {
System.out.print("have recieved");
out.print(CONNECTED);
//randomize a port to send to client
port[count] = (int) Math.ceil(Math.random() * 9999)
//creat a manageClient(Thread) to manage a seperate
// connection with a seperate Client
manageClient[count] = new ManageClient(port[count]);
manageClient[count].start();
out.print(port[count]);
count++;
} catch (Exception e) {
e.printStackTrace();
public static void main(String[] args) {
EchoClient e = new EchoClient();
e.start();
}And a Login Frame which will send to server a varialble (CONNECTING) which requires to connect and keep waiting for a variable to Open a ChatFrame with a new port
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Login extends JFrame {
public ChatFrame chatFrame;
private final int CONNECTED = 1;
private final int CONNECTING = 11;
* @param args
public Login() {
// TODO Auto-generated method stub
setSize(50, 150);
JButton loginButton = new JButton("Login");
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(loginButton);
add(p);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginServer();
public void loginServer() {
try {
Socket connectSocket = new Socket("127.0.0.1", 903);
while (true) {
BufferedReader in = new BufferedReader(new InputStreamReader(
connectSocket.getInputStream()));
PrintWriter out = new PrintWriter(connectSocket
.getOutputStream());
out.print(CONNECTING);
System.out.println("At here");
//(***position***)
int line = in.read();
System.out.println("At here1");
if (line == CONNECTED) {
int port = in.read();
chatFrame = new ChatFrame(port);
chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chatFrame.show();
connectSocket.close();
} catch (Exception exp) {
exp.printStackTrace();
public static void main(String[] args) {
Login login = new Login();
login.show();
login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}and this is ManageClient Thread...
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ManageClient extends Thread {
private ServerSocket ssClient;
private Socket sClient;
private int port;
public ManageClient(int port) {
this.port = port;
public int getPort() {
return port;
public void run() {
try {
ssClient = new ServerSocket(getPort());
sClient = ssClient.accept();
while (true) {
BufferedReader in = new BufferedReader(new InputStreamReader(
sClient.getInputStream()));
PrintWriter out = new PrintWriter(sClient.getOutputStream());
String s = in.readLine();
if (s != null)
out.print("have recieved");
} catch (Exception e) {
e.printStackTrace();
}my problem...
At firts EchoClient will run.. and then Login but when I click to the Login button it has just only did before int line = in.readLine();(*** postion ***)
I don't know why it doesn't continue. It stops here and the login button is still visible(cause code has not finish)..
That's my problem...
Somebody help me
Edited by: rockfanskid on Oct 17, 2007 4:25 AMSomebody helps me to finish this project...
thanks for racing this thread
Maybe you are looking for
-
Error in the assistant of installation of Oracle 9ias 9.0.2 single sing-on
Install oracle 9ias 9.0.2.0, but when this in the assisting existing Oracle 9ias single singn-on the error that the host or port are not been worth appears. in the host sijita.uptc.edu.co places and in the port 7777. That it can be the error??? one d
-
My iPod shuffle 4th generation keeps having to be restored.
When I try to play songs, the iPod keeps deleting the one I'm playing. I'd REALLY love some help with this, as I LOVE iPods and have never had this problem before. It seems like I'm not the only one, but there's very few of us... I can look into Cust
-
Foreign Exchange fluctuation rate is not showing in case of partial payment
Hi, In case of foreign exchange collection from Customer through F-28 in partial payment "Exchange Rate Gain" or "Exchange Rate Loss" is not appearing in GL balance simulation or Customer balance report. Only in case of full payment from Customer the
-
Employee photo is causing one of our crystal reports to crash
Good Afternoon, I'm currently using Crystal report engine version 9.2.3.884 as part of a Peoplesoft package. I have created a new report using the PeopleSoft crystal ODBC driver delivered within the package. One of the field retrieved contains the em
-
Hi I am editing in a ProRes LT 422 time line using ProRes LT material 1920 x 1080p. This is on a Mac pro Quad-Core Xeon with 6GB DDR3 and a 4TB esata stripped raid, using OS x10.6.4 and Final Cut 7. I have been suffering from intermittent playback is