Search in sources :

Example 31 with MessageSendException

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

the class DistributedSharedContextService method onRehashRequest.

protected Message onRehashRequest(final DistributedSharedContextEvent event, final Object sourceId, final int sequence, final String responseSubject, final String responseKey) {
    if (isMain()) {
        Thread rehashThread = new Thread() {

            public void run() {
                Message response = null;
                try {
                    rehash(((Long) event.value).longValue());
                    response = createResponseMessage(responseSubject, responseKey, null);
                } catch (Throwable th) {
                    response = createResponseMessage(responseSubject, responseKey, th);
                }
                try {
                    serverConnection.response(sourceId, sequence, response);
                } catch (MessageSendException e) {
                    getLogger().write("DSCS_00002", new Object[] { isClient ? clientSubject : subject, response }, e);
                }
            }
        };
        rehashThread.start();
    }
    return null;
}
Also used : MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Message(jp.ossc.nimbus.service.publish.Message)

Example 32 with MessageSendException

use of jp.ossc.nimbus.service.publish.MessageSendException 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

MessageSendException (jp.ossc.nimbus.service.publish.MessageSendException)32 Message (jp.ossc.nimbus.service.publish.Message)15 HashSet (java.util.HashSet)14 Set (java.util.Set)14 IOException (java.io.IOException)13 MessageException (jp.ossc.nimbus.service.publish.MessageException)13 SocketException (java.net.SocketException)9 SocketTimeoutException (java.net.SocketTimeoutException)9 LinkedHashSet (java.util.LinkedHashSet)9 RequestTimeoutException (jp.ossc.nimbus.service.publish.RequestTimeoutException)9 HashMap (java.util.HashMap)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Iterator (java.util.Iterator)4 ConnectException (jp.ossc.nimbus.service.publish.ConnectException)4 AsynchContext (jp.ossc.nimbus.service.queue.AsynchContext)4 DefaultQueueService (jp.ossc.nimbus.service.queue.DefaultQueueService)4 EOFException (java.io.EOFException)3 Map (java.util.Map)3 MessageCreateException (jp.ossc.nimbus.service.publish.MessageCreateException)3