EndpointReference target = rmMessageContext.getTo();
if(target == null || target.hasAnonymousAddress()) {
if(target != null && SandeshaUtil.isWSRMAnonymous(target.getAddress())) {
// Search for any sequences that have an acksTo that matches this target, and add an ack
// for each of them.
RMDBean findBean = new RMDBean();
findBean.setAcksToEPR(target.getAddress());
findBean.setTerminated(false);
Collection rmdBeans = storageManager.getRMDBeanMgr().find(findBean);
Iterator sequences = rmdBeans.iterator();
while(sequences.hasNext()) {
RMDBean sequence = (RMDBean) sequences.next();
if (sequence.getHighestInMessageNumber() > 0) {
if(log.isDebugEnabled()) log.debug("Piggybacking ack for sequence: " + sequence.getSequenceID());
RMMsgCreator.addAckMessage(rmMessageContext, sequence.getSequenceID(), sequence);
}
}
} else {
// We have no good indicator of the identity of the destination, so the only sequence
// we can ack is the inbound one that caused us to create this response.
String inboundSequence = (String) rmMessageContext.getProperty(Sandesha2Constants.MessageContextProperties.INBOUND_SEQUENCE_ID);
if(inboundSequence != null) {
RMDBean inboundBean = SandeshaUtil.getRMDBeanFromSequenceId(storageManager, inboundSequence);
if(inboundBean != null && !inboundBean.isTerminated()) {
String acksTo = inboundBean.getAcksToEPR();
EndpointReference acksToEPR = new EndpointReference(acksTo);
if(acksTo == null || acksToEPR.hasAnonymousAddress()) {
if(log.isDebugEnabled()) log.debug("Piggybacking ack for inbound sequence: " + inboundSequence);
RMMsgCreator.addAckMessage(rmMessageContext, inboundSequence, inboundBean);
}
}
}
}
if(log.isDebugEnabled()) log.debug("Exit: AcknowledgementManager::piggybackAcksIfPresent, anon");
return transaction;
}
// From here on, we must be dealing with a real address. Piggyback all sequences that have an
// acksTo that matches the To address, and that have an ackMessage queued up for sending.
Set acked = new HashSet();
SenderBean findBean = new SenderBean();
findBean.setMessageType(Sandesha2Constants.MessageTypes.ACK);
findBean.setSend(true);
findBean.setToAddress(target.getAddress());
Collection collection = retransmitterBeanMgr.find(findBean);
if (transaction != null && transaction.isActive())
transaction.commit();
transaction = storageManager.getTransaction();
Iterator it = collection.iterator();
while (it.hasNext()) {
SenderBean ackBean = (SenderBean) it.next();
// Piggybacking will happen only if the end of ack interval (timeToSend) is not reached.
long timeNow = System.currentTimeMillis();
if (ackBean.getTimeToSend() > timeNow) {
// Delete the beans that would have sent the ack
retransmitterBeanMgr.delete(ackBean.getMessageID());
storageManager.removeMessageContext(ackBean.getMessageContextRefKey());
String sequenceId = ackBean.getSequenceID();
if (log.isDebugEnabled()) log.debug("Piggybacking ack for sequence: " + sequenceId);
RMDBean rmdBean = SandeshaUtil.getRMDBeanFromSequenceId(storageManager, sequenceId);
if(rmdBean != null && !rmdBean.isTerminated()) {
RMMsgCreator.addAckMessage(rmMessageContext, sequenceId, rmdBean);
}
acked.add(sequenceId);
}
}
// As a special case, if this is a terminate sequence message then add in ack messages for
// any sequences that have an acksTo that matches the target address. This helps to ensure
// that request-response sequence pairs end cleanly.
if(rmMessageContext.getMessageType() == Sandesha2Constants.MessageTypes.TERMINATE_SEQ) {
if(log.isDebugEnabled()) log.debug("Adding extra acks, as this is a terminate");
RMDBean findRMDBean = new RMDBean();
findRMDBean.setAcksToEPR(target.getAddress());
findRMDBean.setTerminated(false);
Collection rmdBeans = storageManager.getRMDBeanMgr().find(findRMDBean);
Iterator sequences = rmdBeans.iterator();
while(sequences.hasNext()) {
RMDBean sequence = (RMDBean) sequences.next();
String sequenceId = sequence.getSequenceID();
if(!acked.contains(sequenceId) && sequence.getHighestInMessageNumber() > 0) {
if(log.isDebugEnabled()) log.debug("Piggybacking ack for sequence: " + sequenceId);
RMMsgCreator.addAckMessage(rmMessageContext, sequenceId, sequence);
acked.add(sequenceId);
}
}