Announcement Announcement Module
Collapse
No announcement yet.
Session-scoped RMI Proxy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Session-scoped RMI Proxy

    I want each session to use a different server for RMI calls. My reasons are a little unorthodox, but you can imagine the case of somebody wanting to do load-balancing (badly).

    The following configuration shows my intent, but of course you can't scope an RmiProxyFactoryBean this way. Can anyone help me achieve what I want to do?

    Code:
        <bean id="myService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" scope="session">
            <aop:scoped-proxy />
            <property name="serviceUrl">
                <bean class="com.company.MyServiceSessionURL" scope="session" />
            </property>
            <property name="serviceInterface" value="com.company.MyServiceInterface/>
            <property name="lookupStubOnStartup" value="false" />
            <property name="refreshStubOnConnectFailure" value="true" />
        </bean>
    MyServiceSessionURL determines the appropriate server (and thus serviceUrl) for the session.

  • #2
    I wonder if it works to just make the serviceUrl a session-scoped bean proxy.

    Joerg

    Comment


    • #3
      I'll try, but I don't think that will work. The documentation explicitly warns against hooking scoped beans into singletons without an <aop:scoped-proxy>, because otherwise the scoped bean is instantiated exactly once.

      Comment


      • #4
        serviceUrl requires a String

        I can't try it, because serviceUrl requires a String, and MyServiceSessionURL isn't a String. Is there a way to assign a conversion strategy that simply calls toString() on the MyServiceSessionURL object? Or a way to use a non-static field?

        Comment


        • #5
          Follow-up -- Success:
          I was able to get a session-scoped RMI proxy with a little finagling.

          Code:
              <bean id="myService" class="com.company.MyServiceDelegator" scope="session">
                  <aop:scoped-proxy />
                  <property name="remoteMyService">
                      <bean class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
                          <property name="serviceUrl">
                             [...]*
                          </property>
                          <property name="serviceInterface" value="com.company.MyService"/>
                          <property name="lookupStubOnStartup" value="false" />
                          <property name="refreshStubOnConnectFailure" value="true" />
                      </bean>
                  </property>
              </bean>
          Code:
          public class MyServiceDelegator implements MyService {
          
              private MyService remoteMyService;
          
              public MyService getRemoteMyService() {
                  return remoteMyService;
              }
          
              public void setRemoteMyService(final MyService remoteMyService) {
                  this.remoteMyService= remoteMyService;
              }
          
              public void serviceCall(...) {
                  remoteMyService.serviceCall(...);
              }
          }
          Now, I can transparently inject myService into any bean that needs to use the MyService interface.


          * The [...] represents an unsolved problem. In actuality, I subclassed RmiProxyFactoryBean to override the setServiceUrl method, but that's pretty disgusting and shouldn't be necessary.
          Last edited by Roxton; Oct 30th, 2007, 05:56 PM.

          Comment


          • #6
            Originally posted by Roxton View Post
            The documentation explicitly warns against hooking scoped beans into singletons without an <aop:scoped-proxy>, because otherwise the scoped bean is instantiated exactly once.
            Yes, I wrote "session-scoped bean proxy".

            Originally posted by Roxton View Post
            Or a way to use a non-static field?
            Yes, you can use a so-called instance factory method:

            Code:
            <bean id="myServiceSessionURL" class="com.company.MyServiceSessionURL"/>
            
            <!-- injectable string -->
            <bean id="sessionUrl" factory-bean="myServiceSessionURL"
                                  factory-method="toString"/>
            Both are also session-scopable.

            Joerg

            Comment


            • #7
              Thanks a lot, Jörg. The factory-bean approach worked perfectly.

              As for the other thing, it seems that the RmiProxyFactoryBean wouldn't know that the sessionUrl was a scoped proxy, and would call it exactly once to create a singleton. Scoped proxies are supposed to be transparent to the caller. The Delegator seems to do the trick, though.

              Comment

              Working...
              X