Publication Options
Default Options
Use this method to quickly publish a message with all default options. In which:
- Message will NOT be kept in history.
- No callbacks.
Sample
package com.example.myapplication;
import com.robo.messaging.MessageBus;
public class MyPublisher {
private MessageBus mMessageBus;
public MyPublisher(MessageBus messageBus) {
mMessageBus = messageBus;
}
public void doPublish(String content) {
mMessageBus.publish(new MyMessage(content));
}
}
Keeping message in History
Use this option to specify whether to keep the message in history or not, so that the late-bound subscribers can receive it. Default is false.
Sample
package com.example.myapplication;
import com.robo.messaging.MessageBus;
public class MyPublisher {
private MessageBus mMessageBus;
public MyPublisher(MessageBus messageBus) {
mMessageBus = messageBus;
}
public void doPublishAndKeepInHistory() {
// keep message in history by setting second param to true.
mMessageBus.publish(new MyMessage("Published and kept in history"), true);
}
}
Publishing with Callback
Use this option to enable callback to the publisher. The interface com.robo.messaging.PublisherCallback has two following methods:
- noSubsriber() will be called when there is no subscriber for the message at the moment.
- messageEnqueued() will be called when there is at least one subscriber for the message and the message is being delivered to the subscribers.
Sample
package com.example.myapplication;
import android.util.Log;
import com.robo.messaging.MessageBus;
import com.robo.messaging.PublisherCallback;
public class MyPublisher {
private MessageBus mMessageBus;
public MyPublisher(MessageBus messageBus) {
mMessageBus = messageBus;
}
public void doPublishWithCallback() {
mMessageBus.publish(new MyMessage("Published with callback"), false, new PublisherCallback() {
@Override
public void noSubscriber() {
Log.d("MessageBus", "There was no subscriber at the time of publication");
}
@Override
public void messageEnqueued() {
Log.d("MessageBus", "Message enqueued and will be delivered to subscribers soon");
}
});
}
}