use of jp.ossc.nimbus.service.publish.MessageException 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);
}
}
}
use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.
the class DistributedSharedContextService method createResponseMessage.
protected Message createResponseMessage(String responseSubject, String responseKey, Object response) {
Message result = null;
try {
result = serverConnection.createMessage(responseSubject, responseKey);
result.setObject(response);
} catch (MessageException e) {
getLogger().write("DSCS_00001", new Object[] { isClient ? clientSubject : subject, responseSubject, responseKey, response }, e);
}
return result;
}
use of jp.ossc.nimbus.service.publish.MessageException 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);
}
}
}
use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.
the class DistributedSharedContextService method onRequestMessage.
public Message onRequestMessage(Object sourceId, int sequence, Message message, String responseSubject, String responseKey) {
DistributedSharedContextEvent event = null;
try {
event = (DistributedSharedContextEvent) message.getObject();
} catch (MessageException e) {
return null;
}
Message result = null;
switch(event.type) {
case DistributedSharedContextEvent.EVENT_GET_DIST_INFO:
result = onGetDistributeInfo(event, responseSubject, responseKey);
break;
case DistributedSharedContextEvent.EVENT_REHASH:
result = onRehash(event, sourceId, sequence, responseSubject, responseKey);
break;
case DistributedSharedContextEvent.EVENT_REHASH_REQUEST:
onRehashRequest(event, sourceId, sequence, responseSubject, responseKey);
break;
case DistributedSharedContextEvent.EVENT_SAVE:
result = onSave(event, sourceId, sequence, responseSubject, responseKey);
break;
case DistributedSharedContextEvent.EVENT_LOAD:
result = onLoad(event, sourceId, sequence, responseSubject, responseKey);
break;
case DistributedSharedContextEvent.EVENT_LOAD_KEY:
result = onLoadKey(event, sourceId, sequence, responseSubject, responseKey);
break;
case DistributedSharedContextEvent.EVENT_REHASH_SWITCH:
result = onRehashSwitch(event, responseSubject, responseKey);
break;
default:
}
return result;
}
use of jp.ossc.nimbus.service.publish.MessageException 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);
}
}
}
Aggregations