Search in sources :

Example 16 with AsynchContext

use of jp.ossc.nimbus.service.queue.AsynchContext in project nimbus by nimbus-org.

the class DistributedClientConnectionImpl method removeSubject.

public void removeSubject(String subject, String[] keys) throws MessageSendException {
    if (parallelRequestQueueHandlerContainer == null) {
        for (int i = 0, imax = connectionList.size(); i < imax; i++) {
            ((ClientConnection) connectionList.get(i)).removeSubject(subject, keys);
        }
    } else {
        DefaultQueueService responseQueue = new DefaultQueueService();
        try {
            responseQueue.create();
            responseQueue.start();
        } catch (Exception e) {
        }
        responseQueue.accept();
        for (int i = 0, imax = connectionList.size(); i < imax; i++) {
            AsynchContext asynchContext = new AsynchContext(new RemoveSubjectParallelRequest((ClientConnection) connectionList.get(i), subject, keys), responseQueue);
            parallelRequestQueueHandlerContainer.push(asynchContext);
        }
        for (int i = 0, imax = connectionList.size(); i < imax; i++) {
            AsynchContext asynchContext = (AsynchContext) responseQueue.get();
            if (asynchContext == null) {
                break;
            } else {
                try {
                    asynchContext.checkError();
                } catch (MessageSendException e) {
                    throw e;
                } catch (RuntimeException e) {
                    throw e;
                } catch (Error e) {
                    throw e;
                } catch (Throwable th) {
                    // 起きないはず
                    throw new MessageSendException(th);
                }
            }
        }
    }
}
Also used : AsynchContext(jp.ossc.nimbus.service.queue.AsynchContext) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService) UnknownHostException(java.net.UnknownHostException)

Example 17 with AsynchContext

use of jp.ossc.nimbus.service.queue.AsynchContext in project nimbus by nimbus-org.

the class DistributedClientConnectionImpl method addSubject.

public void addSubject(String subject) throws MessageSendException {
    if (parallelRequestQueueHandlerContainer == null) {
        for (int i = 0, imax = connectionList.size(); i < imax; i++) {
            ((ClientConnection) connectionList.get(i)).addSubject(subject);
        }
    } else {
        DefaultQueueService responseQueue = new DefaultQueueService();
        try {
            responseQueue.create();
            responseQueue.start();
        } catch (Exception e) {
        }
        responseQueue.accept();
        for (int i = 0, imax = connectionList.size(); i < imax; i++) {
            AsynchContext asynchContext = new AsynchContext(new AddSubjectParallelRequest((ClientConnection) connectionList.get(i), subject), responseQueue);
            parallelRequestQueueHandlerContainer.push(asynchContext);
        }
        for (int i = 0, imax = connectionList.size(); i < imax; i++) {
            AsynchContext asynchContext = (AsynchContext) responseQueue.get();
            if (asynchContext == null) {
                break;
            } else {
                try {
                    asynchContext.checkError();
                } catch (MessageSendException e) {
                    throw e;
                } catch (RuntimeException e) {
                    throw e;
                } catch (Error e) {
                    throw e;
                } catch (Throwable th) {
                    // 起きないはず
                    throw new MessageSendException(th);
                }
            }
        }
    }
}
Also used : AsynchContext(jp.ossc.nimbus.service.queue.AsynchContext) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService) UnknownHostException(java.net.UnknownHostException)

Example 18 with AsynchContext

use of jp.ossc.nimbus.service.queue.AsynchContext in project nimbus by nimbus-org.

the class ServerConnectionImpl method send.

public synchronized void send(Message message) throws MessageSendException {
    if (!(message instanceof MessageImpl)) {
        throw new MessageSendException("Message is illegal class. " + (message == null ? null : message.getClass()));
    }
    long startTime = System.currentTimeMillis();
    try {
        Set firstClients = allocateSequence((MessageImpl) message);
        if (clients.size() == 0) {
            return;
        }
        if (multicastAddress == null) {
            if (sendQueueHandlerContainer == null) {
                List currentClients = new ArrayList();
                final Iterator clientItr = clients.iterator();
                while (clientItr.hasNext()) {
                    ClientImpl client = (ClientImpl) clientItr.next();
                    if (client == null || !client.isStartReceive() || !client.isTargetMessage(message)) {
                        continue;
                    }
                    currentClients.add(client);
                }
                Iterator itr = currentClients.iterator();
                while (itr.hasNext()) {
                    ClientImpl client = (ClientImpl) itr.next();
                    try {
                        client.send(message);
                        itr.remove();
                    } catch (MessageSendException e) {
                    }
                }
                ((MessageImpl) message).setSend(true);
                if (currentClients.size() != 0) {
                    throw new MessageSendException("Send error : clients=" + currentClients + ", message=" + message);
                }
            } else {
                final Map sendContexts = new HashMap();
                final Iterator clientItr = clients.iterator();
                while (clientItr.hasNext()) {
                    ClientImpl client = (ClientImpl) clientItr.next();
                    if (!client.isStartReceive() || !client.isTargetMessage(message)) {
                        continue;
                    }
                    SendRequest sendRequest = createSendRequest(client, (MessageImpl) message);
                    AsynchContext asynchContext = createAsynchContext(sendRequest, sendResponseQueue);
                    sendContexts.put(client, asynchContext);
                    sendQueueHandlerContainer.push(asynchContext);
                }
                Throwable th = null;
                for (int i = 0, imax = sendContexts.size(); i < imax; i++) {
                    AsynchContext asynchContext = (AsynchContext) sendResponseQueue.get();
                    SendRequest sendRequest = asynchContext == null ? null : (SendRequest) asynchContext.getInput();
                    if (asynchContext == null) {
                        Iterator itr = sendContexts.values().iterator();
                        while (itr.hasNext()) {
                            ((AsynchContext) itr.next()).cancel();
                        }
                        throw new MessageSendException("Interrupted the waiting for a response sent : clients=" + sendContexts.keySet() + ", message=" + message, new InterruptedException());
                    } else if (asynchContext.isCancel()) {
                        i--;
                    } else if (asynchContext.getThrowable() == null) {
                        sendContexts.remove(sendRequest.client);
                    } else {
                        th = asynchContext.getThrowable();
                    }
                }
                if (sendContexts.size() != 0) {
                    throw new MessageSendException("Send error : clients=" + sendContexts.keySet() + ", message=" + message, th);
                }
                ((MessageImpl) message).setSend(true);
            }
        } else {
            try {
                if (firstClients != null) {
                    Iterator firstClientItr = firstClients.iterator();
                    while (firstClientItr.hasNext()) {
                        ClientImpl client = (ClientImpl) firstClientItr.next();
                        if (client.isStartReceive()) {
                            client.send(message);
                        }
                    }
                }
                sendMessage(sendSocket, multicastAddress, (MessageImpl) message, destPort, true);
                ((MessageImpl) message).setSend(true);
            } catch (IOException e) {
                throw new MessageSendException("Send error : dest=" + multicastAddress + ':' + destPort + ", message=" + message, e);
            }
        }
    } finally {
        sendProcessTime += (System.currentTimeMillis() - startTime);
        try {
            addSendMessageCache((MessageImpl) message);
        } catch (IOException e) {
            throw new MessageSendException("Send error : message=" + message, e);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) AsynchContext(jp.ossc.nimbus.service.queue.AsynchContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 19 with AsynchContext

use of jp.ossc.nimbus.service.queue.AsynchContext in project nimbus by nimbus-org.

the class DistributedSharedContextService method putAllAsynch.

public void putAllAsynch(Map t) throws SharedContextSendException {
    Iterator entries = t.entrySet().iterator();
    Map distMap = new HashMap();
    while (entries.hasNext()) {
        Map.Entry entry = (Map.Entry) entries.next();
        SharedContext context = selectDistributeContext(entry.getKey());
        Map map = (Map) distMap.get(context);
        if (map == null) {
            map = new HashMap();
            distMap.put(context, map);
        }
        map.put(entry.getKey(), entry.getValue());
    }
    if (parallelRequestQueueHandlerContainer == null) {
        entries = distMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            ((SharedContext) entry.getKey()).putAllAsynch((Map) entry.getValue());
        }
    } else {
        DefaultQueueService responseQueue = new DefaultQueueService();
        try {
            responseQueue.create();
            responseQueue.start();
        } catch (Exception e) {
        }
        responseQueue.accept();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            AsynchContext asynchContext = new AsynchContext(new PutAllAsynchParallelRequest((SharedContext) entry.getKey(), (Map) entry.getValue()), responseQueue);
            parallelRequestQueueHandlerContainer.push(asynchContext);
        }
        for (int i = 0; i < sharedContextArray.length; i++) {
            AsynchContext asynchContext = (AsynchContext) responseQueue.get();
            if (asynchContext == null) {
                break;
            } else {
                try {
                    asynchContext.checkError();
                } catch (SharedContextSendException e) {
                    throw e;
                } catch (Error e) {
                    throw e;
                } catch (Throwable th) {
                    // 起きないはず
                    throw new SharedContextSendException(th);
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) EvaluateException(jp.ossc.nimbus.service.interpreter.EvaluateException) RequestTimeoutException(jp.ossc.nimbus.service.publish.RequestTimeoutException) IndexNotFoundException(jp.ossc.nimbus.beans.IndexNotFoundException) MessageException(jp.ossc.nimbus.service.publish.MessageException) IndexPropertyAccessException(jp.ossc.nimbus.beans.IndexPropertyAccessException) Iterator(java.util.Iterator) AsynchContext(jp.ossc.nimbus.service.queue.AsynchContext) CacheMap(jp.ossc.nimbus.service.cache.CacheMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with AsynchContext

use of jp.ossc.nimbus.service.queue.AsynchContext in project nimbus by nimbus-org.

the class DistributedSharedContextService method containsValue.

public boolean containsValue(Object value, long timeout) throws SharedContextSendException, SharedContextTimeoutException {
    if (parallelRequestQueueHandlerContainer == null) {
        for (int i = 0; i < sharedContextArray.length; i++) {
            long start = System.currentTimeMillis();
            final boolean isNoTimeout = timeout <= 0;
            timeout = isNoTimeout ? timeout : timeout - (System.currentTimeMillis() - start);
            if (!isNoTimeout && timeout < 0) {
                throw new SharedContextTimeoutException("nodeSize=" + sharedContextArray.length + ", responseCount=" + i);
            }
            if (sharedContextArray[i].containsValue(value, timeout)) {
                return true;
            }
        }
    } else {
        DefaultQueueService responseQueue = new DefaultQueueService();
        try {
            responseQueue.create();
            responseQueue.start();
        } catch (Exception e) {
        }
        responseQueue.accept();
        for (int i = 0; i < sharedContextArray.length; i++) {
            AsynchContext asynchContext = new AsynchContext(new ContainsValueParallelRequest(sharedContextArray[i], value, timeout), responseQueue);
            parallelRequestQueueHandlerContainer.push(asynchContext);
        }
        for (int i = 0; i < sharedContextArray.length; i++) {
            AsynchContext asynchContext = (AsynchContext) responseQueue.get();
            if (asynchContext == null) {
                break;
            } else {
                try {
                    asynchContext.checkError();
                } catch (SharedContextSendException e) {
                    throw e;
                } catch (SharedContextTimeoutException e) {
                    throw e;
                } catch (Error e) {
                    throw e;
                } catch (Throwable th) {
                    // 起きないはず
                    throw new SharedContextSendException(th);
                }
                if (((Boolean) asynchContext.getOutput()).booleanValue()) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : AsynchContext(jp.ossc.nimbus.service.queue.AsynchContext) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) EvaluateException(jp.ossc.nimbus.service.interpreter.EvaluateException) RequestTimeoutException(jp.ossc.nimbus.service.publish.RequestTimeoutException) IndexNotFoundException(jp.ossc.nimbus.beans.IndexNotFoundException) MessageException(jp.ossc.nimbus.service.publish.MessageException) IndexPropertyAccessException(jp.ossc.nimbus.beans.IndexPropertyAccessException)

Aggregations

AsynchContext (jp.ossc.nimbus.service.queue.AsynchContext)25 DefaultQueueService (jp.ossc.nimbus.service.queue.DefaultQueueService)20 MessageSendException (jp.ossc.nimbus.service.publish.MessageSendException)13 MessageException (jp.ossc.nimbus.service.publish.MessageException)11 IndexNotFoundException (jp.ossc.nimbus.beans.IndexNotFoundException)10 IndexPropertyAccessException (jp.ossc.nimbus.beans.IndexPropertyAccessException)10 EvaluateException (jp.ossc.nimbus.service.interpreter.EvaluateException)10 RequestTimeoutException (jp.ossc.nimbus.service.publish.RequestTimeoutException)10 UnknownHostException (java.net.UnknownHostException)8 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 Set (java.util.Set)3 CacheMap (jp.ossc.nimbus.service.cache.CacheMap)2 QueueHandlerContainerService (jp.ossc.nimbus.service.queue.QueueHandlerContainerService)2 IOException (java.io.IOException)1