Announcement Announcement Module
Collapse
No announcement yet.
Use prototype scope for Endpoints? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use prototype scope for Endpoints?

    Is it possible to make my ServiceActivator Endpoints be prototype scoped? Meaning for each message a new instance of my Endpoint is created?

    I tried combining @Scope("prototype") and @MessageEndpoint and it seems that the same instance of the endpoint is used.

    Thanks for any tips.

    -Adam

  • #2
    The service that is referenced could have prototype scope, but even in that case, it will only create the prototype instance once when first injecting the service into the handler adapter. It would still not be created per-Message. That sounds more like a custom scope requirement (similar to how Spring Batch provides "step" scope). Can you provide just a brief description of the use-case that requires the service to manage state on a per-Message basis?

    Thanks,
    Mark

    Comment


    • #3
      The main reason we want to have prototype scope Endpoints is because many of our other service objects (that need to be invoked by the Endpoints) are prototypes with state. These other services are used by the MVC layer and other threads, so there could be multiple threads operating on these services at the same time, if they are singletons.

      I realized the problem when we @Autowired in a prototype scope bean into an Endpoint - then we started seeing bad behavior in our application because the prototype service was shared across messages.

      Even if we implemented a custom scope, would that ensure the Spring-Integration code that holds a reference to the Endpoint honors that scope? Should I be proxying these prototypes and doing something fancy that makes it look like a singleton but really is a custom scope? I haven't ever implemented a custom scope in Spring.

      Does that make sense? Or is there a cleaner way for my endpoints to interact with non-singleton Services?

      Thanks for your help, Mark.

      Comment


      • #4
        Using "scoped-proxy" on a prototype service that is "ref"-ed from the service-activator should do the trick. In fact, a quick test with the following configuration works:
        Code:
        <channel id="channel"/>
        
        <service-activator input-channel="channel" ref="service"/>
        	
        <beans:bean id="service" class="demo.scope.Service" scope="prototype">
            <aop:scoped-proxy/>
        </beans:bean>
        Now, since you are using annotations, it would obviously be a bit different. The 'component-scan' element does accept a 'scoped-proxy' flag. Maybe you can try to prototype that (no pun intended) and let me know. Otherwise, when I get a chance I'll try that also. The part that is different than a "normal" annotated bean is that the individual method annotations are actually picked up by a BeanPostProcessor.

        -Mark

        Comment


        • #5
          Hey Mark,

          I'm working with Adam on this problem. I prototyped the code using the "scoped-proxy" flag on our component-scan tag. Unfortunately, the scopedProxy never gets created. I'm pretty sure it is because of line 299 of ClassPathBeanDefinitionScanner. That is inside the "applyScope" method which skips proxy creation if the scope is "prototype" or "singleton". At first glance it seems we might need to create a custom scope.

          thanks again
          sam

          Comment

          Working...
          X