Search in sources :

Example 16 with MessageException

use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.

the class DistributedSharedContextService method getClientMemberIdSet.

public Set getClientMemberIdSet() {
    if (serverConnection == null || clientSubject == null) {
        return new HashSet();
    }
    try {
        Message message = serverConnection.createMessage(clientSubject, null);
        Set result = serverConnection.getReceiveClientIds(message);
        if (isClient) {
            result.add(getId());
        }
        return result;
    } catch (MessageException e) {
        return new HashSet();
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Message(jp.ossc.nimbus.service.publish.Message) MessageException(jp.ossc.nimbus.service.publish.MessageException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 17 with MessageException

use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.

the class SharedQueueService method lockFirst.

protected Object lockFirst(long timeout) throws SharedContextSendException, SharedContextTimeoutException {
    if (isMain()) {
        List keys = null;
        if (context.size() != 0) {
            synchronized (context) {
                if (context.size() != 0) {
                    Iterator itr = context.keySet().iterator();
                    for (int i = 0; i < seekDepth && itr.hasNext(); i++) {
                        if (keys == null) {
                            keys = new ArrayList();
                        }
                        keys.add(itr.next());
                    }
                }
            }
        }
        if (keys == null || keys.size() == 0) {
            return null;
        }
        for (int i = 0; i < keys.size(); i++) {
            Object key = keys.get(i);
            try {
                if (lock(key, true, true, timeout)) {
                    return key;
                }
            } catch (SharedContextTimeoutException e) {
                continue;
            }
        }
        return null;
    } else {
        Object lockedKey = null;
        final long start = System.currentTimeMillis();
        try {
            String key = getId().toString() + Thread.currentThread().getId();
            Message message = serverConnection.createMessage(subject, key);
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() != 0) {
                message.setObject(new SharedQueueEvent(SharedQueueEvent.EVENT_LOCK_FIRST, null, new Object[] { new Long(Thread.currentThread().getId()), new Long(timeout) }));
                Message[] responses = serverConnection.request(message, isClient ? clientSubject : subject, key, 1, timeout);
                Object ret = responses[0].getObject();
                responses[0].recycle();
                if (ret instanceof Throwable) {
                    throw new SharedContextSendException((Throwable) ret);
                } else {
                    lockedKey = ret;
                }
            } else {
                throw new NoConnectServerException("Main server is not found.");
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        } catch (RequestTimeoutException e) {
            final boolean isNoTimeout = timeout <= 0;
            timeout = isNoTimeout ? timeout : timeout - (System.currentTimeMillis() - start);
            if (!isNoTimeout && timeout <= 0) {
                throw new SharedContextTimeoutException("timeout=" + timeout + ", processTime=" + (System.currentTimeMillis() - start), e);
            } else {
                return lockFirst(timeout);
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Error e) {
            throw e;
        }
        if (lockedKey != null) {
            Lock lock = null;
            synchronized (keyLockMap) {
                lock = (Lock) keyLockMap.get(lockedKey);
                if (lock == null) {
                    lock = new Lock(lockedKey);
                    keyLockMap.put(lockedKey, lock);
                }
            }
            final boolean isNoTimeout = timeout <= 0;
            timeout = isNoTimeout ? timeout : timeout - (System.currentTimeMillis() - start);
            if (!isNoTimeout && timeout <= 0) {
                unlock(lockedKey);
                throw new SharedContextTimeoutException("timeout=" + timeout + ", processTime=" + (System.currentTimeMillis() - start));
            } else if (!lock.acquire(getId(), true, timeout)) {
                unlock(lockedKey);
                return null;
            }
        }
        return lockedKey;
    }
}
Also used : Message(jp.ossc.nimbus.service.publish.Message) SharedContextSendException(jp.ossc.nimbus.service.context.SharedContextSendException) RequestTimeoutException(jp.ossc.nimbus.service.publish.RequestTimeoutException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) MessageException(jp.ossc.nimbus.service.publish.MessageException) NoConnectServerException(jp.ossc.nimbus.service.context.NoConnectServerException) SharedContextTimeoutException(jp.ossc.nimbus.service.context.SharedContextTimeoutException)

Aggregations

MessageException (jp.ossc.nimbus.service.publish.MessageException)17 Message (jp.ossc.nimbus.service.publish.Message)14 HashSet (java.util.HashSet)10 LinkedHashSet (java.util.LinkedHashSet)10 Set (java.util.Set)10 MessageSendException (jp.ossc.nimbus.service.publish.MessageSendException)9 RequestTimeoutException (jp.ossc.nimbus.service.publish.RequestTimeoutException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ObjectInputStream (java.io.ObjectInputStream)2 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 IndexNotFoundException (jp.ossc.nimbus.beans.IndexNotFoundException)1 IndexPropertyAccessException (jp.ossc.nimbus.beans.IndexPropertyAccessException)1 CacheMap (jp.ossc.nimbus.service.cache.CacheMap)1