Search in sources :

Example 1 with MessageSendException

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

the class DistributedSharedContextService method onSave.

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

            public void run() {
                Message response = null;
                try {
                    Object param = event.value;
                    if (param instanceof Long) {
                        long timeout = ((Long) param).longValue();
                        if (contextStore != null) {
                            if (isClearBeforeSave) {
                                contextStore.clear();
                            }
                            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();
                                }
                                sharedContextArray[i].save(timeout);
                            }
                        } else {
                            throw new UnsupportedOperationException();
                        }
                    } else {
                        Object key = ((Object[]) param)[0];
                        long timeout = ((Long) ((Object[]) param)[1]).longValue();
                        if (contextStore != null) {
                            sharedContextArray[getDataNodeIndex(key)].save(key, timeout);
                        } else {
                            throw new UnsupportedOperationException();
                        }
                    }
                    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);
                }
            }
        };
        saveThread.start();
    }
    return null;
}
Also used : MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Message(jp.ossc.nimbus.service.publish.Message)

Example 2 with MessageSendException

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

the class DistributedSharedContextService method save.

public void save(Object key, long timeout) throws Exception {
    if (isMain()) {
        if (contextStore != null) {
            sharedContextArray[getDataNodeIndex(key)].save(key, timeout);
        } else {
            throw new UnsupportedOperationException();
        }
    } else {
        try {
            Message message = serverConnection.createMessage(subject, key == null ? null : key.toString());
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() != 0) {
                message.setObject(new DistributedSharedContextEvent(DistributedSharedContextEvent.EVENT_SAVE, new Object[] { key, new Long(timeout) }));
                try {
                    Message[] responses = serverConnection.request(message, isClient ? clientSubject : subject, null, 1, timeout);
                    Object ret = responses[0].getObject();
                    responses[0].recycle();
                    if (ret instanceof Throwable) {
                        throw new SharedContextSendException((Throwable) ret);
                    }
                } catch (RequestTimeoutException e) {
                    throw new SharedContextTimeoutException(e);
                }
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        }
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Message(jp.ossc.nimbus.service.publish.Message) RequestTimeoutException(jp.ossc.nimbus.service.publish.RequestTimeoutException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) MessageException(jp.ossc.nimbus.service.publish.MessageException)

Example 3 with MessageSendException

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

the class DistributedSharedContextService method onLoad.

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

            public void run() {
                Message response = null;
                try {
                    if (contextStore != null) {
                        if (event.value == null) {
                            contextStore.load(DistributedSharedContextService.this);
                        } else {
                            contextStore.load(DistributedSharedContextService.this, event.value);
                        }
                    } else {
                        throw new UnsupportedOperationException();
                    }
                    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);
                }
            }
        };
        loadThread.start();
    }
    return null;
}
Also used : MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Message(jp.ossc.nimbus.service.publish.Message)

Example 4 with MessageSendException

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

the class DistributedSharedContextService method rehash.

public synchronized void rehash(long timeout) throws SharedContextSendException, SharedContextTimeoutException {
    if (!isRehashEnabled) {
        return;
    }
    if (isMain()) {
        try {
            Message message = serverConnection.createMessage(subject, Integer.toString(DistributedSharedContextEvent.EVENT_GET_DIST_INFO));
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() == 0) {
                DistributeGrid grid = new DistributeGrid();
                grid.addDistributeInfo(distributeInfo);
                grid.rehash();
                distributeInfo.apply(distributeInfo, sharedContextArray);
            } else {
                message.setObject(new DistributedSharedContextEvent(DistributedSharedContextEvent.EVENT_GET_DIST_INFO, new Long(timeout)));
                long start = System.currentTimeMillis();
                Message[] responses = null;
                try {
                    responses = serverConnection.request(message, 0, timeout);
                } catch (RequestTimeoutException e) {
                    throw new SharedContextTimeoutException("Timeout has occurred to get state of distribution.", e);
                }
                final boolean isNoTimeout = timeout <= 0;
                timeout = isNoTimeout ? timeout : timeout - (System.currentTimeMillis() - start);
                DistributeGrid grid = new DistributeGrid();
                grid.addDistributeInfo(distributeInfo);
                for (int i = 0; i < responses.length; i++) {
                    grid.addDistributeInfo((DistributeInfo) responses[i].getObject());
                    responses[i].recycle();
                }
                grid.rehash();
                RehashResponseCallBack callback = new RehashResponseCallBack();
                Map increaseDistributeInfos = grid.getIncreaseDistributeInfos();
                DistributeInfo info = (DistributeInfo) increaseDistributeInfos.remove(getId());
                if (info != null) {
                    info.apply(distributeInfo, sharedContextArray);
                }
                if (increaseDistributeInfos.size() != 0) {
                    callback.setResponseCount(increaseDistributeInfos.size());
                    Iterator infos = increaseDistributeInfos.values().iterator();
                    while (infos.hasNext()) {
                        info = (DistributeInfo) infos.next();
                        Message rehashMessage = serverConnection.createMessage(subject, Integer.toString(DistributedSharedContextEvent.EVENT_REHASH));
                        rehashMessage.setObject(new DistributedSharedContextEvent(DistributedSharedContextEvent.EVENT_REHASH, info));
                        rehashMessage.addDestinationId(info.getId());
                        serverConnection.request(rehashMessage, 1, timeout, callback);
                    }
                    callback.waitResponse(timeout);
                }
                Map decreaseDistributeInfos = grid.getDecreaseDistributeInfos();
                info = (DistributeInfo) decreaseDistributeInfos.remove(getId());
                if (info != null) {
                    info.apply(distributeInfo, sharedContextArray);
                }
                if (decreaseDistributeInfos.size() != 0) {
                    callback.setResponseCount(decreaseDistributeInfos.size());
                    Iterator infos = decreaseDistributeInfos.values().iterator();
                    while (infos.hasNext()) {
                        info = (DistributeInfo) infos.next();
                        Message rehashMessage = serverConnection.createMessage(subject, Integer.toString(DistributedSharedContextEvent.EVENT_REHASH));
                        rehashMessage.setObject(new DistributedSharedContextEvent(DistributedSharedContextEvent.EVENT_REHASH, info));
                        rehashMessage.addDestinationId(info.getId());
                        serverConnection.request(rehashMessage, 1, timeout, callback);
                    }
                    callback.waitResponse(timeout);
                }
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        }
    } else {
        try {
            Message message = serverConnection.createMessage(subject, Integer.toString(DistributedSharedContextEvent.EVENT_REHASH_REQUEST));
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() != 0) {
                message.setObject(new DistributedSharedContextEvent(DistributedSharedContextEvent.EVENT_REHASH_REQUEST, new Long(timeout)));
                try {
                    Message[] responses = serverConnection.request(message, isClient ? clientSubject : subject, null, 1, timeout);
                    Object ret = responses[0].getObject();
                    responses[0].recycle();
                    if (ret instanceof Throwable) {
                        throw new SharedContextSendException((Throwable) ret);
                    }
                } catch (RequestTimeoutException e) {
                    throw new SharedContextTimeoutException(e);
                }
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        }
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Message(jp.ossc.nimbus.service.publish.Message) RequestTimeoutException(jp.ossc.nimbus.service.publish.RequestTimeoutException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) MessageException(jp.ossc.nimbus.service.publish.MessageException) Iterator(java.util.Iterator) CacheMap(jp.ossc.nimbus.service.cache.CacheMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with MessageSendException

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

the class DistributedSharedContextService method load.

public void load(Object key, long timeout) throws Exception {
    if (isMain()) {
        if (contextStore != null) {
            contextStore.load(this, key);
        } else {
            throw new UnsupportedOperationException();
        }
    } else {
        try {
            Message message = serverConnection.createMessage(subject, key == null ? null : key.toString());
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() != 0) {
                message.setObject(new DistributedSharedContextEvent(DistributedSharedContextEvent.EVENT_LOAD, key));
                try {
                    Message[] responses = serverConnection.request(message, isClient ? clientSubject : subject, null, 1, timeout);
                    Object ret = responses[0].getObject();
                    responses[0].recycle();
                    if (ret instanceof Throwable) {
                        throw new SharedContextSendException((Throwable) ret);
                    }
                } catch (RequestTimeoutException e) {
                    throw new SharedContextTimeoutException(e);
                }
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        }
    }
}
Also used : RequestTimeoutException(jp.ossc.nimbus.service.publish.RequestTimeoutException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Message(jp.ossc.nimbus.service.publish.Message) MessageException(jp.ossc.nimbus.service.publish.MessageException)

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