Friday 30 August 2013

Timeout the thread if any bundle is taking lot of time

Timeout the thread if any bundle is taking lot of time

I am working on a project in which I will have different Bundles. Let's
take an example, Suppose I have 5 bundles and each of those bundles will
have a method name process.
Now currently, I am calling the process method of all those 5 bundles
sequentially, one by one and then I am writing to the database. But that's
what I don't want.
I need to call all those 5 Bundles process method in parallel using
multithread and then write to the database.
And I also want to have some timeout feature for those threads. I will be
having a default timeout settings for all the threads for the bundles. If
any bundle is taking some higher time than the timeout settings I have,
then I want to timeout those threads and then log back saying this bundle
got timeout bcoz it was taking lot of time.
I hope question is clear enough...
Below is the code I have so far which is calling process method
sequentially one by one.
public void processEvents(final Map<String, Object> eventData) {
final Map<String, String> outputs = (Map<String,
String>)eventData.get(BConstants.EVENT_HOLDER);
for (final BundleRegistration.BundlesHolderEntry entry :
BundleRegistration.getInstance()) {
final Map<String, String> response =
entry.getPlugin().process(outputs);
// write to the database.
System.out.println(response);
}
}
I am not sure what is the best and efficient way to do this? Because, in
future, it might be possible that I will have more than 5 bundles.
Can anyone provide me an example of how can I achieve this? Any help will
be appreciated on this. Thanks.

No comments:

Post a Comment