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.UnmarshalException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCache; import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes; import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheClient; import org.apache.commons.jcs3.engine.CacheAdaptor; import org.apache.commons.jcs3.engine.CacheEventQueueFactory; import org.apache.commons.jcs3.engine.CacheStatus; import org.apache.commons.jcs3.engine.behavior.ICacheElement; import org.apache.commons.jcs3.engine.behavior.ICacheEventQueue; import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal; import org.apache.commons.jcs3.engine.stats.StatElement; import org.apache.commons.jcs3.engine.stats.Stats; import org.apache.commons.jcs3.engine.stats.behavior.IStatElement; import org.apache.commons.jcs3.engine.stats.behavior.IStats; import org.apache.commons.jcs3.log.Log; import org.apache.commons.jcs3.log.LogManager; /** * The RemoteCacheNoWait wraps the RemoteCacheClient. The client holds a handle on the * RemoteCacheService. *
* Used to queue up update requests to the underlying cache. These requests will be processed in * their order of arrival via the cache event queue processor. *
* Typically errors will be handled down stream. We only need to kill the queue if an error makes it * to this level from the queue. That can only happen if the queue is damaged, since the events are * Processed asynchronously. *
* There is no reason to create a queue on startup if the remote is not healthy. *
* If the remote cache encounters an error it will zombie--create a balking facade for the service.
* The Zombie will queue up items until the connection is restored. An alternative way to accomplish
* the same thing would be to stop, not destroy the queue at this level. That way items would be
* added to the queue and then when the connection is restored, we could start the worker threads
* again. This is a better long term solution, but it requires some significant changes to the
* complicated worker queues.
*/
public class RemoteCacheNoWait
* @param cache
*/
public RemoteCacheNoWait( final IRemoteCacheClient
* @param element
* @throws IOException
*/
@Override
public void update( final ICacheElement
* @param key
* @return element from the remote cache, or null if not present
* @throws IOException
*/
@Override
public ICacheElement
* @param keys
* @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no
* data in cache for any of these keys
* @throws IOException
*/
@Override
public Map
* @see org.apache.commons.jcs3.auxiliary.AuxiliaryCache#getKeySet()
*/
@Override
public Set
* @param key
* @return if this was successful
* @throws IOException
*/
@Override
public boolean remove( final K key )
throws IOException
{
removeCount++;
try
{
cacheEventQueue.addRemoveEvent( key );
}
catch ( final IOException e )
{
log.error( "Problem adding RemoveEvent to queue.", e );
cacheEventQueue.destroy();
throw e;
}
return false;
}
/**
* Adds a removeAll request to the remote cache.
*
* @throws IOException
*/
@Override
public void removeAll()
throws IOException
{
try
{
cacheEventQueue.addRemoveAllEvent();
}
catch ( final IOException e )
{
log.error( "Problem adding RemoveAllEvent to queue.", e );
cacheEventQueue.destroy();
throw e;
}
}
/** Adds a dispose request to the remote cache. */
@Override
public void dispose()
{
try
{
cacheEventQueue.addDisposeEvent();
}
catch ( final IOException e )
{
log.error( "Problem adding DisposeEvent to queue.", e );
cacheEventQueue.destroy();
}
}
/**
* No remote invocation.
*
* @return The size value
*/
@Override
public int getSize()
{
return remoteCacheClient.getSize();
}
/**
* No remote invocation.
*
* @return The cacheType value
*/
@Override
public CacheType getCacheType()
{
return CacheType.REMOTE_CACHE;
}
/**
* Returns the async cache status. An error status indicates either the remote connection is not
* available, or the asyn queue has been unexpectedly destroyed. No remote invocation.
*
* @return The status value
*/
@Override
public CacheStatus getStatus()
{
return cacheEventQueue.isWorking() ? remoteCacheClient.getStatus() : CacheStatus.ERROR;
}
/**
* Gets the cacheName attribute of the RemoteCacheNoWait object
*
* @return The cacheName value
*/
@Override
public String getCacheName()
{
return remoteCacheClient.getCacheName();
}
/**
* Replaces the remote cache service handle with the given handle and reset the event queue by
* starting up a new instance.
*
* @param remote
*/
public void fixCache( final ICacheServiceNonLocal, ?> remote )
{
remoteCacheClient.fixCache( remote );
resetEventQ();
}
/**
* Resets the event q by first destroying the existing one and starting up new one.
*
* There may be no good reason to kill the existing queue. We will sometimes need to set a new
* listener id, so we should create a new queue. We should let the old queue drain. If we were
* Connected to the failover, it would be best to finish sending items.
*/
public void resetEventQ()
{
final ICacheEventQueue
* @return the instance of the remote cache client used by this object
*/
protected IRemoteCacheClient
* @return ICacheEventQueue
*/
protected ICacheEventQueue
* @see Object#toString()
*/
@Override
public String toString()
{
return getStats() + "\n" + remoteCacheClient.toString();
}
/**
* Returns the statistics in String form.
*
* @return String
*/
@Override
public String getStats()
{
return getStatistics().toString();
}
/**
* @return statistics about this communication
*/
@Override
public IStats getStatistics()
{
final IStats stats = new Stats();
stats.setTypeName( "Remote Cache No Wait" );
final ArrayList
* @return String
*/
@Override
public String getEventLoggingExtraInfo()
{
return "Remote Cache No Wait";
}
}