// 3. get the next message
List<MessageRef> myMessageRefList = myMessageGroup.getMessageRefList();
// the MessageRef that we are to send
MessageRef mySendMessageRef = null;
if (mySendMessageRefIndex >= 0) {
// make sure we haven't gone beyond the end of the messages
if (myMessageRefList.size() > mySendMessageRefIndex) {
mySendMessageRef = myMessageRefList.get(mySendMessageRefIndex);
}
}
logger().debug("about to send mySendMessageRef: {}", mySendMessageRef);
// no message to send (either couldn't find last message or
// there is no next message); also check for the case where
// the last send date is not set (should never happen, but just
// to keep it from breaking the system we treat it like end
// of list)
if (mySendMessageRef == null || mySubscription.getLastSendDate() == null) {
logger().debug("no more messages to send for this subscription");
/* ====================================================== */
/* clear next send date of subscription */
/* ====================================================== */
endSubscription(mySubscription);
return true;
}
if (mySubscription.getLastSendDate() == null) {
logger().warn("last_send_date was null on subscription, should never happen");
}
// 3a.read the time on it and recalculate, confirm that
// time is now past
Long myWait = mySendMessageRef.getWaitAfterLastMessage();
if (myWait == null) {
/* ====================================================== */
/* clear next send date of subscription */
/* ====================================================== */
logger().warn("'wait after last message' value was not set for this message, ending the subscription");
endSubscription(mySubscription);
return true;
}
// 3b.if not past then set next consideration date to recalced date
// and kick back
// see if it's time to send yet
long myLastSendDate = mySubscription.getLastSendDate().getTime();
if (myLastSendDate + myWait > new Date().getTime()) {
logger().debug("it is not yet time to send this message, putting it back for later");
// not time to send (possibly message changed since
// this subscription record was last edited - also first
// subscription looks like this);
// reset the nextSendDate and move on
mySubscription.setNextSendDate(new Date(myLastSendDate + myWait));
mainDao.persist(mySubscription);
return true;
}
// 4. render and send message
boolean mySendWorked = sendMessage(
mySendMessageRef,
myMessageGroup,
mySubscriber,
mySubscription,
LogEntryType.MESSAGE_SENT
);
// if the sending was skipped...
if (!mySendWorked) {
// write a log entry for the skipping
mainDao.logEntry
(
LogEntryType.MESSAGE_SKIPPED,
mySubscriber,
myMessageGroup.getName(),
propUtil.mkprops(
"message_name", mySendMessageRef.getName()
)
);
}
// 5. get next message in the list and recalculate date based on that,
// update last message name and save
// update the last sent info
mySubscription.setLastMessageName(mySendMessageRef.getName());
mySubscription.setLastSendDate(new Date());
// index of next message
int myNextMessageRefIndex = mySendMessageRefIndex + 1;
// 5a.if no more messages, then next consideration date is set to null
if (myNextMessageRefIndex >= myMessageRefList.size()) {
mySubscription.setNextSendDate(null);
}
// if we have a next message, then calc when it fires
else {
MessageRef myNextMessageRef = myMessageRefList.get(myNextMessageRefIndex);
Long myNextWait = myNextMessageRef.getWaitAfterLastMessage();
mySubscription.setNextSendDate(
new Date(mySubscription.getLastSendDate().getTime() + myNextWait)
);
}