PDA

View Full Version : java.rmi.server.ExportException: object already exported



cressie176
Nov 20th, 2005, 06:33 PM
Hi,

I've seen this issue reported in a couple of places now, but no solutions.

I'm trying to expose a service over RMI using Spring, but when the ApplicationContext is being loaded keep getting an ExportException: object already exported.

The relevant bits of the application-context.xml are

<bean id="loginService" class="com.myapp.service.impl.LoginService" singleton="true" lazy-init="false">

<constructor-arg>

<bean id="persistenceManager" class="com.myapp.persistence.hibernate.PersistenceManager" singleton="true" >

<constructor-arg><ref bean="hibernateSessionFactory"/></constructor-arg>
</bean>
</constructor-arg>
<constructor-arg value="3" /> <!-- Maximum Failed Login Attempts -->
<constructor-arg value="900" /> <!-- Seconds Before Failure Count Reset -->
</bean>

<bean class="org.springframework.remoting.rmi.RmiServiceExporte r">

<property name="serviceName" value="loginService"/>
<property name="service" ref="loginService"/>
<property name="serviceInterface" value="com.myapp.service.ILoginService"/>
<property name="registryPort" value="1199"/>
</bean>

The ILoginService interface looks like

package com.myapp.service;

import java.rmi.Remote;
import java.rmi.RemoteException;

import com.myapp.domainmodel.User;

public interface ILoginService extends Remote
{

public User login(String username, String password) throws RemoteException;
}

I'm not actually able to get as far as looking up the RMI object as as soon as I try and load the context (in the setUp method of my test case), Spring throws the exception. Any ideas?

public void setUp() throws Exception
{

// LoginServiceClientTest line 22
applicationContext = new FileSystemXmlApplicationContext(APPLICATION_CONTEX T_PATH);
}

org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.remoting.rmi.RmiServiceExport er' defined in file [C:\Projects\MyApp\workspace\MyAppTest\env\spring\a pplication-context.xml]: Initialization of bean failed; nested exception is java.rmi.server.ExportException: object already exported
java.rmi.server.ExportException: object already exported
at sun.rmi.transport.ObjectTable.putTarget(Unknown Source)
at sun.rmi.transport.Transport.exportObject(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.exportObject(Un known Source)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(Unk nown Source)
at sun.rmi.transport.LiveRef.exportObject(Unknown Source)
at sun.rmi.server.UnicastServerRef.exportObject(Unkno wn Source)
at java.rmi.server.UnicastRemoteObject.exportObject(U nknown Source)
at java.rmi.server.UnicastRemoteObject.exportObject(U nknown Source)
at org.springframework.remoting.rmi.RmiServiceExporte r.afterPropertiesSet(RmiServiceExporter.java:244)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1059)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:363)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:226)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:275)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:320)
at org.springframework.context.support.FileSystemXmlA pplicationContext.<init>(FileSystemXmlApplicationContext.java:89)
at org.springframework.context.support.FileSystemXmlA pplicationContext.<init>(FileSystemXmlApplicationContext.java:74)
at org.springframework.context.support.FileSystemXmlA pplicationContext.<init>(FileSystemXmlApplicationContext.java:65)
at com.myapp.service.spring.LoginServiceClientTest.se tUp(LoginServiceClientTest.java:22)
at junit.framework.TestCase.runBare(TestCase.java:125 )
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)

cressie176
Nov 21st, 2005, 02:20 AM
Just to get round this problem for now I'm creating my own RmiServiceExporter class and explicitly unexporting every object before it is exported using the following

/**
* Register the service as RMI object.
* Creates an RMI registry on the specified port if none exists.
*/
public void afterPropertiesSet() throws RemoteException {

checkService();

// ... lots of code not shown above here

// Initialize and cache exported object.
this.exportedObject = getObjectToExport();

// Export remote object and bind it to RMI registry.
if (logger.isInfoEnabled()) {

logger.info("Binding RMI service '" + this.serviceName +
"' to registry at port '" + this.registryPort + "'");
}

// Workaround to stop ExportException: object already exported
if (exportedObject != null) {

UnicastRemoteObject.unexportObject(this.exportedOb ject, true);
}

if (this.clientSocketFactory != null) {

UnicastRemoteObject.exportObject(
this.exportedObject, this.servicePort, this.clientSocketFactory, this.serverSocketFactory);
}
else {

UnicastRemoteObject.exportObject(this.exportedObje ct, this.servicePort);
}
this.registry.rebind(this.serviceName, this.exportedObject);
}