Search in sources :

Example 11 with Message

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

the class SharedQueueService method onRemove.

protected Message onRemove(final SharedContextEvent event, final Object sourceId, final int sequence, final String responseSubject, final String responseKey) {
    Message ret = super.onRemove(event, sourceId, sequence, responseSubject, responseKey);
    getAfter();
    return ret;
}
Also used : Message(jp.ossc.nimbus.service.publish.Message)

Example 12 with Message

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

the class SharedQueueService method onRequestMessage.

public Message onRequestMessage(Object sourceId, int sequence, Message message, String responseSubject, String responseKey) {
    SharedContextEvent event = null;
    try {
        event = (SharedContextEvent) message.getObject();
    } catch (MessageException e) {
        e.printStackTrace();
        message.recycle();
        return null;
    }
    Message result = null;
    switch(event.type) {
        case SharedQueueEvent.EVENT_LOCK_FIRST:
            message.recycle();
            result = onLockFirst(event, sourceId, sequence, responseSubject, responseKey);
            break;
        default:
            result = super.onRequestMessage(sourceId, sequence, message, responseSubject, responseKey);
    }
    return result;
}
Also used : Message(jp.ossc.nimbus.service.publish.Message) MessageException(jp.ossc.nimbus.service.publish.MessageException)

Example 13 with Message

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

the class SharedQueueService method onLockFirst.

protected Message onLockFirst(SharedContextEvent event, final Object sourceId, final int sequence, final String responseSubject, final String responseKey) {
    if (isMain(sourceId)) {
        final Object[] params = (Object[]) event.value;
        final long threadId = ((Long) params[0]).longValue();
        long timeout = ((Long) params[1]).longValue();
        Object[] keys = null;
        if (context.size() != 0) {
            synchronized (context) {
                if (context.size() != 0) {
                    keys = context.keySet().toArray();
                }
            }
        }
        if (keys == null || keys.length == 0) {
            return createResponseMessage(responseSubject, responseKey, null);
        }
        for (int i = 0; i < keys.length; i++) {
            final Object key = keys[i];
            if (!containsKey(key)) {
                continue;
            }
            Lock lock = null;
            synchronized (keyLockMap) {
                lock = (Lock) keyLockMap.get(key);
                if (lock == null) {
                    lock = new Lock(key);
                    keyLockMap.put(key, lock);
                }
            }
            final long start = System.currentTimeMillis();
            if (lock.acquireForReply(sourceId, threadId, true, true, timeout, sourceId, sequence, responseSubject, responseKey)) {
                if (!containsKey(key)) {
                    lock.release(sourceId, false);
                    continue;
                }
                final boolean isNoTimeout = timeout <= 0;
                timeout = isNoTimeout ? timeout : (timeout - (System.currentTimeMillis() - start));
                if (!isNoTimeout && timeout <= 0) {
                    lock.release(sourceId, false);
                    return createResponseMessage(responseSubject, responseKey, null);
                } else {
                    try {
                        Message message = serverConnection.createMessage(subject, key.toString());
                        message.setSubject(clientSubject, key.toString());
                        final Set receiveClients = serverConnection.getReceiveClientIds(message);
                        receiveClients.remove(sourceId);
                        if (receiveClients.size() != 0) {
                            message.setDestinationIds(receiveClients);
                            message.setObject(new SharedContextEvent(SharedContextEvent.EVENT_GOT_LOCK, key, new Object[] { sourceId, new Long(threadId), new Long(timeout) }));
                            final Lock lockedLock = lock;
                            serverConnection.request(message, isClient ? clientSubject : subject, key == null ? null : key.toString(), 0, timeout, new RequestServerConnection.ResponseCallBack() {

                                public void onResponse(Object fromId, Message response, boolean isLast) {
                                    if (receiveClients.size() == 0) {
                                        return;
                                    }
                                    try {
                                        if (response == null) {
                                            unlock(key);
                                            serverConnection.response(sourceId, sequence, createResponseMessage(responseSubject, responseKey, null));
                                            receiveClients.clear();
                                            return;
                                        }
                                        receiveClients.remove(fromId);
                                        Object ret = response.getObject();
                                        response.recycle();
                                        if (ret == null || ret instanceof Throwable || !((Boolean) ret).booleanValue()) {
                                            unlock(key);
                                            serverConnection.response(sourceId, sequence, createResponseMessage(responseSubject, responseKey, null));
                                            receiveClients.clear();
                                        } else if (isLast) {
                                            serverConnection.response(sourceId, sequence, createResponseMessage(responseSubject, responseKey, key));
                                        }
                                    } catch (Throwable th) {
                                        try {
                                            unlock(key);
                                        } catch (SharedContextSendException e) {
                                            getLogger().write("SCS__00007", new Object[] { isClient ? clientSubject : subject, key }, e);
                                        }
                                        try {
                                            serverConnection.response(sourceId, sequence, createResponseMessage(responseSubject, responseKey, th));
                                        } catch (MessageSendException e) {
                                            getLogger().write("SCS__00006", new Object[] { isClient ? clientSubject : subject, key }, e);
                                        }
                                    }
                                }
                            });
                            return null;
                        } else {
                            return createResponseMessage(responseSubject, responseKey, key);
                        }
                    } catch (Throwable th) {
                        try {
                            unlock(key);
                        } catch (SharedContextSendException e) {
                            getLogger().write("SCS__00007", new Object[] { isClient ? clientSubject : subject, key }, e);
                        }
                        return createResponseMessage(responseSubject, responseKey, th);
                    }
                }
            }
        }
        return createResponseMessage(responseSubject, responseKey, null);
    }
    return null;
}
Also used : Message(jp.ossc.nimbus.service.publish.Message) SharedContextSendException(jp.ossc.nimbus.service.context.SharedContextSendException) RequestServerConnection(jp.ossc.nimbus.service.publish.RequestServerConnection) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException)

Example 14 with Message

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

the class SharedQueueService method onClear.

protected Message onClear(final SharedContextEvent event, final Object sourceId, final int sequence, final String responseSubject, final String responseKey) {
    Message ret = super.onClear(event, sourceId, sequence, responseSubject, responseKey);
    getAfter();
    return ret;
}
Also used : Message(jp.ossc.nimbus.service.publish.Message)

Example 15 with Message

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

the class ServerConnectionImpl method castMessage.

public Message castMessage(Message message) throws MessageException {
    if (message instanceof MessageImpl) {
        return message;
    }
    Message msg = createMessage(message.getSubject(), message.getKey());
    msg.setObject(message.getObject());
    return msg;
}
Also used : Message(jp.ossc.nimbus.service.publish.Message)

Aggregations

Message (jp.ossc.nimbus.service.publish.Message)25 MessageSendException (jp.ossc.nimbus.service.publish.MessageSendException)15 MessageException (jp.ossc.nimbus.service.publish.MessageException)14 HashSet (java.util.HashSet)10 LinkedHashSet (java.util.LinkedHashSet)10 Set (java.util.Set)10 RequestTimeoutException (jp.ossc.nimbus.service.publish.RequestTimeoutException)9 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SharedContextSendException (jp.ossc.nimbus.service.context.SharedContextSendException)2 BufferedReader (java.io.BufferedReader)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Iterator (java.util.Iterator)1 IndexNotFoundException (jp.ossc.nimbus.beans.IndexNotFoundException)1 IndexPropertyAccessException (jp.ossc.nimbus.beans.IndexPropertyAccessException)1 CacheMap (jp.ossc.nimbus.service.cache.CacheMap)1 NoConnectServerException (jp.ossc.nimbus.service.context.NoConnectServerException)1 SharedContextTimeoutException (jp.ossc.nimbus.service.context.SharedContextTimeoutException)1 EvaluateException (jp.ossc.nimbus.service.interpreter.EvaluateException)1