Search in sources :

Example 11 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, 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();
    }
}
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 12 with MessageSendException

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;
}
Also used : Message(jp.ossc.nimbus.service.publish.Message) SharedContextSendException(jp.ossc.nimbus.service.context.SharedContextSendException) RequestServerConnection(jp.ossc.nimbus.service.publish.RequestServerConnection) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException)

Example 13 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 (!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]);
        }
    }
}
Also used : MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 14 with MessageSendException

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

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