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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations