Search in sources :

Example 11 with MessageException

use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.

the class MessageImpl method getObject.

public Object getObject() throws MessageException {
    if (object == null && serializedBytes != null) {
        synchronized (serializedBytes) {
            if (object != null) {
                return object;
            }
            try {
                ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedBytes));
                object = ois.readObject();
            } catch (IOException e) {
                throw new MessageException(e);
            } catch (ClassNotFoundException e) {
                throw new MessageException(e);
            }
        }
    }
    return object;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MessageException(jp.ossc.nimbus.service.publish.MessageException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 12 with MessageException

use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.

the class ClientConnectionImpl method receive.

private Message receive() throws MessageCommunicateException {
    if (socket == null) {
        return null;
    }
    int length = 0;
    try {
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        length = dis.readInt();
        if (length <= 0) {
            return null;
        }
        if (receiveBytes == null || receiveBytes.length < length) {
            receiveBytes = new byte[length];
        }
        dis.readFully(receiveBytes, 0, length);
        ByteArrayInputStream bais = new ByteArrayInputStream(receiveBytes, 0, length);
        MessageImpl message = MessageImpl.read(bais, externalizer, messageBuffer);
        message.setClientConnection(this);
        if (message != null) {
            final short messageType = message.getMessageType();
            switch(messageType) {
                case MessageImpl.MESSAGE_TYPE_SERVER_CLOSE:
                    if (serverCloseMessageId != null) {
                        ServiceManagerFactory.getLogger().write(serverCloseMessageId, new Object[] { this });
                    }
                    isServerClosed = true;
                    close();
                    return null;
                case MessageImpl.MESSAGE_TYPE_SERVER_RESPONSE:
                    if (isAcknowledge && requestMonitorMap != null) {
                        synchronized (requestMonitorMap) {
                            Object reqId = null;
                            try {
                                reqId = message.getObject();
                            } catch (MessageException e) {
                                return null;
                            }
                            SynchronizeMonitor responseMonitor = (SynchronizeMonitor) requestMonitorMap.get(reqId);
                            if (responseMonitor != null) {
                                responseMonitor.notifyAllMonitor();
                            }
                        }
                    }
                    return null;
                case MessageImpl.MESSAGE_TYPE_APPLICATION:
                default:
                    break;
            }
        }
        return message;
    } catch (SocketTimeoutException e) {
        return null;
    } catch (SocketException e) {
        if (isClosing || !isConnected) {
            return null;
        }
        if (reconnectCount > 0) {
            if (receiveWarnMessageId != null) {
                ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
            }
            reconnect();
            return receive();
        } else {
            close();
            return null;
        }
    } catch (EOFException e) {
        if (isClosing || !isConnected) {
            return null;
        }
        if (reconnectCount > 0) {
            if (receiveWarnMessageId != null) {
                ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
            }
            reconnect();
            return receive();
        } else if (length == 0) {
            close();
            return null;
        } else {
            throw new MessageCommunicateException("length=" + length + ", receiveBytes=" + (receiveBytes == null ? "null" : Integer.toString(receiveBytes.length)), e);
        }
    } catch (IOException e) {
        if (isClosing || !isConnected) {
            return null;
        }
        throw new MessageCommunicateException(e);
    } catch (ClassNotFoundException e) {
        if (isClosing || !isConnected) {
            return null;
        }
        throw new MessageCommunicateException(e);
    }
}
Also used : SocketException(java.net.SocketException) MessageCommunicateException(jp.ossc.nimbus.service.publish.MessageCommunicateException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) WaitSynchronizeMonitor(jp.ossc.nimbus.util.WaitSynchronizeMonitor) SynchronizeMonitor(jp.ossc.nimbus.util.SynchronizeMonitor) SocketTimeoutException(java.net.SocketTimeoutException) ByteArrayInputStream(java.io.ByteArrayInputStream) MessageException(jp.ossc.nimbus.service.publish.MessageException) EOFException(java.io.EOFException)

Example 13 with MessageException

use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.

the class DistributedSharedContextService method load.

public synchronized void load(long timeout) throws Exception {
    if (isMain()) {
        if (contextStore != null) {
            contextStore.load(this);
        } else {
            throw new UnsupportedOperationException();
        }
    } else {
        try {
            Message message = serverConnection.createMessage(subject, null);
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() != 0) {
                message.setObject(new DistributedSharedContextEvent(DistributedSharedContextEvent.EVENT_LOAD));
                try {
                    Message[] responses = serverConnection.request(message, isClient ? clientSubject : subject, null, 1, timeout);
                    Object ret = responses[0].getObject();
                    responses[0].recycle();
                    if (ret instanceof Throwable) {
                        throw new SharedContextSendException((Throwable) ret);
                    }
                } catch (RequestTimeoutException e) {
                    throw new SharedContextTimeoutException(e);
                }
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        }
    }
}
Also used : RequestTimeoutException(jp.ossc.nimbus.service.publish.RequestTimeoutException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Message(jp.ossc.nimbus.service.publish.Message) MessageException(jp.ossc.nimbus.service.publish.MessageException)

Example 14 with MessageException

use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.

the class DistributedSharedContextService method getServerMemberIdSet.

public Set getServerMemberIdSet() {
    if (serverConnection == null) {
        return new HashSet();
    }
    try {
        Message message = serverConnection.createMessage(subject, null);
        Set result = serverConnection.getReceiveClientIds(message);
        if (!isClient) {
            result.add(getId());
        }
        return result;
    } catch (MessageException e) {
        return new HashSet();
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Message(jp.ossc.nimbus.service.publish.Message) MessageException(jp.ossc.nimbus.service.publish.MessageException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 15 with MessageException

use of jp.ossc.nimbus.service.publish.MessageException in project nimbus by nimbus-org.

the class DistributedSharedContextService method loadKey.

public synchronized void loadKey(long timeout) throws Exception {
    if (isMain()) {
        if (contextStore != null) {
            contextStore.loadKey(this);
        } else {
            throw new UnsupportedOperationException();
        }
    } else {
        try {
            Message message = serverConnection.createMessage(subject, null);
            Set receiveClients = serverConnection.getReceiveClientIds(message);
            if (receiveClients.size() != 0) {
                message.setObject(new DistributedSharedContextEvent(DistributedSharedContextEvent.EVENT_LOAD_KEY));
                try {
                    Message[] responses = serverConnection.request(message, isClient ? clientSubject : subject, null, 1, timeout);
                    Object ret = responses[0].getObject();
                    responses[0].recycle();
                    if (ret instanceof Throwable) {
                        throw new SharedContextSendException((Throwable) ret);
                    }
                } catch (RequestTimeoutException e) {
                    throw new SharedContextTimeoutException(e);
                }
            }
        } catch (MessageException e) {
            throw new SharedContextSendException(e);
        } catch (MessageSendException e) {
            throw new SharedContextSendException(e);
        }
    }
}
Also used : RequestTimeoutException(jp.ossc.nimbus.service.publish.RequestTimeoutException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Message(jp.ossc.nimbus.service.publish.Message) MessageException(jp.ossc.nimbus.service.publish.MessageException)

Aggregations

MessageException (jp.ossc.nimbus.service.publish.MessageException)17 Message (jp.ossc.nimbus.service.publish.Message)14 HashSet (java.util.HashSet)10 LinkedHashSet (java.util.LinkedHashSet)10 Set (java.util.Set)10 MessageSendException (jp.ossc.nimbus.service.publish.MessageSendException)9 RequestTimeoutException (jp.ossc.nimbus.service.publish.RequestTimeoutException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ObjectInputStream (java.io.ObjectInputStream)2 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 IndexNotFoundException (jp.ossc.nimbus.beans.IndexNotFoundException)1 IndexPropertyAccessException (jp.ossc.nimbus.beans.IndexPropertyAccessException)1 CacheMap (jp.ossc.nimbus.service.cache.CacheMap)1