use of org.apache.catalina.tribes.UniqueId 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 (RuntimeException | IOException e) {
throw new ChannelException(e);
} finally {
if (buffer != null) {
BufferPool.getBufferPool().returnBuffer(buffer);
}
}
}
use of org.apache.catalina.tribes.UniqueId 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);
}
}
use of org.apache.catalina.tribes.UniqueId 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.UniqueId in project tomcat70 by apache.
the class MessageDispatchInterceptor method sendAsyncData.
// run
protected LinkObject sendAsyncData(LinkObject link) {
ChannelMessage msg = link.data();
Member[] destination = link.getDestination();
try {
super.sendMessage(destination, msg, null);
try {
if (link.getHandler() != null)
link.getHandler().handleCompletion(new UniqueId(msg.getUniqueId()));
} catch (Exception ex) {
log.error("Unable to report back completed message.", ex);
}
} catch (Exception x) {
ChannelException cx = null;
if (x instanceof ChannelException)
cx = (ChannelException) x;
else
cx = new ChannelException(x);
if (log.isDebugEnabled())
log.debug("Error while processing async message.", x);
try {
if (link.getHandler() != null)
link.getHandler().handleError(cx, new UniqueId(msg.getUniqueId()));
} catch (Exception ex) {
log.error("Unable to report back error message.", ex);
}
} finally {
addAndGetCurrentSize(-msg.getMessage().getLength());
link = link.next();
}
// try
return link;
}
use of org.apache.catalina.tribes.UniqueId in project tomcat70 by apache.
the class TwoPhaseCommitInterceptor method messageReceived.
@Override
public void messageReceived(ChannelMessage msg) {
if (okToProcess(msg.getOptions())) {
if (msg.getMessage().getLength() == (START_DATA.length + msg.getUniqueId().length + END_DATA.length) && Arrays.contains(msg.getMessage().getBytesDirect(), 0, START_DATA, 0, START_DATA.length) && Arrays.contains(msg.getMessage().getBytesDirect(), START_DATA.length + msg.getUniqueId().length, END_DATA, 0, END_DATA.length)) {
UniqueId id = new UniqueId(msg.getMessage().getBytesDirect(), START_DATA.length, msg.getUniqueId().length);
MapEntry original = messages.get(id);
if (original != null) {
super.messageReceived(original.msg);
messages.remove(id);
} else
log.warn("Received a confirmation, but original message is missing. Id:" + Arrays.toString(id.getBytes()));
} else {
UniqueId id = new UniqueId(msg.getUniqueId());
MapEntry entry = new MapEntry((ChannelMessage) msg.deepclone(), id, System.currentTimeMillis());
messages.put(id, entry);
}
} else {
super.messageReceived(msg);
}
}
Aggregations