Search in sources :

Example 21 with MessageSendException

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();
    }
}
Also used : DistributedQueueHandlerContainerService(jp.ossc.nimbus.service.queue.DistributedQueueHandlerContainerService) QueueHandlerContainerService(jp.ossc.nimbus.service.queue.QueueHandlerContainerService) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) MessageCreateException(jp.ossc.nimbus.service.publish.MessageCreateException) EOFException(java.io.EOFException) CancelledKeyException(java.nio.channels.CancelledKeyException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) MessageException(jp.ossc.nimbus.service.publish.MessageException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 22 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 {
    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);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) 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)

Example 23 with MessageSendException

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

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

Example 25 with MessageSendException

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;
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) List(java.util.List) ArrayList(java.util.ArrayList) ConnectException(jp.ossc.nimbus.service.publish.ConnectException)

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