0

My java web service performs lots of mathematical operations and finally finds a result to return. Before return the result I want to insert some data to DB about mathematical operations. You think like that I want to log something.

But I have very busy server where the database is located. So logging takes too much time. But in fact math operations doesn't take too much time. And my webservices always wait for finishing logging to DB.

My web services pseudocode is like that :

WebService {  

math operations..     
math operations..    
math operations..  

    insertion to db  --> taking to much time

   return result 
}

So I want to instert logs to DB asnychoronously. Can you give me advice for that? It can be library or framework which perform multithreading and asnyc queue inside.

PS: I dont want to use Log4j and nested webservices. And I don't want to handle threads manually.

neverwinter
  • 810
  • 2
  • 15
  • 42

1 Answers1

0

Create a thread with Executors.newSingleThreadExecutor() (it does not count as manual thread management). And instead of writing

dao.persist(item);

write

executor.submit(new Runnable() {
    @Override public void run() { dao.persist(item); }
});
bobah
  • 18,364
  • 2
  • 37
  • 70
  • I think it counts as manual thread management. I know this way. Can you give me another advice ? – neverwinter Dec 03 '13 at 11:20
  • It won't be an advice you are after, but here it is: if you can implement what you want reliably in couple of lines of own code do just that and do not introduce third party dependencies to your app. – bobah Dec 03 '13 at 15:31