-
Notifications
You must be signed in to change notification settings - Fork 31
11 Flyver Message Queue
Flyver SDK provides message queue for interprocess communication. Through it every component can register itself as a producer or consumer for an arbitrary topic, and receive messages published to it.
To register to the message queue as a producer, your component should subclass the abstract class FlyverMQProducer, and call the super() constructor with the topic, to which it wishes to publish messages
public class MyProducer extends FlyverMQProducer {
MyProducer() {
super("mytopic");
}
after that, it should register to the Message queue with the register(boolean replaceExisting) method, provided by the superclass.
New messages are added to the queue via the addMessage(FlyverMQMessage message) method, also provided by the superclass
Producers have four callbacks
This callback is called when the provider is successfully registered to the Message Queue
This callback is called when the provider is removed from the queue by any means
Providers should implement pause functionallity to halt sending events to the queue on request. This callback is called when a pause is requested for the current provider
This callback is called to resume an already paused provider
Every class implementing the FlyverMQConsumer interface can register to a topic, and be notified for new messages received on it.
FlyverMQ.getInstance().registerConsumer(FlyverMQConsumer consumer, String topic)
This callback is called every time a new message is received on the topic, that the consumer is registered to.
This callback is called when the consumer is unregistered from the Message Queue
The message queue can serve producers/consumers that are running on devices other than the phone, or another phones as well. To do so, you must open a socket, and send JSON-formatted message with your intent. Simple node.js example
var net = require('net');
var HOST = ''; //your device IP
var PORT = 51423;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.setNoDelay(true);
client.write('{"key":"mq","value1":"consumer","value2":"sensors.raw"}\n');
});
client.on('data', function(data) {
console.log(data.toString());
});
This registers the node.js application as a consumer for the sensors.raw topic, and messages published to it are received in the client.on method.
FlyverMQMessage class is a container for the messages. It defines fields for topic, creation time, priority, message id, ttl and data. It should be constructed via a builder
FlyverMQMessage message = new FlyverMQMessage.MessageBuilder().
setCreationTime(System.nanoTime()).
setMessageId(13532).
setTopic(TOPIC).
setPriority((short) 1).
setTtl(12341).
setData(data).
build();