Results 1 to 7 of 7

Thread: Session-scoped RMI Proxy

  1. #1
    Join Date
    Oct 2007
    Posts
    6

    Default 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. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

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

    Joerg
    This post can contain insufficient information.

  3. #3
    Join Date
    Oct 2007
    Posts
    6

    Default

    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.

  4. #4
    Join Date
    Oct 2007
    Posts
    6

    Default 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?

  5. #5
    Join Date
    Oct 2007
    Posts
    6

    Default

    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 at 05:56 PM.

  6. #6
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote 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".

    Quote 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
    This post can contain insufficient information.

  7. #7
    Join Date
    Oct 2007
    Posts
    6

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •