Announcement Announcement Module
No announcement yet.
Threading issues with scope="prototype"? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Threading issues with scope="prototype"?


    I have some issues regarding the use of beans with scope=prototype. I boiled my problem done to a minimal scenario.

    There are two exporter (OSGi) bundles (exporter1 and exporter2) exporting classes called Consumer1 and Consumer2, which are identically except for their names. To avoid information overflow I inserted just the "1"-version.
      _at_Configurable(preConstruction = true)
      public class Consumer1 {
        String text;
        ... //Constructor, setters/getters,...
      <bean class="exporter1.Consumer1" scope="prototype">
        <property name="text" value="text1"></property>
    These Consumer classes are used in a third bundle:
        //Some code ensuring that all application contexts are created
        log.debug("creating objects...");
        Consumer1 c1 = new Consumer1();
        Consumer2 c2 = new Consumer2();
        System.out.println(c1.getText() != null); // Test wether the Property has been set
        System.out.println(c2.getText() != null); // Test wether the Property has been set
    I launch all this via eclipse. The sysouts of different launches alternate between all possible combinations true/true, true/false,...

    I made some interesting observations:
    1) The application contexts has always been initialized. The log file does not show any entries from different threads after starting the creation of objects. BTW I set the log level to ALL...
    2) Putting additional delays between the creation of the objects can sometimes stabilize the output for some consecutive launches.
    3) Instead of relying on my code waiting for application context creation I also waited "for a very long time", which had the same effects.
    4) The log output in case of a successfull initialization of an object looks as follows while not showing any entries when not setting the text.
    TRACE [Thread-4                      ] org.springframework.beans.CachedIntrospectionResults          - Getting BeanInfo for class [exporter2.Consumer2]
    TRACE [Thread-4                      ] org.springframework.beans.CachedIntrospectionResults          - Caching PropertyDescriptors for class [exporter2.Consumer2]
    TRACE [Thread-4                      ] org.springframework.beans.CachedIntrospectionResults          - Found bean property 'text' of type [java.lang.String]
    DEBUG [Thread-4                      ] org.springframework.beans.CachedIntrospectionResults          - Not strongly caching class [exporter2.Consumer2] because it is not cache-safe
    I am not sure what is going on here.
    a) Am I completely abusing some constructs? Maybe new() should not be used in a different bundle?
    b) Is there a general timing/synchronization issue?
    c) ???

    Can anybody help?

    Best regards, Jens