FTL Client API
The samples provided use our FTL API to send messages. We will show an example of our API being used to send a message on a queue.
These snippets skip full production error handling and closing. Use best practices when creating applications.
- Go
- Java
- C
Subscriber
We first connect to the FTL server and set up our message channel, event queue, and the subscriber on the quickstart queue destination:
realm, _ := ftl.Connect("http://localhost:8585", "", nil)
messageChannel := make(chan ftl.Message)
queue, _ := realm.NewQueue(nil, nil)
sub, _ := queue.SubscribeOnDest("Q:quickstart.queue", nil, messageChannel)
We then wait for the message on our message channel and print its contents upon obtaining it:
msg := <-messageChannel
fmt.Printf("received message: %s\n", msg.String())
Publisher
We first connect to the FTL server and make a new destination publisher:
realm, _ := ftl.Connect("http://localhost:8585", "", nil)
pub, _ := realm.NewDestPublisher("", nil)
Then, we create a new message and set its type and message content:
msg, _ := realm.NewMessage("")
content := make(map[string]interface{})
content["type"] = "hello"
content["message"] = "hello world earth"
Lastly, we attach the content to the message and send it on our quickstart queue destination:
msg.Marshal(&content)
pub.Send("Q:quickstart.queue", msg)
Subscriber
We first connect to the FTL server and set up our event queue:
Realm realm = FTL.connectToRealmServer("http://localhost:8585", null, null);
EventQueue queue = realm.createEventQueue();
We then set up our subscriber on the quickstart queue destination using the quickstart durable:
DestSubscriber sub = realm.createDestSubscriber("Q:quickstart.queue", null);
We then add the subcriber to our event queue and attach an implementation of the SubscriberListener interface that prints the message contents upon obtaining it:
public class SubListen implements SubscriberListener {
public void messagesReceived(List<Message> messages, EventQueue eventQueue) {
for (message : messages) {
System.out.print("Received message");
System.out.println(message.toString());
}
}
}
SubListen subListener = new SubListen();
queue.addDestSubscriber(sub, subListener);
queue.dispatch();
Publisher
We first connect to the FTL server and make a new destination publisher:
Realm realm = FTL.connectToRealmServer("http://localhost:8585", null, null);
DestPublisher pub = realm.createDestPublisher(null, null);
Then, we create a new message and set its type and message content:
Message msg = realm.createMessage("");
msg.setString("type", "hello");
msg.setString("message", "hello world earth");
Lastly, we send the message on our quickstart queue destination:
pub.send("Q:quickstart.queue", msg);
Subscriber
We first connect to the FTL server and set up our message channel and our event queue:
tibEx ex = tibEx_Create();
tibRealm realm = tibRealm_Connect(ex, "http://localhost:8585", NULL, NULL);
tibEventQueue queue = tibEventQueue_Create(ex, realm, NULL);
We then set up our subscriber on the quickstart queue destination using the quickstart durable:
tibDestSubscriber sub = tibRealm_CreateDestSubscriber(ex, realm, "Q:quickstart.queue", NULL);
We then add the subcriber to our event queue and attach an onMessages function that prints the message contents upon obtaining it:
void
onMessages(
tibEx ex,
tibEventQueue msgQueue,
tibint32_t msgNum,
tibMessage* msgs,
void** closures)
{
char buffer[1024];
(void)tibMessage_ToString(ex, msgs[0], buffer, sizeof(buffer));
printf("received message: %s\n", buffer);
}
tibEventQueue_AddDestSubscriber(ex, queue, sub, onMessages, NULL);
tibEventQueue_Dispatch(ex, queue, TIB_TIMEOUT_WAIT_FOREVER);
Publisher
We first connect to the FTL server and make a new destination publisher:
tibEx ex = tibEx_Create();
tibRealm realm = tibRealm_Connect(ex, "http://localhost:8585", NULL, NULL);
tibDestPublisher pub = tibRealm_CreateDestPublisher(ex, realm, NULL, NULL);
Then, we create a new message and set its type and message content:
tibMessage msg = tibMessage_Create(ex, realm, "");
tibMessage_SetString(ex, msg, "type", "hello");
tibMessage_SetString(ex, msg, "message", "hello world earth");
Lastly, we send the message on our quickstart queue destination:
tibDestPublisher_Send(ex, pub, "Q:quickstart.queue", msg);