This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit a4acca0cc29af671df2bd576a07780acdb0f30f3 Author: Thomas Vandahl <t...@apache.org> AuthorDate: Fri Feb 5 16:43:35 2021 +0100 Remove duplicate code --- .../remote/server/RegistryKeepAliveRunner.java | 45 +++------------------- .../jcs3/utils/discovery/DiscoveredService.java | 21 ++++++++++ .../jcs3/utils/discovery/UDPCleanupRunner.java | 30 +-------------- .../jcs3/utils/discovery/UDPDiscoveryReceiver.java | 6 +-- .../jcs3/utils/discovery/UDPDiscoveryService.java | 38 ++++++------------ 5 files changed, 40 insertions(+), 100 deletions(-) diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RegistryKeepAliveRunner.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RegistryKeepAliveRunner.java index f1687b7..ec369a6 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RegistryKeepAliveRunner.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RegistryKeepAliveRunner.java @@ -19,7 +19,6 @@ package org.apache.commons.jcs3.auxiliary.remote.server; * under the License. */ -import java.rmi.Naming; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.registry.Registry; @@ -41,11 +40,8 @@ public class RegistryKeepAliveRunner /** The logger */ private static final Log log = LogManager.getLog( RegistryKeepAliveRunner.class ); - /** The URL of the service to look for. */ - private final String namingURL; - - /** The service name. */ - private final String serviceName; + /** the host on which to start the registry */ + private final String registryHost; /** the port on which to start the registry */ private final int registryPort; @@ -63,8 +59,7 @@ public class RegistryKeepAliveRunner */ public RegistryKeepAliveRunner( final String registryHost, final int registryPort, final String serviceName ) { - this.namingURL = RemoteUtils.getNamingURL(registryHost, registryPort, serviceName); - this.serviceName = serviceName; + this.registryHost = registryHost; this.registryPort = registryPort; } @@ -85,32 +80,7 @@ public class RegistryKeepAliveRunner */ protected void checkAndRestoreIfNeeded() { - log.debug( "looking up server {0}", namingURL ); - - try - { - final Object obj = Naming.lookup( namingURL ); - - // Successful connection to the remote server. - final String message = "RMI registry looks fine. Found [" + obj + "] in registry [" + namingURL + "]"; - if ( cacheEventLogger != null ) - { - cacheEventLogger.logApplicationEvent( "RegistryKeepAliveRunner", "Naming.lookup", message ); - } - log.debug( message ); - } - catch ( final Exception ex ) - { - // Failed to connect to the remote server. - final String message = "Problem finding server at [" + namingURL - + "]. Will attempt to start registry and rebind."; - log.error( message, ex ); - if ( cacheEventLogger != null ) - { - cacheEventLogger.logError( "RegistryKeepAliveRunner", "Naming.lookup", message + ":" + ex.getMessage() ); - } - createAndRegister( serviceName ); - } + RemoteCacheServerFactory.keepAlive(registryHost, registryPort, cacheEventLogger); } /** @@ -160,13 +130,8 @@ public class RegistryKeepAliveRunner { // try to rebind anyway final Remote server = RemoteCacheServerFactory.getRemoteCacheServer(); + RemoteCacheServerFactory.registerServer(serviceName, server); - if ( server == null ) - { - throw new RemoteException( "Cannot register the server until it is created." ); - } - - this.registry.rebind( serviceName, server ); final String message = "Successfully rebound server to registry [" + serviceName + "]."; if ( cacheEventLogger != null ) { diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/DiscoveredService.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/DiscoveredService.java index a39d5cc..57cabb3 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/DiscoveredService.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/DiscoveredService.java @@ -48,6 +48,27 @@ public class DiscoveredService private long lastHearFromTime; /** + * Default constructor + */ + public DiscoveredService() + { + // empty + } + + /** + * Constructor + * + * @param message incoming message + */ + public DiscoveredService(UDPDiscoveryMessage message) + { + setServiceAddress( message.getHost() ); + setCacheNames( message.getCacheNames() ); + setServicePort( message.getPort() ); + setLastHearFromTime( System.currentTimeMillis() ); + } + + /** * @param cacheNames the cacheNames to set */ public void setCacheNames( final ArrayList<String> cacheNames ) diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPCleanupRunner.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPCleanupRunner.java index ea72028..d741d42 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPCleanupRunner.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPCleanupRunner.java @@ -19,9 +19,6 @@ package org.apache.commons.jcs3.utils.discovery; * under the License. */ -import org.apache.commons.jcs3.log.Log; -import org.apache.commons.jcs3.log.LogManager; - /** * This class periodically check the lastHeardFrom time on the services. * <p> @@ -34,18 +31,9 @@ import org.apache.commons.jcs3.log.LogManager; public class UDPCleanupRunner implements Runnable { - /** log instance */ - private static final Log log = LogManager.getLog( UDPCleanupRunner.class ); - /** UDP discovery service */ private final UDPDiscoveryService discoveryService; - /** default for max idle time, in seconds */ - private static final long DEFAULT_MAX_IDLE_TIME_SECONDS = 180; - - /** The configured max idle time, in seconds */ - private final long maxIdleTimeSeconds = DEFAULT_MAX_IDLE_TIME_SECONDS; - /** * @param service UDPDiscoveryService */ @@ -63,22 +51,6 @@ public class UDPCleanupRunner @Override public void run() { - final long now = System.currentTimeMillis(); - - // the listeners need to be notified. - discoveryService.getDiscoveredServices().stream() - .filter(service -> { - if (now - service.getLastHearFromTime() > maxIdleTimeSeconds * 1000) - { - log.info( "Removing service, since we haven't heard from it in " - + "{0} seconds. service = {1}", maxIdleTimeSeconds, service ); - return true; - } - - return false; - }) - // remove the bad ones - // call this so the listeners get notified - .forEach(service -> discoveryService.removeDiscoveredService(service)); + discoveryService.cleanup(); } } diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPDiscoveryReceiver.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPDiscoveryReceiver.java index 2e2baba..e269301 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPDiscoveryReceiver.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPDiscoveryReceiver.java @@ -323,11 +323,7 @@ public class UDPDiscoveryReceiver */ private void processMessage(UDPDiscoveryMessage message) { - final DiscoveredService discoveredService = new DiscoveredService(); - discoveredService.setServiceAddress( message.getHost() ); - discoveredService.setCacheNames( message.getCacheNames() ); - discoveredService.setServicePort( message.getPort() ); - discoveredService.setLastHearFromTime( System.currentTimeMillis() ); + final DiscoveredService discoveredService = new DiscoveredService(message); switch (message.getMessageType()) { diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPDiscoveryService.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPDiscoveryService.java index caa64d0..fcb301a 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPDiscoveryService.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/discovery/UDPDiscoveryService.java @@ -267,10 +267,7 @@ public class UDPDiscoveryService log.info( "Removing {0}", service ); } - for (final IDiscoveryListener listener : getDiscoveryListeners()) - { - listener.removeDiscoveredService( service ); - } + getDiscoveryListeners().forEach(listener -> listener.removeDiscoveredService(service)); } /** @@ -296,24 +293,17 @@ public class UDPDiscoveryService log.debug( "Updating service in the set {0}", discoveredService ); // Update the list of cache names if it has changed. - DiscoveredService theOldServiceInformation = null; // need to update the time this sucks. add has no effect convert to a map - for (final DiscoveredService service1 : discoveredServices) - { - if ( discoveredService.equals( service1 ) ) - { - theOldServiceInformation = service1; - break; - } - } - if ( theOldServiceInformation != null ) + DiscoveredService theOldServiceInformation = discoveredServices.stream() + .filter(service -> discoveredService.equals(service)) + .findFirst() + .orElse(null); + + if (theOldServiceInformation != null && + !theOldServiceInformation.getCacheNames().equals(discoveredService.getCacheNames())) { - if ( !theOldServiceInformation.getCacheNames().equals( - discoveredService.getCacheNames() ) ) - { - log.info( "List of cache names changed for service: {0}", - discoveredService ); - } + log.info( "List of cache names changed for service: {0}", + discoveredService ); } // replace it, we want to reset the payload and the last heard from time. @@ -325,10 +315,7 @@ public class UDPDiscoveryService // If we don't do this, then if a region using the default config is initialized after notification, // it will never get the service in it's no wait list. // Leave it to the listeners to decide what to do. - for (final IDiscoveryListener listener : getDiscoveryListeners()) - { - listener.addDiscoveredService( discoveredService ); - } + getDiscoveryListeners().forEach(listener -> listener.addDiscoveredService( discoveredService)); } /** @@ -338,8 +325,7 @@ public class UDPDiscoveryService */ protected ArrayList<String> getCacheNames() { - final ArrayList<String> names = new ArrayList<>(cacheNames); - return names; + return new ArrayList<>(cacheNames); } /**