Search Results

TIBCO eFTL Android Quick Start

Quick Start Contents

Introduction

This quick start guide is intended to provide basic instructions for writing Android applications for TIBCO eFTL. It focuses on a collection of sample applications included in the TIBCO eFTL Messaging SDK for Java.

Getting Started

  • Download the TIBCO eFTL Messaging SDK for Java. For information about using Java see the Java Quick Start.
  • Obtain a URL and authentication key from your TIBCO Cloud Messaging account.
  • The following permission must be added to any application’s AndroidManifest.xml file using eFTL client library:

    <uses-permission android:name="android.permission.INTERNET">

Building and running samples

Follow the steps below to build and run the Android samples:

  • Using Android Studio, open the eFTL Android sample applications as an existing Android Studio project.
  • In the sample application source files, replace the placeholders TIBCO-CLOUD-MESSAGING-URL and TIBCO-CLOUD-MESSAGING-KEY with your account URL and authentication key.
  • Add the eFTL client library by dragging the tibeftl.jar file to the project’s app/libs folder.
  • Build and run the eFTL Android sample application.

Description of sample applications

Publisher

Basic publisher program that demonstrates the use of publishing eFTL messages.

Subscriber

Basic subscriber program that demonstrates the use of subscribing to eFTL messages.

Run the Subscriber and Publisher at the same time to demonstrate real-time messaging. Stop the Subscriber, run the Publisher, and restart the Subscriber to demonstrate persistence.

Using the eFTL SDK

From Android Studio, copy the eFTL Client Java library JAR file, tibeftl.jar, to your application’s libs directory, right-click the JAR file and select Add As Library.

Connect to TIBCO Cloud Messaging

To establish a connection to TIBCO Cloud Messaging, you need the URL and authentication key. Authentication keys can be obtained only from your TIBCO Cloud Messaging account page on the Authentication key tab. The eFTL client uses the URL to connect to the library. The authentication key is the password used by the eFTL client library to authenticate itself.

You must also want to provide a unique identifier for your client so that it can receive any missed messages while it is disconnected. Note: Client identifiers must be unique; no two clients with the same identifier can be connected simultaneously.

final Properties props = new Properties(); 
// set the password using your authentication key
props.setProperty(EFTL.PROPERTY_PASSWORD, "6ee7639011dd67ac79272a0844c16671"); 

// provide a unique client identifier 
props.setProperty(EFTL.PROPERTY_CLIENT_ID, "abc123"); 

//connect to TIBCO Cloud Messaging
EFTL.connect("wss://messaging.cloud.tibco.com/tcm/01BT0Q4Q3QMK65FBC80P1MDP5Q/channel",
             props, 
             new ConnectionListener() {
               @ Override 
               public void onConnect(Connection connection) {
                                 System.out.printf("connected\n"); 
                     } 
                     @ Override 
                     public void onDisconnect(Connection connection, int code, String reason) {
                  System.out.printf("disconnected: %s\n", reason); 
               } 
               @Override public
               void onReconnect(Connection connection) {
                  System.out.printf("reconnected\n"); 
               } 
               @Override 
               public void onError(Connection connection, int code, String reason) {
               System.out.printf("error: %s\n", reason); 
               } 
            });

onConnect callback

The onConnect callback is invoked after the connection to TIBCO Cloud Messaging is established. The Connection object passed to the onConnect callback can be used for subscribing to messages and publishing messages.

onDisconnect callback

If the connection cannot be established, or is lost, the onDisconnect callback is invoked.

onReconnect callback

The onReconnect callback is invoked when the client successfully reconnects to TIBCO Cloud Messaging.

onError callback

The onError callback is invoked when TIBCO Cloud Messaging sends an error to the client.

Publish Messages

After clients are connected to TIBCO Cloud Messaging, they can publish messages. To publish messages, use the connection returned in the onConnect callback.

// create a message 
final Message message = connection.createMessage(); 
message.setString("event", "hello");
message.setString("text", "Hello, World!"); 

// publish a message to TIBCO Cloud Messaging 
// Fields and arrays of type string, numeric, date, and sub-messages are used in messages.
connection.publish(message, null);

// To be notified if the publish was successful or if an error occurred, callbacks can be provided.

// publish a message to TIBCO Cloud Messaging
connection.publish(message, new CompletionListener() {
@Override
   public void onCompletion(Message message) {
      System.out.printf("publish success\n"); 
   } 
@Override 
   public void onError(Message message, int code, String reason) {
      System.out.printf("publish error: %s\n", reason); 
   } 
});

Receive Messages

After the clients are connected to TIBCO Cloud Messaging, they can create one or more subscriptions to receive messages of interest. You subscribe to messages by matching on the message fields that are of your interest. Use the connection returned in the onConnect callback to register subscriptions.

// create a subscription in TIBCO Cloud Messaging 
// 
// this subscription matches all published messages that contain a 
// field named "event" with a value of "hello" 
// 
// this subscription also sets the durable name to "hello" which allows 
// the client to receive messages that were published while disconnected 
// 
connection.subscribe("{\"event\":\"hello\"}", "hello",
   new SubscriptionListener() { 
      @Override 
      public void onMessage(Message[]messages) { 
         for (Message message : messages) {
         System.out.printf("message received: %s", message.getString("text")); 
         } 
      } 
      @Override 
      public void onSubscribe(String subscription) { 
         System.out.printf("subscribed");
      } 
      public void onError(String subscription, int code, String reason){ 
         System.out.printf("subscription error: %s\n", reason); 
      }
});

Matcher field

The matcher field specifies the messages that are to be received by matching their content. In this case, the subscription receives all published messages containing a field named “event” whose value is “hello”.

Durable field

The durable field is the unique subscription name used by TIBCO Cloud Messaging to store messages for the client when the client is not connected.

onMessage callback

The onMessage callback is invoked when the content of a published message matches the subscription’s matcher.

onError callback

The onError callback is invoked when an error occurs while registering the subscription, typically because of an invalid matcher or invalid durable.