Spring stopwatch concurrency

I have a Bean as shown below:

    @Bean(name = "myStopWatch")
    @Scope(value = "prototype")
    public MyStopWatch myStopWatch() {
       return new MyStopWatch();
    }

And the MyStopWatch class looks like this:

import org.springframework.util.StopWatch;

public class MyStopWatch {

   private StopWatch sw = new StopWatch();

   public void start() {
       if(!sw.isRunning()) {
           sw.start();
       }
   }

   public void stop() {
       if(sw.isRunning()) {
           sw.stop();
       }
   }
}

We use this Bean in a highly competitive environment. If my understanding is correct, the MyStopWatch class should never be shared with a stream, right?

However, we sometimes (very rarely) get the following error:

java.lang.IllegalStateException: Cannot start StopWatch: it is already running on org.springframework.util.StopWatch.start (StopWatch.java:127) in org.springframework.util.StopWatch.start (StopWatch.java:116)

So far, we have not been able to reproduce this behavior using our test. I am looking for additional information on how to correctly define the sw (or ma bean) variable in order to avoid this error.

+4
source share
2

. bean prototype . . .

Spring StopWatch, , .

+1

-, , . beans prototype , , , bean - ( getBean @Autowired).

( , - ), StopWatch Spring. guava, StopWatch - ( ), System.nanoTime , .

0

Source: https://habr.com/ru/post/1690845/


All Articles