0

I am using the spring-mvc with hibernate using tomcat . I am new in this field. i have one query. Earlier i was working on core java , where i learned many concurrent data structures (java.util.concurrent). I can spawn threads to deligate task, and can create my won manager worker model. My question is , can i use those concurrent package to create new threads to delegate tasks, can i create my own manager-worker model. I read, that in spring, its not advisable to spawn new thread, so how to achieve concurrency?

user3265739
  • 55
  • 1
  • 10
  • 3
    Tomcat handles the concurrency for you. Each HTTP request is handled in a thread, concurrently with other threads handling other HTTP requests. – JB Nizet Feb 17 '14 at 16:57
  • Suppose for a single request , i need to fetch data via hibernate and with that i need to create the report. Data is huge, so can i devide the task among multiple worker and one extra thread will be writing to a file whatever data we fetch, or i cant create such model in spring? What i mean , can i spawn new thread in the service to execute some task synchronously and asynchronously? – user3265739 Feb 17 '14 at 17:02
  • you can create such model. The questions boils down to what are you going to do with Http Request ? do you want to return the response once you started your worker / do you want to wait to resposne ? – Mani Feb 17 '14 at 17:08
  • See problem is i need to put data in xls format , after fetching data from DB, so currently i am using single thread to do this. Its very time taking. What i think , i need to distribute the task among workers , and then finally i need to send the xls sheet to client. with single thread model , its very time consuming. – user3265739 Feb 17 '14 at 17:13
  • It is possible to spawn multiple thread from your service and delegate the task to workers. Once the worker completed the task you can return the control from service to controller. From controller point of view it is single request. but i am wondering which util/framework you are using to write/build one xls stream from multiple threads. is it csv formated xls ? – Mani Feb 17 '14 at 17:17
  • No , actually , i still searching for such util to write data concurrently in xls sheet.Currently i am using HSSFWorkBook . But to fetch data from DB , i want to follow worker model as my data fetching from DB is nested i.e after getting result from one table, i need to search in other table with that result and so on for 5 times. And i need to do this operation for 5000 times... so its going to be 5K* O(n^5) complexity – user3265739 Feb 17 '14 at 17:34
  • If your algorithm is requiring something that's n^5, it's going to be slow no matter how you parallelize it; depending on what your query means, it may make more sense to use a different data structure partially or entirely. Specifically, "friend-of-a-friend" queries are pathological for relational data models but naturally (and efficiently) expressed as graph queries. – chrylis -cautiouslyoptimistic- Feb 17 '14 at 17:53
  • To answer your question, Most of the case Spring used to create beans in specified scope apart from that you have full freedom to use language features. So it is possible to have the model like you except, using concurrent package. As far as my knowledge i dont know the framework which allow to create excel from multiple threads. For Db access yes you can. but i would suggest to write some join to avoid multiple iteration for an single record. some time it would also good to go Store procedure (if you db supports) to return. which will solve lot of lot of time. – Mani Feb 17 '14 at 17:59

1 Answers1

0

You usually use stateless beans in Spring MVC, so concurrency shouldn't be an issue.

See here:

spring mvc declaring all beans singleton

Community
  • 1
  • 1
cbach
  • 394
  • 3
  • 8