Search in sources :

Example 1 with ChannelListener

use of org.apache.catalina.tribes.ChannelListener in project tomcat70 by apache.

the class GroupChannel method messageReceived.

/**
 * Callback from the interceptor stack. <br>
 * When a message is received from a remote node, this method will be invoked by
 * the previous interceptor.<br>
 * This method can also be used to send a message to other components within the same application,
 * but its an extreme case, and you're probably better off doing that logic between the applications itself.
 * @param msg ChannelMessage
 */
@Override
public void messageReceived(ChannelMessage msg) {
    if (msg == null)
        return;
    try {
        if (Logs.MESSAGES.isTraceEnabled()) {
            Logs.MESSAGES.trace("GroupChannel - Received msg:" + new UniqueId(msg.getUniqueId()) + " at " + new java.sql.Timestamp(System.currentTimeMillis()) + " from " + msg.getAddress().getName());
        }
        Serializable fwd = null;
        if ((msg.getOptions() & SEND_OPTIONS_BYTE_MESSAGE) == SEND_OPTIONS_BYTE_MESSAGE) {
            fwd = new ByteMessage(msg.getMessage().getBytes());
        } else {
            try {
                fwd = XByteBuffer.deserialize(msg.getMessage().getBytesDirect(), 0, msg.getMessage().getLength());
            } catch (Exception sx) {
                log.error("Unable to deserialize message:" + msg, sx);
                return;
            }
        }
        if (Logs.MESSAGES.isTraceEnabled()) {
            Logs.MESSAGES.trace("GroupChannel - Receive Message:" + new UniqueId(msg.getUniqueId()) + " is " + fwd);
        }
        // get the actual member with the correct alive time
        Member source = msg.getAddress();
        boolean rx = false;
        boolean delivered = false;
        for (int i = 0; i < channelListeners.size(); i++) {
            ChannelListener channelListener = (ChannelListener) channelListeners.get(i);
            if (channelListener != null && channelListener.accept(fwd, source)) {
                channelListener.messageReceived(fwd, source);
                delivered = true;
                // is responsible for returning the reply, otherwise we send an absence reply
                if (channelListener instanceof RpcChannel)
                    rx = true;
            }
        }
        // for
        if ((!rx) && (fwd instanceof RpcMessage)) {
            // if we have a message that requires a response,
            // but none was given, send back an immediate one
            sendNoRpcChannelReply((RpcMessage) fwd, source);
        }
        if (Logs.MESSAGES.isTraceEnabled()) {
            Logs.MESSAGES.trace("GroupChannel delivered[" + delivered + "] id:" + new UniqueId(msg.getUniqueId()));
        }
    } catch (Exception x) {
        // as a warning.
        if (log.isWarnEnabled())
            log.warn("Error receiving message:", x);
        throw new RemoteProcessException("Exception:" + x.getMessage(), x);
    }
}
Also used : UniqueId(org.apache.catalina.tribes.UniqueId) Serializable(java.io.Serializable) ChannelListener(org.apache.catalina.tribes.ChannelListener) RemoteProcessException(org.apache.catalina.tribes.RemoteProcessException) ChannelException(org.apache.catalina.tribes.ChannelException) RemoteProcessException(org.apache.catalina.tribes.RemoteProcessException) ByteMessage(org.apache.catalina.tribes.ByteMessage) Member(org.apache.catalina.tribes.Member)

Example 2 with ChannelListener

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

the class GroupChannel method messageReceived.

/**
 * Callback from the interceptor stack. <br>
 * When a message is received from a remote node, this method will be
 * invoked by the previous interceptor.<br>
 * This method can also be used to send a message to other components
 * within the same application, but its an extreme case, and you're probably
 * better off doing that logic between the applications itself.
 * @param msg ChannelMessage
 */
@Override
public void messageReceived(ChannelMessage msg) {
    if (msg == null) {
        return;
    }
    try {
        if (Logs.MESSAGES.isTraceEnabled()) {
            Logs.MESSAGES.trace("GroupChannel - Received msg:" + new UniqueId(msg.getUniqueId()) + " at " + new java.sql.Timestamp(System.currentTimeMillis()) + " from " + msg.getAddress().getName());
        }
        Serializable fwd = null;
        if ((msg.getOptions() & SEND_OPTIONS_BYTE_MESSAGE) == SEND_OPTIONS_BYTE_MESSAGE) {
            fwd = new ByteMessage(msg.getMessage().getBytes());
        } else {
            try {
                fwd = XByteBuffer.deserialize(msg.getMessage().getBytesDirect(), 0, msg.getMessage().getLength());
            } catch (Exception sx) {
                log.error(sm.getString("groupChannel.unable.deserialize", msg), sx);
                return;
            }
        }
        if (Logs.MESSAGES.isTraceEnabled()) {
            Logs.MESSAGES.trace("GroupChannel - Receive Message:" + new UniqueId(msg.getUniqueId()) + " is " + fwd);
        }
        // get the actual member with the correct alive time
        Member source = msg.getAddress();
        boolean rx = false;
        boolean delivered = false;
        for (ChannelListener channelListener : channelListeners) {
            if (channelListener != null && channelListener.accept(fwd, source)) {
                channelListener.messageReceived(fwd, source);
                delivered = true;
                // is responsible for returning the reply, otherwise we send an absence reply
                if (channelListener instanceof RpcChannel) {
                    rx = true;
                }
            }
        }
        // for
        if ((!rx) && (fwd instanceof RpcMessage)) {
            // if we have a message that requires a response,
            // but none was given, send back an immediate one
            sendNoRpcChannelReply((RpcMessage) fwd, source);
        }
        if (Logs.MESSAGES.isTraceEnabled()) {
            Logs.MESSAGES.trace("GroupChannel delivered[" + delivered + "] id:" + new UniqueId(msg.getUniqueId()));
        }
    } catch (Exception x) {
        // as a warning.
        if (log.isWarnEnabled()) {
            log.warn(sm.getString("groupChannel.receiving.error"), x);
        }
        throw new RemoteProcessException(sm.getString("groupChannel.receiving.error"), x);
    }
}
Also used : UniqueId(org.apache.catalina.tribes.UniqueId) Serializable(java.io.Serializable) ChannelListener(org.apache.catalina.tribes.ChannelListener) ByteMessage(org.apache.catalina.tribes.ByteMessage) Member(org.apache.catalina.tribes.Member) RemoteProcessException(org.apache.catalina.tribes.RemoteProcessException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ChannelException(org.apache.catalina.tribes.ChannelException) RemoteProcessException(org.apache.catalina.tribes.RemoteProcessException)

Example 3 with ChannelListener

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

the class GroupChannel method heartbeat.

/**
     * Sends a heartbeat through the interceptor stack.<br>
     * Invoke this method from the application on a periodic basis if
     * you have turned off internal heartbeats <code>channel.setHeartbeat(false)</code>
     */
@Override
public void heartbeat() {
    super.heartbeat();
    Iterator<MembershipListener> membershipListenerIterator = membershipListeners.iterator();
    while (membershipListenerIterator.hasNext()) {
        MembershipListener listener = membershipListenerIterator.next();
        if (listener instanceof Heartbeat)
            ((Heartbeat) listener).heartbeat();
    }
    Iterator<ChannelListener> channelListenerIterator = channelListeners.iterator();
    while (channelListenerIterator.hasNext()) {
        ChannelListener listener = channelListenerIterator.next();
        if (listener instanceof Heartbeat)
            ((Heartbeat) listener).heartbeat();
    }
}
Also used : ChannelListener(org.apache.catalina.tribes.ChannelListener) Heartbeat(org.apache.catalina.tribes.Heartbeat) MembershipListener(org.apache.catalina.tribes.MembershipListener)

Aggregations

ChannelListener (org.apache.catalina.tribes.ChannelListener)3 Serializable (java.io.Serializable)2 ByteMessage (org.apache.catalina.tribes.ByteMessage)2 ChannelException (org.apache.catalina.tribes.ChannelException)2 Member (org.apache.catalina.tribes.Member)2 RemoteProcessException (org.apache.catalina.tribes.RemoteProcessException)2 UniqueId (org.apache.catalina.tribes.UniqueId)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Heartbeat (org.apache.catalina.tribes.Heartbeat)1 MembershipListener (org.apache.catalina.tribes.MembershipListener)1