How to do many simultaneous jsoup sessions (Spring boot project and concurrancy)

361 views Asked by At

Maybe this is a very basic Java question. Forgive me if it is.

I have this Spring Boot project that needs to, for every registered user, automatically (in the background) connect periodically to a website and download many documents and/or check for changes.

The "download/check" system was externally developed. Every method was static.

Am I right thinking that it IS a problem when I want to have more than one simultaneous execution? Even if it has all specific connection parameters through parameters?

Thinking of that, I removed the static annotation on almost every method. Now I need to create a new instance for every execution.

When I began adding it to my Spring boot project, I realized there are a bunch of services that make simultaneous connections to a web service, and there I didn't even care about concurrency.

AFAIK, each service is a singleton. Right? So there is no new instance for every connection. Right?

My question is: If I happen to have 100's of users... How should I integrate the jsoup code?

  1. Should I create a new instance from a service?
  2. Should I convert it to a Service itself and just @Autowire it to the service where the process is triggered?
  3. Is it better to make it static? (this one I don't think so, but maybe I'm wrong)
  4. Any other option?

If possible, please add a reason why I should follow your suggestion.

Thank you all for your help.

1

There are 1 answers

4
daniel.eichten On

First of all I'd say if your classes are not maintaining any state then it is actually a good thing if everything is static cause this gets you rather close to functional programming. If you are then passing everything as an parameter to a method and don't have any state in the class (not the method) then everything is kept on the stack which is not shared amongst threads. Long story short: thread-safe.

If you maintain any type of state in your classes – be it services or components – then you have to make sure that they are executing in a thread safe way. One of the easiest ways is to change the @Scope of that particular bean to prototype or for instance request as you are running this as a web app.