use of jp.ossc.nimbus.service.publish.MessageSendException in project nimbus by nimbus-org.
the class ServerConnectionImpl method initSend.
private void initSend(ServiceName sendQueueServiceName, int sendThreadSize, boolean isMulticast) throws Exception {
if (!isMulticast && sendThreadSize >= 1) {
sendQueueHandlerContainer = new QueueHandlerContainerService();
sendQueueHandlerContainer.create();
if (sendQueueServiceName == null) {
DefaultQueueService sendQueue = new DefaultQueueService();
sendQueue.create();
sendQueue.start();
sendQueueHandlerContainer.setQueueService(sendQueue);
} else {
sendQueueHandlerContainer.setQueueServiceName(sendQueueServiceName);
}
sendQueueHandlerContainer.setQueueHandlerSize(sendThreadSize);
sendQueueHandlerContainer.setQueueHandler(new SendQueueHandler());
sendQueueHandlerContainer.setIgnoreNullElement(true);
sendQueueHandlerContainer.setWaitTimeout(1000l);
sendQueueHandlerContainer.start();
sendResponseQueue = new DefaultQueueService();
try {
sendResponseQueue.create();
sendResponseQueue.start();
} catch (Exception e) {
throw new MessageSendException(e);
}
sendResponseQueue.accept();
}
}
use of jp.ossc.nimbus.service.publish.MessageSendException 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.MessageSendException in project nimbus by nimbus-org.
the class ClientConnectionImpl method addSubject.
public void addSubject(String subject, String[] keys) throws MessageSendException {
if (!isConnected) {
throw new MessageSendException("Not connected.");
}
if (subject == null) {
return;
}
serverConnection.addSubject(this.id, subject, keys);
if (subjects == null) {
subjects = Collections.synchronizedMap(new HashMap());
}
Set keySet = (Set) subjects.get(subject);
if (keySet == null) {
keySet = Collections.synchronizedSet(new HashSet());
subjects.put(subject, keySet);
}
if (keys == null) {
keySet.add(null);
} else {
for (int i = 0; i < keys.length; i++) {
keySet.add(keys[i]);
}
}
}
use of jp.ossc.nimbus.service.publish.MessageSendException in project nimbus by nimbus-org.
the class ServerConnectionImpl method send.
public void send(Message message) throws MessageSendException {
addSendMessageCache((MessageImpl) message);
if (clients.size() == 0) {
return;
}
ClientImpl[] clientArray = (ClientImpl[]) clients.values().toArray(new ClientImpl[clients.size()]);
if (sendQueueHandlerContainer == null) {
for (int i = 0; i < clientArray.length; i++) {
if (!clientArray[i].isStartReceive() || !clientArray[i].isTargetMessage(message)) {
continue;
}
clientArray[i].send((MessageImpl) message);
}
} else {
DefaultQueueService responseQueue = new DefaultQueueService();
try {
responseQueue.create();
responseQueue.start();
} catch (Exception e) {
throw new MessageSendException(e);
}
responseQueue.accept();
for (int i = 0; i < clientArray.length; i++) {
if (!clientArray[i].isStartReceive() || !clientArray[i].isTargetMessage(message)) {
clientArray[i] = null;
continue;
}
sendQueueHandlerContainer.push(new AsynchContext(new SendRequest(clientArray[i], (MessageImpl) message), responseQueue));
}
List errorClients = new ArrayList();
for (int i = 0; i < clientArray.length; i++) {
if (clientArray[i] == null) {
continue;
}
AsynchContext asynchContext = (AsynchContext) responseQueue.get();
if (asynchContext.getThrowable() != null) {
errorClients.add(((SendRequest) asynchContext.getInput()).client);
}
}
if (errorClients.size() != 0) {
throw new MessageSendException("Send error : clients=" + errorClients + ", message=" + message);
}
}
}
use of jp.ossc.nimbus.service.publish.MessageSendException in project nimbus by nimbus-org.
the class ClientConnectionImpl method reconnect.
private void reconnect() throws ConnectException, MessageSendException {
boolean isNowReconnecting = isReconnecting;
synchronized (this) {
if (isNowReconnecting) {
return;
}
isReconnecting = true;
try {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
socket = null;
}
int tryCount = 0;
boolean isSuccess = false;
while (!isSuccess) {
tryCount++;
try {
connect(id);
if (subjects != null) {
Object[] subjectArray = subjects.keySet().toArray();
for (int i = 0; i < subjectArray.length; i++) {
Object subject = subjectArray[i];
Set keySet = (Set) subjects.get(subject);
if (keySet != null) {
String[] keys = (String[]) keySet.toArray(new String[keySet.size()]);
boolean containsNull = false;
List keyList = new ArrayList();
for (int j = 0; j < keys.length; j++) {
if (keys[j] == null) {
containsNull = true;
} else {
keyList.add(keys[j]);
}
}
if (containsNull) {
addSubject((String) subject);
keys = (String[]) keyList.toArray(new String[keyList.size()]);
}
if (keys != null && keys.length != 0) {
addSubject((String) subject, keys);
}
}
}
}
if (isStartReceive) {
long time = -1;
if (latestMessage != null) {
time = latestMessage.getReceiveTime() - reconnectBufferTime;
}
startReceive(time, true);
}
isSuccess = true;
} catch (ConnectException e) {
if (tryCount >= reconnectCount) {
throw e;
} else {
if (receiveWarnMessageId != null) {
ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
}
}
} catch (MessageSendException e) {
if (tryCount >= reconnectCount) {
throw e;
} else {
if (receiveWarnMessageId != null) {
ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
}
}
}
if (!isSuccess && reconnectInterval > 0) {
try {
Thread.sleep(reconnectInterval);
} catch (InterruptedException e) {
throw new ConnectException(e);
}
}
}
} finally {
isReconnecting = false;
}
}
}
Aggregations