Need Help in Threading Design
We need to create a web application that needs to work with various companies. We would like to work with these companies simultaneously so that we can reduce the time taken.
The situation would be the following: A client requests something then we would go to the various companies and make the request on their behalf and then show the customer the results from these companies.
Problems that need to be addressed:
1. Threading will provide the solution however since many clients will be accessing this feature we want to ensure that we do not run out of resources and have memory issues.
2. In addition at times we have noticed that the companies have hiccups and sometimes take an enormous amount of time to return back to us and so we would like to set a time limit by wanting to wait a maximum of 30 seconds before returning back to the customer and saying only these companies returned back. We would hate to wait forever because of some issue at one of the companies.
Has anyone worked with this scenario and how do we approach it and what should we use? Examples or tutorials would be a great advantage.
So far we have realized that we should use the java.util.concurrent package but not sure what exactly to use such as ThreadPoolExecutor, etc. and why one would be better or why would be the obvious choice. I have read several documents but can't seem to pinpoint on what exactly to use, etc.
Thank you for any input that you provide.
Message was edited by:
dawuad
[1544 byte] By [
dawuada] at [2007-11-27 5:56:21]

# 1
The server creates queues (that represent the companies) and a thread pool to process each queue.
When the server gets a request (doWork() method) the server places the request into each of the queues and waits up to n milliseconds for all the threads to finish.
When all the threads finish, the server concatenates the response from the threads into an Object[] and passes it back to the client.
If there is a time-out, (i.e. n-milliseconds elapses and not all threads finish), the server concatenates the response from the threads that finished into an Object[] and passes it back to the client with a notification that there was a time-out.
All the client has to do is:
Server server = new Server();
Object[] back = server.doWork(request);
Now, this should work for local requests as well as remote (RMI).
Is that what you抮e looking for? Wouldn抰 it be great if it were already written?
# 2
Thank you for responding. And yes, it would be great if it were already written! We were wondering what classes within the concurrency package would be best appropriate for our situation. We know about the ThreadPoolExecutor however we were having difficulties controlling the time issue where if the time has expired then it should kill the other threads and then continue afterwards. Is it possible to not constantly poll every second or so to determine if the time has expired and the threads have not completed yet. I feel that that is consuming more resources. Which class would be most appropriate for us?
# 3
summer project?
a list of some technologies to explore
client request to webapp
jsp -> servlet (should handle 1)
webapp request to various companies
threaded requests via http or webservice or jms or query, storing results in db
(keep it simple e.g.
for( final Company c : companies ) {
new Thread(
new Runnable() {
public void run() {
// access a company
// store results in db
}
} ).start();
}
webapp asynchronous response to client
ajax -> javascript
# 4
You can create a collection containing java.util.concurrent.Callable<V> instances, each instance representing a task, and then call java.util.concurrent.ExecutorService.invokeAll(Collection<Callable><T>>, long, TimeUnit) for executing the tasks.
Here you can set the desired timeout and you don't need to constantly poll the queue.
# 5
For an open source project that does what you want, see here: http://coopsoft.com/JavaProduct.html
# 6
cooper6 is right. What you need is a Thread pool and tasks Queues.
You need a super class for the task and then you have to specialize it. For example
class Task{
public void doSomethin()
}
Your queue must be thread safe and each of your threads in your pool consume task by executing the (possible overrided ) method doSomething
I also recommend you to use java NIO; is the only way to have asynchronous socket in java, and the I/O operation are faster with buffer
# 7
yes, if you need a commercial-grade app, a threadpool is the way to go for asynchronous requests to the other companies, but most of the client io and concurrency can be handled by a servlet container and a well-written servlet...many commercial-grade servlet containers are available for free, Tomcat comes to mind
as for the nio points, you did say webapp, right? only a non-standard webapp should need to create its own io handlers
nio and thread-safety points are valid, but io is optimized and abstracted away by a servlet container, concurrency is a gimme if you avoid a couple of pitfalls
if the companies to access have standard interfaces you could be done with the assignment in a week
# 8
Thank you to each one of you and for your input. I will try to evaluate each of your comments and see what best fits us. I will post our solution once we have it established. Thank you again.
# 9
sorry, I confuse the question. developer_jbs is right if you want to make a Web application; you don't need to care about thread problem, instead of that, you should use a web container and write JSP and Servlets