Skip to content

Commit 6ca253e

Browse files
committed
RmiServiceExporter and RmiRegistryFactoryBean synchronize registry lookup/creation
1 parent be61846 commit 6ca253e

File tree

2 files changed

+55
-47
lines changed

2 files changed

+55
-47
lines changed

org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiRegistryFactoryBean.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,20 @@ protected Registry getRegistry(
215215
if (logger.isInfoEnabled()) {
216216
logger.info("Looking for RMI registry at port '" + registryPort + "', using custom socket factory");
217217
}
218-
try {
219-
// Retrieve existing registry.
220-
Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory);
221-
testRegistry(reg);
222-
return reg;
223-
}
224-
catch (RemoteException ex) {
225-
logger.debug("RMI registry access threw exception", ex);
226-
logger.info("Could not detect RMI registry - creating new one");
227-
// Assume no registry found -> create new one.
228-
this.created = true;
229-
return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory);
218+
synchronized (LocateRegistry.class) {
219+
try {
220+
// Retrieve existing registry.
221+
Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory);
222+
testRegistry(reg);
223+
return reg;
224+
}
225+
catch (RemoteException ex) {
226+
logger.debug("RMI registry access threw exception", ex);
227+
logger.info("Could not detect RMI registry - creating new one");
228+
// Assume no registry found -> create new one.
229+
this.created = true;
230+
return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory);
231+
}
230232
}
231233
}
232234

@@ -250,18 +252,20 @@ protected Registry getRegistry(int registryPort) throws RemoteException {
250252
if (logger.isInfoEnabled()) {
251253
logger.info("Looking for RMI registry at port '" + registryPort + "'");
252254
}
253-
try {
254-
// Retrieve existing registry.
255-
Registry reg = LocateRegistry.getRegistry(registryPort);
256-
testRegistry(reg);
257-
return reg;
258-
}
259-
catch (RemoteException ex) {
260-
logger.debug("RMI registry access threw exception", ex);
261-
logger.info("Could not detect RMI registry - creating new one");
262-
// Assume no registry found -> create new one.
263-
this.created = true;
264-
return LocateRegistry.createRegistry(registryPort);
255+
synchronized (LocateRegistry.class) {
256+
try {
257+
// Retrieve existing registry.
258+
Registry reg = LocateRegistry.getRegistry(registryPort);
259+
testRegistry(reg);
260+
return reg;
261+
}
262+
catch (RemoteException ex) {
263+
logger.debug("RMI registry access threw exception", ex);
264+
logger.info("Could not detect RMI registry - creating new one");
265+
// Assume no registry found -> create new one.
266+
this.created = true;
267+
return LocateRegistry.createRegistry(registryPort);
268+
}
265269
}
266270
}
267271

org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiServiceExporter.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -352,17 +352,19 @@ protected Registry getRegistry(
352352
if (logger.isInfoEnabled()) {
353353
logger.info("Looking for RMI registry at port '" + registryPort + "', using custom socket factory");
354354
}
355-
try {
356-
// Retrieve existing registry.
357-
Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory);
358-
testRegistry(reg);
359-
return reg;
360-
}
361-
catch (RemoteException ex) {
362-
logger.debug("RMI registry access threw exception", ex);
363-
logger.info("Could not detect RMI registry - creating new one");
364-
// Assume no registry found -> create new one.
365-
return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory);
355+
synchronized (LocateRegistry.class) {
356+
try {
357+
// Retrieve existing registry.
358+
Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory);
359+
testRegistry(reg);
360+
return reg;
361+
}
362+
catch (RemoteException ex) {
363+
logger.debug("RMI registry access threw exception", ex);
364+
logger.info("Could not detect RMI registry - creating new one");
365+
// Assume no registry found -> create new one.
366+
return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory);
367+
}
366368
}
367369
}
368370

@@ -385,17 +387,19 @@ protected Registry getRegistry(int registryPort) throws RemoteException {
385387
if (logger.isInfoEnabled()) {
386388
logger.info("Looking for RMI registry at port '" + registryPort + "'");
387389
}
388-
try {
389-
// Retrieve existing registry.
390-
Registry reg = LocateRegistry.getRegistry(registryPort);
391-
testRegistry(reg);
392-
return reg;
393-
}
394-
catch (RemoteException ex) {
395-
logger.debug("RMI registry access threw exception", ex);
396-
logger.info("Could not detect RMI registry - creating new one");
397-
// Assume no registry found -> create new one.
398-
return LocateRegistry.createRegistry(registryPort);
390+
synchronized (LocateRegistry.class) {
391+
try {
392+
// Retrieve existing registry.
393+
Registry reg = LocateRegistry.getRegistry(registryPort);
394+
testRegistry(reg);
395+
return reg;
396+
}
397+
catch (RemoteException ex) {
398+
logger.debug("RMI registry access threw exception", ex);
399+
logger.info("Could not detect RMI registry - creating new one");
400+
// Assume no registry found -> create new one.
401+
return LocateRegistry.createRegistry(registryPort);
402+
}
399403
}
400404
}
401405

0 commit comments

Comments
 (0)