Search in sources :

Example 16 with MessageSendException

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);
    }
}
Also used : SocketException(java.net.SocketException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 17 with MessageSendException

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);
    }
}
Also used : SocketException(java.net.SocketException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 18 with MessageSendException

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);
            }
        }
    }
}
Also used : WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor) SynchronizeMonitor(jp.ossc.nimbus.util.SynchronizeMonitor) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 19 with MessageSendException

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) SocketException(java.net.SocketException) MessageCommunicateException(jp.ossc.nimbus.service.publish.MessageCommunicateException) ConnectException(jp.ossc.nimbus.service.publish.ConnectException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) MessageException(jp.ossc.nimbus.service.publish.MessageException) EOFException(java.io.EOFException) UnknownHostException(java.net.UnknownHostException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Daemon(jp.ossc.nimbus.daemon.Daemon) ServiceManager(jp.ossc.nimbus.core.ServiceManager) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ConnectException(jp.ossc.nimbus.service.publish.ConnectException)

Example 20 with MessageSendException

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);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) AsynchContext(jp.ossc.nimbus.service.queue.AsynchContext) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

MessageSendException (jp.ossc.nimbus.service.publish.MessageSendException)32 Message (jp.ossc.nimbus.service.publish.Message)15 HashSet (java.util.HashSet)14 Set (java.util.Set)14 IOException (java.io.IOException)13 MessageException (jp.ossc.nimbus.service.publish.MessageException)13 SocketException (java.net.SocketException)9 SocketTimeoutException (java.net.SocketTimeoutException)9 LinkedHashSet (java.util.LinkedHashSet)9 RequestTimeoutException (jp.ossc.nimbus.service.publish.RequestTimeoutException)9 HashMap (java.util.HashMap)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Iterator (java.util.Iterator)4 ConnectException (jp.ossc.nimbus.service.publish.ConnectException)4 AsynchContext (jp.ossc.nimbus.service.queue.AsynchContext)4 DefaultQueueService (jp.ossc.nimbus.service.queue.DefaultQueueService)4 EOFException (java.io.EOFException)3 Map (java.util.Map)3 MessageCreateException (jp.ossc.nimbus.service.publish.MessageCreateException)3