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());
isStartReceive = false;
} catch (SocketTimeoutException e) {
throw new MessageSendException(e);
} catch (SocketException e) {
throw new MessageSendException(e);
} catch (IOException e) {
throw new MessageSendException(e);
}
}
use of jp.ossc.nimbus.service.publish.MessageSendException in project nimbus by nimbus-org.
the class ClientConnectionImpl method startReceive.
private void startReceive(long from, boolean isRestart) throws MessageSendException {
if (socket == null) {
throw new MessageSendException("Not connected.");
}
if (!isRestart && isStartReceive) {
return;
}
try {
send(new StartReceiveMessage(from));
isStartReceive = true;
} catch (SocketTimeoutException e) {
throw new MessageSendException(e);
} catch (SocketException e) {
throw new MessageSendException(e);
} catch (IOException e) {
throw new MessageSendException(e);
}
}
use of jp.ossc.nimbus.service.publish.MessageSendException in project nimbus by nimbus-org.
the class ClientConnectionImpl method send.
private void send(ClientMessage message) throws IOException, MessageSendException {
SynchronizeMonitor responseMonitor = null;
Short reqId = null;
boolean isBye = message.getMessageType() == ClientMessage.MESSAGE_BYE;
try {
if (!isBye && isAcknowledge) {
if (requestMonitorMap == null) {
requestMonitorMap = new HashMap();
}
synchronized (requestMonitorMap) {
message.setRequestId(requestId++);
responseMonitor = new WaitSynchronizeMonitor();
responseMonitor.initMonitor();
reqId = new Short(message.getRequestId());
requestMonitorMap.put(reqId, responseMonitor);
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (externalizer == null) {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(message);
oos.flush();
} else {
externalizer.writeExternal(message, baos);
}
byte[] bytes = baos.toByteArray();
synchronized (this) {
if (socket == null) {
throw new MessageSendException("No connected.");
}
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeInt(bytes.length);
dos.write(bytes);
dos.flush();
}
if (!isBye && isAcknowledge) {
try {
if (!responseMonitor.waitMonitor(responseTimeout)) {
throw new MessageSendException("Acknowledge is timed out.");
}
} catch (InterruptedException e) {
throw new MessageSendException("Acknowledge is interrupted.", e);
}
}
} finally {
if (!isBye && isAcknowledge) {
synchronized (requestMonitorMap) {
requestMonitorMap.remove(reqId);
}
}
}
}
use of jp.ossc.nimbus.service.publish.MessageSendException in project nimbus by nimbus-org.
the class ClientConnectionImpl method connect.
public synchronized void connect(Object id) throws ConnectException {
if (socket != null) {
return;
}
isConnected = false;
InetAddress bindAddress = null;
int bindPort = 0;
try {
bindAddress = getBindAddress();
bindPort = getBindPort();
if (socketFactory == null) {
socket = new Socket(address, port, bindAddress, bindPort);
} else {
socket = socketFactory.createSocket(address, port, bindAddress, bindPort);
}
} catch (UnknownHostException e) {
throw new ConnectException("address=" + address + ", port=" + port + ", bindAddress=" + bindAddress + ", bindPort=" + bindPort, e);
} catch (NumberFormatException e) {
throw new ConnectException(e);
} catch (IOException e) {
throw new ConnectException("address=" + address + ", port=" + port + ", bindAddress=" + bindAddress + ", bindPort=" + bindPort, e);
}
try {
if (messageReceiveDaemon == null) {
messageReceiveDaemon = new Daemon(this);
messageReceiveDaemon.setDaemon(true);
messageReceiveDaemon.setName("Nimbus Publish(TCP) ClientConnection SocketReader " + socket.getLocalSocketAddress());
messageReceiveDaemon.start();
}
this.id = id == null ? socket.getLocalSocketAddress() : id;
try {
send(new IdMessage(this.id));
} catch (IOException e) {
throw new ConnectException(e);
} catch (MessageSendException e) {
throw new ConnectException(e);
}
if (serviceManagerName != null && serverServiceName != null) {
ServiceManager manager = ServiceManagerFactory.findManager(serviceManagerName);
if (manager != null) {
final ClientConnectionService ccs = new ClientConnectionService();
try {
String name = serverServiceName.getServiceName() + '$' + socket.getLocalSocketAddress();
name = name.replaceAll(":", "\\$");
if (!manager.isRegisteredService(name) && manager.registerService(name, ccs)) {
serviceName = ccs.getServiceNameObject();
manager.createService(ccs.getServiceName());
manager.startService(ccs.getServiceName());
}
} catch (Exception e) {
throw new ConnectException(e);
}
}
}
} catch (ConnectException e) {
if (socket != null) {
try {
socket.close();
} catch (IOException e2) {
}
socket = null;
}
throw e;
}
isConnected = true;
isServerClosed = false;
}
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 {
long startTime = System.currentTimeMillis();
if (clients.size() == 0) {
addSendMessageCache((MessageImpl) message);
return;
}
try {
if (sendQueueHandlerContainer == null) {
List currentClients = new ArrayList();
final Iterator clientItr = clients.iterator();
while (clientItr.hasNext()) {
ClientImpl client = (ClientImpl) clientItr.next();
if (!client.isStartReceive() || !client.isTargetMessage(message)) {
continue;
}
currentClients.add(client);
}
int retryCount = -1;
while (currentClients.size() != 0 && retryCount < maxSendRetryCount) {
Iterator itr = currentClients.iterator();
while (itr.hasNext()) {
ClientImpl client = (ClientImpl) itr.next();
try {
client.send(message);
itr.remove();
} catch (MessageSendException e) {
if (logger != null) {
if ((retryCount + 1) >= maxSendRetryCount) {
if (sendErrorRetryOverMessageId != null) {
logger.write(sendErrorRetryOverMessageId, new Object[] { client, message }, e);
}
} else {
if (sendErrorMessageId != null) {
logger.write(sendErrorMessageId, new Object[] { client, message }, e);
}
}
}
}
}
retryCount++;
}
if (currentClients.size() != 0) {
throw new MessageSendException("Send error : clients=" + currentClients + ", message=" + message);
}
((MessageImpl) message).setSend(true);
} 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();
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--;
continue;
} else if (asynchContext.getThrowable() == null) {
sendContexts.remove(((SendRequest) asynchContext.getInput()).client);
} else {
th = asynchContext.getThrowable();
}
recycleAsynchContext(asynchContext);
}
if (sendContexts.size() != 0) {
throw new MessageSendException("Send error : clients=" + sendContexts.keySet() + ", message=" + message, th);
}
((MessageImpl) message).setSend(true);
}
} finally {
addSendMessageCache((MessageImpl) message);
sendProcessTime += (System.currentTimeMillis() - startTime);
}
}
Aggregations