Search in sources :

Example 1 with ChannelException

use of org.apache.catalina.tribes.ChannelException in project tomcat by apache.

the class GroupChannel method send.

/**
     *
     * @param destination Member[] - destination.length > 0
     * @param msg Serializable - the message to send
     * @param options sender options, options can trigger guarantee levels and different
     *                interceptors to react to the message see class documentation for the
     *                <code>Channel</code> object.<br>
     * @param handler - callback object for error handling and completion notification,
     *                  used when a message is sent asynchronously using the
     *                  <code>Channel.SEND_OPTIONS_ASYNCHRONOUS</code> flag enabled.
     * @return UniqueId - the unique Id that was assigned to this message
     * @throws ChannelException - if an error occurs processing the message
     * @see org.apache.catalina.tribes.Channel
     */
@Override
public UniqueId send(Member[] destination, Serializable msg, int options, ErrorHandler handler) throws ChannelException {
    if (msg == null)
        throw new ChannelException(sm.getString("groupChannel.nullMessage"));
    XByteBuffer buffer = null;
    try {
        if (destination == null || destination.length == 0) {
            throw new ChannelException(sm.getString("groupChannel.noDestination"));
        }
        //generates a unique Id
        ChannelData data = new ChannelData(true);
        data.setAddress(getLocalMember(false));
        data.setTimestamp(System.currentTimeMillis());
        byte[] b = null;
        if (msg instanceof ByteMessage) {
            b = ((ByteMessage) msg).getMessage();
            options = options | SEND_OPTIONS_BYTE_MESSAGE;
        } else {
            b = XByteBuffer.serialize(msg);
            options = options & (~SEND_OPTIONS_BYTE_MESSAGE);
        }
        data.setOptions(options);
        //XByteBuffer buffer = new XByteBuffer(b.length+128,false);
        buffer = BufferPool.getBufferPool().getBuffer(b.length + 128, false);
        buffer.append(b, 0, b.length);
        data.setMessage(buffer);
        InterceptorPayload payload = null;
        if (handler != null) {
            payload = new InterceptorPayload();
            payload.setErrorHandler(handler);
        }
        getFirstInterceptor().sendMessage(destination, data, payload);
        if (Logs.MESSAGES.isTraceEnabled()) {
            Logs.MESSAGES.trace("GroupChannel - Sent msg:" + new UniqueId(data.getUniqueId()) + " at " + new java.sql.Timestamp(System.currentTimeMillis()) + " to " + Arrays.toNameString(destination));
            Logs.MESSAGES.trace("GroupChannel - Send Message:" + new UniqueId(data.getUniqueId()) + " is " + msg);
        }
        return new UniqueId(data.getUniqueId());
    } catch (Exception x) {
        if (x instanceof ChannelException)
            throw (ChannelException) x;
        throw new ChannelException(x);
    } finally {
        if (buffer != null)
            BufferPool.getBufferPool().returnBuffer(buffer);
    }
}
Also used : UniqueId(org.apache.catalina.tribes.UniqueId) XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData) ByteMessage(org.apache.catalina.tribes.ByteMessage) RemoteProcessException(org.apache.catalina.tribes.RemoteProcessException) ChannelException(org.apache.catalina.tribes.ChannelException) ChannelException(org.apache.catalina.tribes.ChannelException)

Example 2 with ChannelException

use of org.apache.catalina.tribes.ChannelException in project tomcat by apache.

the class RpcChannel method messageReceived.

@Override
public void messageReceived(Serializable msg, Member sender) {
    RpcMessage rmsg = (RpcMessage) msg;
    RpcCollectorKey key = new RpcCollectorKey(rmsg.uuid);
    if (rmsg.reply) {
        RpcCollector collector = responseMap.get(key);
        if (collector == null) {
            if (!(rmsg instanceof RpcMessage.NoRpcChannelReply))
                callback.leftOver(rmsg.message, sender);
        } else {
            synchronized (collector) {
                //make sure it hasn't been removed
                if (responseMap.containsKey(key)) {
                    if ((rmsg instanceof RpcMessage.NoRpcChannelReply))
                        collector.destcnt--;
                    else
                        collector.addResponse(rmsg.message, sender);
                    if (collector.isComplete())
                        collector.notifyAll();
                } else {
                    if (!(rmsg instanceof RpcMessage.NoRpcChannelReply))
                        callback.leftOver(rmsg.message, sender);
                }
            }
        //synchronized
        }
    //end if
    } else {
        boolean finished = false;
        final ExtendedRpcCallback excallback = (callback instanceof ExtendedRpcCallback) ? ((ExtendedRpcCallback) callback) : null;
        boolean asyncReply = ((replyMessageOptions & Channel.SEND_OPTIONS_ASYNCHRONOUS) == Channel.SEND_OPTIONS_ASYNCHRONOUS);
        Serializable reply = callback.replyRequest(rmsg.message, sender);
        ErrorHandler handler = null;
        final Serializable request = msg;
        final Serializable response = reply;
        final Member fsender = sender;
        if (excallback != null && asyncReply) {
            handler = new ErrorHandler() {

                @Override
                public void handleError(ChannelException x, UniqueId id) {
                    excallback.replyFailed(request, response, fsender, x);
                }

                @Override
                public void handleCompletion(UniqueId id) {
                    excallback.replySucceeded(request, response, fsender);
                }
            };
        }
        rmsg.reply = true;
        rmsg.message = reply;
        try {
            if (handler != null) {
                channel.send(new Member[] { sender }, rmsg, replyMessageOptions & ~Channel.SEND_OPTIONS_SYNCHRONIZED_ACK, handler);
            } else {
                channel.send(new Member[] { sender }, rmsg, replyMessageOptions & ~Channel.SEND_OPTIONS_SYNCHRONIZED_ACK);
            }
            finished = true;
        } catch (Exception x) {
            if (excallback != null && !asyncReply) {
                excallback.replyFailed(rmsg.message, reply, sender, x);
            } else {
                log.error(sm.getString("rpcChannel.replyFailed"), x);
            }
        }
        if (finished && excallback != null && !asyncReply) {
            excallback.replySucceeded(rmsg.message, reply, sender);
        }
    }
//end if
}
Also used : ErrorHandler(org.apache.catalina.tribes.ErrorHandler) UniqueId(org.apache.catalina.tribes.UniqueId) Serializable(java.io.Serializable) ChannelException(org.apache.catalina.tribes.ChannelException) Member(org.apache.catalina.tribes.Member) ChannelException(org.apache.catalina.tribes.ChannelException)

Example 3 with ChannelException

use of org.apache.catalina.tribes.ChannelException in project tomcat by apache.

the class TcpPingInterceptor method sendPingMessage.

protected void sendPingMessage(Member[] members) {
    if (members == null || members.length == 0)
        return;
    //generates a unique Id
    ChannelData data = new ChannelData(true);
    data.setAddress(getLocalMember(false));
    data.setTimestamp(System.currentTimeMillis());
    data.setOptions(getOptionFlag());
    data.setMessage(new XByteBuffer(TCP_PING_DATA, false));
    try {
        super.sendMessage(members, data, null);
    } catch (ChannelException x) {
        log.warn(sm.getString("tcpPingInterceptor.ping.failed"), x);
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData) ChannelException(org.apache.catalina.tribes.ChannelException)

Example 4 with ChannelException

use of org.apache.catalina.tribes.ChannelException in project tomcat by apache.

the class GroupChannel method checkOptionFlags.

/**
     * Validates the option flags that each interceptor is using and reports
     * an error if two interceptor share the same flag.
     * @throws ChannelException Error with option flag
     */
protected void checkOptionFlags() throws ChannelException {
    StringBuilder conflicts = new StringBuilder();
    ChannelInterceptor first = interceptors;
    while (first != null) {
        int flag = first.getOptionFlag();
        if (flag != 0) {
            ChannelInterceptor next = first.getNext();
            while (next != null) {
                int nflag = next.getOptionFlag();
                if (nflag != 0 && (((flag & nflag) == flag) || ((flag & nflag) == nflag))) {
                    conflicts.append("[");
                    conflicts.append(first.getClass().getName());
                    conflicts.append(":");
                    conflicts.append(flag);
                    conflicts.append(" == ");
                    conflicts.append(next.getClass().getName());
                    conflicts.append(":");
                    conflicts.append(nflag);
                    conflicts.append("] ");
                }
                //end if
                next = next.getNext();
            }
        //while
        }
        //end if
        first = first.getNext();
    }
    //while
    if (conflicts.length() > 0)
        throw new ChannelException(sm.getString("groupChannel.optionFlag.conflict", conflicts.toString()));
}
Also used : ChannelInterceptor(org.apache.catalina.tribes.ChannelInterceptor) ChannelException(org.apache.catalina.tribes.ChannelException)

Example 5 with ChannelException

use of org.apache.catalina.tribes.ChannelException in project tomcat by apache.

the class TestGroupChannelOptionFlag method testOptionNoConflict.

@Test
public void testOptionNoConflict() throws Exception {
    boolean error = false;
    channel.setOptionCheck(true);
    ChannelInterceptor i = new TestInterceptor();
    i.setOptionFlag(128);
    channel.addInterceptor(i);
    i = new TestInterceptor();
    i.setOptionFlag(64);
    channel.addInterceptor(i);
    i = new TestInterceptor();
    i.setOptionFlag(256);
    channel.addInterceptor(i);
    try {
        channel.start(Channel.DEFAULT);
    } catch (ChannelException x) {
        if (x.getMessage().indexOf("option flag conflict") >= 0)
            error = true;
    }
    assertFalse(error);
}
Also used : ChannelInterceptor(org.apache.catalina.tribes.ChannelInterceptor) ChannelException(org.apache.catalina.tribes.ChannelException) Test(org.junit.Test)

Aggregations

ChannelException (org.apache.catalina.tribes.ChannelException)27 IOException (java.io.IOException)10 Member (org.apache.catalina.tribes.Member)8 UnknownHostException (java.net.UnknownHostException)4 UniqueId (org.apache.catalina.tribes.UniqueId)4 Test (org.junit.Test)4 ByteMessage (org.apache.catalina.tribes.ByteMessage)3 FaultyMember (org.apache.catalina.tribes.ChannelException.FaultyMember)3 ChannelInterceptor (org.apache.catalina.tribes.ChannelInterceptor)3 Serializable (java.io.Serializable)2 Response (org.apache.catalina.tribes.group.Response)2 ChannelData (org.apache.catalina.tribes.io.ChannelData)2 XByteBuffer (org.apache.catalina.tribes.io.XByteBuffer)2 DatagramPacket (java.net.DatagramPacket)1 SelectionKey (java.nio.channels.SelectionKey)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1