package org.apache.commons.jcs3.auxiliary.remote; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.io.IOException; import java.rmi.Naming; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheAttributes; import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheClient; import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheListener; import org.apache.commons.jcs3.engine.CacheStatus; import org.apache.commons.jcs3.engine.CacheWatchRepairable; import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal; import org.apache.commons.jcs3.engine.ZombieCacheWatch; import org.apache.commons.jcs3.engine.behavior.ICacheObserver; import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal; import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager; import org.apache.commons.jcs3.engine.behavior.IElementSerializer; import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger; import org.apache.commons.jcs3.log.Log; import org.apache.commons.jcs3.log.LogManager; /** * An instance of RemoteCacheManager corresponds to one remote connection of a specific host and * port. All RemoteCacheManager instances are monitored by the singleton RemoteCacheMonitor * monitoring daemon for error detection and recovery. *
* Getting an instance of the remote cache has the effect of getting a handle on the remote server.
* Listeners are not registered with the server until a cache is requested from the manager.
*/
public class RemoteCacheManager
{
/** The logger */
private static final Log log = LogManager.getLog( RemoteCacheManager.class );
/** Contains instances of RemoteCacheNoWait managed by a RemoteCacheManager instance. */
private final ConcurrentMap
* @param cattr cache attributes
* @param cacheMgr the cache hub
* @param monitor the cache monitor thread for error notifications
* @param cacheEventLogger
* @param elementSerializer
*/
protected RemoteCacheManager( final IRemoteCacheAttributes cattr, final ICompositeCacheManager cacheMgr,
final RemoteCacheMonitor monitor,
final ICacheEventLogger cacheEventLogger, final IElementSerializer elementSerializer)
{
this.cacheMgr = cacheMgr;
this.monitor = monitor;
this.cacheEventLogger = cacheEventLogger;
this.elementSerializer = elementSerializer;
this.remoteWatch = new CacheWatchRepairable();
this.registry = RemoteUtils.getNamingURL(cattr.getRemoteLocation(), cattr.getRemoteServiceName());
try
{
lookupRemoteService();
}
catch (final IOException e)
{
log.error("Could not find server", e);
// Notify the cache monitor about the error, and kick off the
// recovery process.
monitor.notifyError();
}
}
/**
* Lookup remote service from registry
* @throws IOException if the remote service could not be found
*
*/
protected void lookupRemoteService() throws IOException
{
log.info( "Looking up server [{0}]", registry );
try
{
final Object obj = Naming.lookup( registry );
log.info( "Server found: {0}", obj );
// Successful connection to the remote server.
this.remoteService = (ICacheServiceNonLocal, ?>) obj;
log.debug( "Remote Service = {0}", remoteService );
remoteWatch.setCacheWatch( (ICacheObserver) remoteService );
}
catch ( final Exception ex )
{
// Failed to connect to the remote server.
// Configure this RemoteCacheManager instance to use the "zombie"
// services.
this.remoteService = new ZombieCacheServiceNonLocal<>();
remoteWatch.setCacheWatch( new ZombieCacheWatch() );
throw new IOException( "Problem finding server at [" + registry + "]", ex );
}
}
/**
* Adds the remote cache listener to the underlying cache-watch service.
*
* @param cattr The feature to be added to the RemoteCacheListener attribute
* @param listener The feature to be added to the RemoteCacheListener attribute
* @throws IOException
*/
public
* @param cattr
* @throws IOException
*/
public void removeRemoteCacheListener( final IRemoteCacheAttributes cattr )
throws IOException
{
final RemoteCacheNoWait, ?> cache = caches.get( cattr.getCacheName() );
if ( cache != null )
{
removeListenerFromCache(cache);
}
else
{
if ( cattr.isReceive() )
{
log.warn( "Trying to deregister Cache Listener that was never registered." );
}
else
{
log.debug( "Since the remote cache is configured to not receive, "
+ "there is no listener to deregister." );
}
}
}
// common helper method
private void removeListenerFromCache(final RemoteCacheNoWait, ?> cache) throws IOException
{
final IRemoteCacheClient, ?> rc = cache.getRemoteCache();
log.debug( "Found cache for [{0}], deregistering listener.", cache::getCacheName);
// could also store the listener for a server in the manager.
remoteWatch.removeCacheListener(cache.getCacheName(), rc.getListener());
}
/**
* Gets a RemoteCacheNoWait from the RemoteCacheManager. The RemoteCacheNoWait objects are
* identified by the cache name value of the RemoteCacheAttributes object.
*
* If the client is configured to register a listener, this call results on a listener being
* created if one isn't already registered with the remote cache for this region.
*
* @param cattr
* @return The cache value
*/
@SuppressWarnings("unchecked") // Need to cast because of common map for all caches
public
* @return true if we found a failover server
*/
public boolean canFixCaches()
{
try
{
lookupRemoteService();
}
catch (final IOException e)
{
log.error("Could not find server", e);
canFix = false;
}
return canFix;
}
}