Search in sources :

Example 1 with CachedReference

use of jp.ossc.nimbus.service.cache.CachedReference in project nimbus by nimbus-org.

the class SharedContextService method wrapCachedReference.

protected Object wrapCachedReference(Object key, Object value) {
    if (value == null || cacheMap == null) {
        return value;
    } else {
        cacheMap.put(key, value);
        CachedReference ref = cacheMap.getCachedReference(key);
        if (ref != null) {
            ref.addCacheRemoveListener(this);
        }
        return ref;
    }
}
Also used : CachedReference(jp.ossc.nimbus.service.cache.CachedReference) KeyCachedReference(jp.ossc.nimbus.service.cache.KeyCachedReference)

Example 2 with CachedReference

use of jp.ossc.nimbus.service.cache.CachedReference in project nimbus by nimbus-org.

the class SharedContextService method setClient.

public synchronized void setClient(boolean isClient) throws SharedContextSendException, SharedContextTimeoutException {
    if (getState() == STARTED) {
        try {
            if (this.isClient == isClient) {
                return;
            }
            Object id = cluster.getUID();
            try {
                referLock.acquireForLock(id, -1);
                updateLock.acquireForLock(id, -1);
                CacheMap oldCacheMap = cacheMap;
                if (isClient) {
                    if (clientCacheMapServiceName != null) {
                        cacheMap = (CacheMap) ServiceManagerFactory.getServiceObject(clientCacheMapServiceName);
                    } else if (clientCacheMap != null) {
                        cacheMap = clientCacheMap;
                    }
                } else {
                    if (serverCacheMapServiceName != null) {
                        cacheMap = (CacheMap) ServiceManagerFactory.getServiceObject(serverCacheMapServiceName);
                    } else if (serverCacheMap != null) {
                        cacheMap = serverCacheMap;
                    }
                }
                if (indexManager != null) {
                    indexManager.clear();
                }
                try {
                    messageReceiver.addSubject(this, isClient ? clientSubject : subject);
                } catch (MessageSendException e) {
                    throw new SharedContextSendException(e);
                }
                try {
                    if (isClient) {
                        synchronizeForClient(synchronizeTimeout);
                    } else {
                        synchronizeWithMain(synchronizeTimeout);
                    }
                } catch (NoConnectServerException e) {
                }
                try {
                    messageReceiver.removeSubject(this, isClient ? subject : clientSubject);
                } catch (MessageSendException e) {
                    throw new SharedContextSendException(e);
                }
                if (oldCacheMap != null) {
                    Object[] keys = null;
                    synchronized (context) {
                        keys = super.keySet().toArray();
                    }
                    for (int i = 0; i < keys.length; i++) {
                        CachedReference ref = oldCacheMap.getCachedReference(keys[i]);
                        if (ref != null) {
                            ref.removeCacheRemoveListener(this);
                        }
                        oldCacheMap.remove(keys[i]);
                    }
                }
            } finally {
                updateLock.releaseForLock(id);
                referLock.releaseForLock(id);
            }
            this.isClient = isClient;
            resetCacheHitRatio();
        } finally {
            boolean isMainTmp = isMain();
            if (isMain != isMainTmp) {
                if (!isClient && updateListeners != null) {
                    for (int i = 0; i < updateListeners.size(); i++) {
                        if (isMainTmp) {
                            ((SharedContextUpdateListener) updateListeners.get(i)).onChangeMain(this);
                        } else {
                            ((SharedContextUpdateListener) updateListeners.get(i)).onChangeSub(this);
                        }
                    }
                }
            }
            isMain = isMainTmp;
        }
        try {
            Message message = serverConnection.createMessage(subject, null);
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() != 0) {
                message.setObject(new SharedContextEvent(SharedContextEvent.EVENT_CHANGE_MODE, cluster.getUID(), isClient));
                serverConnection.sendAsynch(message);
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        }
    } else {
        this.isClient = isClient;
    }
}
Also used : CachedReference(jp.ossc.nimbus.service.cache.CachedReference) KeyCachedReference(jp.ossc.nimbus.service.cache.KeyCachedReference) CacheMap(jp.ossc.nimbus.service.cache.CacheMap)

Example 3 with CachedReference

use of jp.ossc.nimbus.service.cache.CachedReference in project nimbus by nimbus-org.

the class SharedContextService method unwrapCachedReference.

protected Object unwrapCachedReference(Object value, boolean notify, boolean remove) {
    if (value == null) {
        return null;
    }
    if (cacheMap == null) {
        return value;
    } else {
        CachedReference ref = (CachedReference) value;
        Object ret = ref.get(this, notify);
        if (remove) {
            ref.remove(this);
        }
        return ret;
    }
}
Also used : CachedReference(jp.ossc.nimbus.service.cache.CachedReference) KeyCachedReference(jp.ossc.nimbus.service.cache.KeyCachedReference)

Example 4 with CachedReference

use of jp.ossc.nimbus.service.cache.CachedReference in project nimbus by nimbus-org.

the class DelayQueueService method pushElement.

protected boolean pushElement(Object element, long timeout) {
    if (getState() != STARTED || fourceEndFlg) {
        throw new IllegalServiceStateException(this);
    }
    if (!(element instanceof QueueElement)) {
        element = new QueueElement(element);
    }
    if (maxThresholdSize > 0 && (pushMonitor.isWait() || (size() >= maxThresholdSize)) && !fourceEndFlg) {
        try {
            if (timeout == 0) {
                return false;
            } else if (timeout < 0) {
                pushMonitor.initAndWaitMonitor();
            } else {
                if (!pushMonitor.initAndWaitMonitor(timeout)) {
                    return false;
                }
            }
        } catch (InterruptedException e) {
            return false;
        } finally {
            pushMonitor.releaseMonitor();
        }
    }
    if (cache == null) {
        queueElements.add(element);
    } else {
        final CachedReference ref = cache.add(element);
        if (ref != null) {
            ref.addCacheRemoveListener(this);
            queueElements.add(ref);
        } else {
            queueElements.add(element);
        }
    }
    int size = size();
    if (size > maxDepth) {
        maxDepth = size;
    }
    count++;
    countDelta++;
    lastPushedTime = System.currentTimeMillis();
    peekMonitor.notifyAllMonitor();
    if (getMonitor.isWait()) {
        long waitTime = 0;
        if (isDelay) {
            Object firstElement = getQueueElement(false);
            if (firstElement != EMPTY) {
                waitTime = delayTime - (System.currentTimeMillis() - ((QueueElement) firstElement).pushTime);
            }
        }
        if (!isDelay || waitTime <= 0) {
            if (isSafeGetOrder) {
                getMonitor.notifyMonitor();
            } else {
                getMonitor.notifyAllMonitor();
            }
        }
    }
    if (pushMonitor.isWait() && size() < maxThresholdSize) {
        pushMonitor.notifyMonitor();
    }
    return true;
}
Also used : IllegalServiceStateException(jp.ossc.nimbus.lang.IllegalServiceStateException) CachedReference(jp.ossc.nimbus.service.cache.CachedReference)

Example 5 with CachedReference

use of jp.ossc.nimbus.service.cache.CachedReference in project nimbus by nimbus-org.

the class CacheMap method putAll.

/* (非 Javadoc)
     * @see java.util.Map#putAll(java.util.Map)
     */
public void putAll(Map map) {
    final Iterator ite = map.keySet().iterator();
    for (; ite.hasNext(); ) {
        final Object key = ite.next();
        final Object value = map.get(key);
        if (key == null) {
            return;
        }
        if (key instanceof CachedReference) {
            // そのまま登録
            super.put(key, value);
        }
    }
}
Also used : Iterator(java.util.Iterator) CachedReference(jp.ossc.nimbus.service.cache.CachedReference)

Aggregations

CachedReference (jp.ossc.nimbus.service.cache.CachedReference)5 KeyCachedReference (jp.ossc.nimbus.service.cache.KeyCachedReference)3 Iterator (java.util.Iterator)1 IllegalServiceStateException (jp.ossc.nimbus.lang.IllegalServiceStateException)1 CacheMap (jp.ossc.nimbus.service.cache.CacheMap)1