Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load spring context using multiple threads

I have a large application context consisting of many context files, using autowiring and package scans, starting up web services, establishing connections to databases and and external legacy system etc. I have been thinking on how to improve context loading times since it takes a while without really taking up CPU. Is there a way to tell application context to initialize using multiple threads? In theory it should be possible since we have dependencies already defined. I'd like the resources (db, web services and legacy connections) to be initialized in parallel.

like image 345
anydoby Avatar asked Oct 19 '22 18:10

anydoby


1 Answers

There's one option that comes in my mind and I'm not sure if it will work, as I've never tried doing this (in my view, if an app takes too long to start up is a sign that it has to be broken down in smaller components, where each component is an app on its own right).

The solution that I think might work is to have a hierarchy of context files, so you can instantiate the parent application context and then instantiate each one of the child context concurrently. The problem with this approach is that you cannot have dependencies between child contexts, but you can have indirect ones (e.g. The parent context has an event dispatcher then classes in one context listen to events triggered from the parent context, and another child context triggers events on the parent context).

like image 176
Augusto Avatar answered Oct 22 '22 21:10

Augusto