Search in sources :

Example 1 with CacheMap

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

the class DistributedSharedContextService method startService.

/**
 * サービスの開始処理を行う。<p>
 *
 * @exception Exception サービスの開始処理に失敗した場合
 */
public void startService() throws Exception {
    if (sharedContextKeyDistributorServiceName == null) {
        MD5HashSharedContextKeyDistributorService defaultKeyDistributor = new MD5HashSharedContextKeyDistributorService();
        defaultKeyDistributor.create();
        defaultKeyDistributor.start();
        keyDistributor = defaultKeyDistributor;
    } else {
        keyDistributor = (SharedContextKeyDistributor) ServiceManagerFactory.getServiceObject(sharedContextKeyDistributorServiceName);
    }
    if (contextStoreServiceName != null) {
        contextStore = (ContextStore) ServiceManagerFactory.getServiceObject(contextStoreServiceName);
    }
    if (requestConnectionFactoryServiceName == null) {
        throw new IllegalArgumentException("RequestConnectionFactoryServiceName must be specified.");
    }
    if (sharedContextUpdateListenerServiceNames != null) {
        for (int i = 0; i < sharedContextUpdateListenerServiceNames.length; i++) {
            addSharedContextUpdateListener((SharedContextUpdateListener) ServiceManagerFactory.getServiceObject(sharedContextUpdateListenerServiceNames[i]));
        }
    }
    if (parallelRequestThreadSize > 0) {
        parallelRequestQueueHandlerContainer = new QueueHandlerContainerService();
        parallelRequestQueueHandlerContainer.create();
        parallelRequestQueueHandlerContainer.setQueueHandlerSize(parallelRequestThreadSize);
        if (parallelRequestQueueServiceName == null) {
            DefaultQueueService parallelRequestQueue = new DefaultQueueService();
            parallelRequestQueue.create();
            parallelRequestQueue.start();
            parallelRequestQueueHandlerContainer.setQueueService(parallelRequestQueue);
        } else {
            parallelRequestQueueHandlerContainer.setQueueServiceName(parallelRequestQueueServiceName);
        }
        parallelRequestQueueHandlerContainer.setQueueHandler(new ParallelRequestQueueHandler());
        parallelRequestQueueHandlerContainer.start();
    }
    ServerConnectionFactory factory = (ServerConnectionFactory) ServiceManagerFactory.getServiceObject(requestConnectionFactoryServiceName);
    serverConnection = (RequestServerConnection) factory.getServerConnection();
    messageReceiver = (MessageReceiver) ServiceManagerFactory.getServiceObject(requestConnectionFactoryServiceName);
    clientSubject = subject + CLIENT_SUBJECT_SUFFIX;
    targetMessage = serverConnection.createMessage(subject, null);
    if (clusterServiceName == null) {
        throw new IllegalArgumentException("ClusterServiceName must be specified.");
    }
    cluster = (ClusterService) ServiceManagerFactory.getServiceObject(clusterServiceName);
    if (interpreterServiceName != null) {
        interpreter = (Interpreter) ServiceManagerFactory.getServiceObject(interpreterServiceName);
    }
    sharedContextArray = new SharedContextService[distributedSize];
    for (int i = 0; i < sharedContextArray.length; i++) {
        sharedContextArray[i] = isMainDistributed ? new ForDistributedSharedContextService(i) : new SharedContextService();
        if (isManagedDataNode) {
            sharedContextArray[i].setServiceManagerName(getServiceManagerName());
        }
        sharedContextArray[i].setServiceName(getServiceName() + '$' + i);
        sharedContextArray[i].create();
        sharedContextArray[i].setRequestConnectionFactoryServiceName(requestConnectionFactoryServiceName);
        sharedContextArray[i].setClusterServiceName(clusterServiceName);
        if (clientCacheMapServiceName != null) {
            sharedContextArray[i].setClientCacheMap((CacheMap) ServiceManagerFactory.getServiceObject(clientCacheMapServiceName));
        }
        if (serverCacheMapServiceName != null) {
            sharedContextArray[i].setServerCacheMap((CacheMap) ServiceManagerFactory.getServiceObject(serverCacheMapServiceName));
        }
        if (contextStoreServiceName != null) {
            sharedContextArray[i].setContextStoreServiceName(contextStoreServiceName);
        }
        if (interpreterServiceName != null) {
            sharedContextArray[i].setInterpreterServiceName(interpreterServiceName);
        }
        if (interpretContextVariableName != null) {
            sharedContextArray[i].setInterpretContextVariableName(interpretContextVariableName);
        }
        if (executeQueueServiceName != null) {
            sharedContextArray[i].setExecuteQueueServiceName(executeQueueServiceName);
        }
        if (sharedContextTransactionManagerServiceName != null) {
            sharedContextArray[i].setSharedContextTransactionManagerServiceName(sharedContextTransactionManagerServiceName);
        }
        sharedContextArray[i].setExecuteThreadSize(executeThreadSize);
        sharedContextArray[i].setSubject(subject + "$" + i);
        sharedContextArray[i].setClient(isClient || isRehashEnabled ? true : false);
        sharedContextArray[i].setSynchronizeTimeout(synchronizeTimeout);
        sharedContextArray[i].setDefaultTimeout(defaultTimeout);
        sharedContextArray[i].setSynchronizeOnStart(false);
        sharedContextArray[i].setSaveOnlyMain(true);
        sharedContextArray[i].setClearBeforeSave(false);
        sharedContextArray[i].setLoadOnStart(false);
        sharedContextArray[i].setLoadKeyOnStart(false);
        sharedContextArray[i].setSaveOnStop(false);
        sharedContextArray[i].setWaitConnectAllOnStart(isWaitConnectAllOnStart());
        sharedContextArray[i].setWaitConnectTimeout(waitConnectTimeout);
        if (updateListeners != null) {
            for (int j = 0; j < updateListeners.size(); j++) {
                sharedContextArray[i].addSharedContextUpdateListener((SharedContextUpdateListener) updateListeners.get(j));
            }
        }
        Iterator entries = indexMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            if (entry.getValue() instanceof BeanTableIndexKeyFactory) {
                sharedContextArray[i].setIndex((String) entry.getKey(), (BeanTableIndexKeyFactory) entry.getValue());
            } else {
                sharedContextArray[i].setIndex((String) entry.getKey(), (String[]) entry.getValue());
            }
        }
        sharedContextArray[i].start();
    }
    distributeInfo = new DistributeInfo(getId(), distributedSize);
    serverConnection.addServerConnectionListener(this);
    messageReceiver.addSubject(this, isClient ? clientSubject : subject);
    if (!isClient) {
        rehash();
    }
    for (int i = 0; i < sharedContextArray.length; i++) {
        if (sharedContextArray[i].isClient() && sharedContextArray[i].indexManager != null && sharedContextArray[i].indexManager.hasIndex()) {
            sharedContextArray[i].waitConnectMain();
            sharedContextArray[i].synchronize();
        }
    }
    if (isMain()) {
        if (isLoadKeyOnStart) {
            loadKey();
        } else if (isLoadOnStart) {
            load();
        }
    }
}
Also used : QueueHandlerContainerService(jp.ossc.nimbus.service.queue.QueueHandlerContainerService) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService) ServerConnectionFactory(jp.ossc.nimbus.service.publish.ServerConnectionFactory) Iterator(java.util.Iterator) CacheMap(jp.ossc.nimbus.service.cache.CacheMap) HashMap(java.util.HashMap) Map(java.util.Map) BeanTableIndexKeyFactory(jp.ossc.nimbus.beans.BeanTableIndexKeyFactory)

Example 2 with CacheMap

use of jp.ossc.nimbus.service.cache.CacheMap 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 CacheMap

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

the class SharedContextService method synchronizeForClient.

protected synchronized void synchronizeForClient(long timeout) throws SharedContextSendException, SharedContextTimeoutException {
    if (cacheMap != null) {
        Object[] keys = null;
        synchronized (context) {
            keys = super.keySet().toArray();
        }
        for (int i = 0; i < keys.length; i++) {
            cacheMap.remove(keys[i]);
        }
    }
    if (updateListeners != null) {
        for (int i = 0; i < updateListeners.size(); i++) {
            ((SharedContextUpdateListener) updateListeners.get(i)).onClearSynchronize(this);
        }
    }
    super.clear();
    if (updateListeners != null || indexManager.hasIndex()) {
        try {
            Message message = serverConnection.createMessage(subject, null);
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() != 0) {
                message.setObject(new SharedContextEvent(SharedContextEvent.EVENT_GET_ALL));
                Message[] responses = serverConnection.request(message, 1, timeout);
                Map result = (Map) responses[0].getObject();
                responses[0].recycle();
                if (result != null) {
                    Iterator entries = result.entrySet().iterator();
                    while (entries.hasNext()) {
                        Map.Entry entry = (Map.Entry) entries.next();
                        boolean isPut = true;
                        if (updateListeners != null) {
                            for (int i = 0; i < updateListeners.size(); i++) {
                                if (!((SharedContextUpdateListener) updateListeners.get(i)).onPutSynchronize(this, entry.getKey(), entry.getValue())) {
                                    isPut = false;
                                    break;
                                }
                            }
                        }
                        if (isPut && indexManager.hasIndex() && entry.getValue() != null) {
                            indexManager.add(entry.getKey(), entry.getValue());
                        }
                    }
                }
            } else {
                throw new NoConnectServerException();
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        } catch (RequestTimeoutException e) {
            throw new SharedContextTimeoutException(e);
        }
    }
}
Also used : CacheMap(jp.ossc.nimbus.service.cache.CacheMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 4 with CacheMap

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

the class SharedContextService method allLocal.

public Map allLocal() {
    try {
        referLock.acquireForUse(-1);
        if (cacheMap == null) {
            return super.all();
        } else {
            Map result = new HashMap();
            Object[] keys = null;
            synchronized (context) {
                keys = super.keySet().toArray();
            }
            for (int i = 0; i < keys.length; i++) {
                result.put(keys[i], cacheMap.get(keys[i]));
            }
            return result;
        }
    } finally {
        referLock.releaseForUse();
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CacheMap(jp.ossc.nimbus.service.cache.CacheMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

CacheMap (jp.ossc.nimbus.service.cache.CacheMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 BeanTableIndexKeyFactory (jp.ossc.nimbus.beans.BeanTableIndexKeyFactory)1 CachedReference (jp.ossc.nimbus.service.cache.CachedReference)1 KeyCachedReference (jp.ossc.nimbus.service.cache.KeyCachedReference)1 ServerConnectionFactory (jp.ossc.nimbus.service.publish.ServerConnectionFactory)1 DefaultQueueService (jp.ossc.nimbus.service.queue.DefaultQueueService)1 QueueHandlerContainerService (jp.ossc.nimbus.service.queue.QueueHandlerContainerService)1