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