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);
}
}
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
}
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);
}
}
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()));
}
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);
}
Aggregations