}
private AsyncTask _sendRepeatingWith(final Message message, final RequestDispatcher viaThis, TimeUnit unit, int interval) {
final boolean isConversational = message instanceof ConversationMessageWrapper;
final AsyncTask task = TaskManagerFactory.get().scheduleRepeating(unit, interval, new HasAsyncTaskRef() {
AsyncTask task;
AsyncDelegateErrorCallback errorCallback;
final Runnable sender;
{
errorCallback = new AsyncDelegateErrorCallback(this, message.getErrorCallback());
if (isConversational) {
final Message incomingMsg = ((ConversationMessageWrapper) message).getIncomingMessage();
if (incomingMsg.hasPart(MessageParts.ReplyTo)) {
sender = new Runnable() {
final String replyTo = incomingMsg
.get(String.class, MessageParts.ReplyTo);
@Override
public void run() {
try {
MessageBuilder.getMessageProvider().get()
.toSubject(replyTo)
.copyResource("Session", incomingMsg)
.addAllParts(message.getParts())
.addAllProvidedParts(message.getProvidedParts())
.errorsCall(errorCallback).sendNowWith(viaThis);
}
catch (Throwable t) {
t.printStackTrace();
getAsyncTask().cancel(true);
}
}
};
}
else {
sender = new Runnable() {
@Override
public void run() {
try {
MessageBuilder.getMessageProvider().get()
.copyResource("Session", incomingMsg)
.addAllParts(message.getParts())
.addAllProvidedParts(message.getProvidedParts())
.errorsCall(errorCallback).sendNowWith(viaThis);
}
catch (Throwable t) {
t.printStackTrace();
getAsyncTask().cancel(true);
}
}
};
}
}
else {
sender = new Runnable() {
@Override
public void run() {
try {
viaThis.dispatchGlobal(MessageBuilder.getMessageProvider().get()
.addAllParts(message.getParts())
.addAllProvidedParts(message.getProvidedParts())
.errorsCall(errorCallback));
}
catch (Throwable t) {
t.printStackTrace();
getAsyncTask().cancel(true);
}
}
};
}
}
@Override
public void setAsyncTask(AsyncTask task) {
synchronized (this) {
this.task = task;
}
}
@Override
public AsyncTask getAsyncTask() {
synchronized (this) {
return task;
}
}
@Override
public void run() {
sender.run();
}
});
if (isConversational) {
Object sessionResource = ((ConversationMessageWrapper) message).getIncomingMessage().getResource(Object.class, "Session");
final LaundryReclaim reclaim =
LaundryListProviderFactory.get().getLaundryList(sessionResource).add(new Laundry() {
@Override
public void clean() {
task.cancel(true);
}
});
task.setExitHandler(new Runnable() {
@Override
public void run() {
reclaim.reclaim();
}
});