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) throws Exception {
if (sendThreadSize >= 2) {
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 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.publish.MessageSendException in project nimbus by nimbus-org.
the class ClientConnectionImpl method addSubject.
public void addSubject(String subject, String[] keys) throws MessageSendException {
if (socket == null) {
throw new MessageSendException("Not connected.");
}
if (subject == null) {
return;
}
try {
send(new AddMessage(subject, keys));
} catch (SocketTimeoutException e) {
throw new MessageSendException(e);
} catch (SocketException e) {
throw new MessageSendException(e);
} catch (IOException e) {
throw new MessageSendException(e);
}
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 ClientConnectionImpl method stopReceive.
public void stopReceive() throws MessageSendException {
if (socket == null) {
throw new MessageSendException("Not connected.");
}
if (!isStartReceive) {
return;
}
try {
send(new StopReceiveMessage(), isAcknowledge);
isStartReceive = false;
} catch (SocketTimeoutException e) {
throw new MessageSendException(e);
} catch (SocketException e) {
throw new MessageSendException(e);
} catch (IOException e) {
throw new MessageSendException(e);
} catch (ClassNotFoundException e) {
throw new MessageSendException(e);
}
}
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;
}
((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).reset();
int tryCount = 0;
boolean isSuccess = false;
while (!isSuccess) {
tryCount++;
try {
connect(id, true);
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;
MessageId latestMessageId = ((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).getLatestMessageId();
if (latestMessageId != null) {
time = ((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).getLatestMessageReceiveTime() - 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