Simple Source

Introduction

The SimpleSource is the fastes way to write sources, it handles most of the book keeping involved in writing sources.

The Methods

  • Constructor that makes it easy to quickly create a source apecifying server and buffer sizes
  • addChannel() method for defining a channel and corresponding MIME_Type
  • put() methods that provide an easy interface for storing data of most types (String, integers, decimals, binary, arrays etc…)
  • flush() method that sends the data to the server
  • Helper methods such as methods for reading timestamps

Programming Using Simple Source

Using SimpleSource is straightforward and allows you to do the following in the order , channels are automatically identified by names.

  1. Construct a SimpleSource object that manages the connection to the appropriate DataTurbine server specifying the name, address, port, cache size, and archive size as the Constructor arguments (the most common ones are default).
  2. Create channel using the addChannel() method specifying appropriate MIME types (common types are defined as constants in SimpleSource.java.
  3. Put data into the channel using the put() method using the same channel names as those defined byaddChannel().
  4. Important: Data is not transmitted until flush() is called.
  5. Optional: by default the source connects to a server when flush is called then disconects. If a persistent connection is preffered it can be establised using the connect() and then explictly closed using the close()method.

Hello World

In this example we create a source named MySource with a single channel named count.
The source puts a single data point into the source, addressing the source by name.
The source flushes the data (put it into the server), then disconnects.

The source uses all default settings. By default it connects to the server at localhost port 3333.
The channel creates a channel with default settings, inserting numerical data, specifying the type MIME_BINARY, and using the current system time as the data point’s timestamp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import com.rbnb.sapi.SAPIException;
public class HelloWorld {
    
    public static void main(String[] args) throws SAPIException {
        SimpleSource src = new SimpleSource("MySource");
        
        src.addChannel("count");
        
        src.put("count", 42);
        
        src.flush();
    }
}

Note: This particular bit of code does not have print statements, so correct execution would result in no console output, you can verify that data is being added via using a sink to connect to the server or looking at the server log.

Multiple Channels

This source connects to a server at a specific host 192.168.1.7 and port 4444.
It contains two channels one supplying numeric data the other GPS lat,long tuples
This method loops adding multiple data points, flushing every time a data point is added.

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void example() throws SAPIException{
         SimpleSource src = new SimpleSource("MySource2", "192.168.1.7", 4444) ;
         
         src.addChannel("temp");
         src.addChannel("gps", SimpleSource.MIME_GPS);
         
         for(double offset=0; offset < 5; offset += 0.1){
             src.put("temp", 32.0 + offset);
             src.put("gps", new double[]{53.0 + offset,-12.0});
             
             src.flush();
         }
}

Persistent Connection

This source establishes a persistent connection to the server, disabling automatic connection handling.
Only a single connection is established for all three flushes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void example() throws SAPIException{
        SimpleSource src = new SimpleSource("MySource3");
        src.setConnectionHandling(false);
        src.setArchiveSize(400);
        src.setCacheSize(10);
        src.addChannel("temp");
        
        src.connect();
        
        src.put("temp", 32.1);
        src.flush();
        src.put("temp", 30.7);
        src.flush();
        
        src.close();
    }

Listeners

This source specifies a listener that prints out messages for various source operations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public static void example() throws SAPIException{
        SimpleSource src = new SimpleSource("MySource4");
        src.addChannel("count");
        src.addListener(new SimpleSourceListener() {
            
            @Override
            public void onPut(String chName, Object[] data, double timestamp) {
                System.out.println(chName +": "+data[0]+" @ "+timestamp);
            }
            
            @Override
            public void onFlush() {
                System.out.println("Sending Data");
            }
            
            @Override
            public void onAddChannel(String channelName, String mimeType) {
                System.out.println("Ready Channel: "+channelName +" as"+
            mimeType);
            }
            @Override
            public void onConnect() {
                System.out.println("Connecting to Source");
            }
            @Override
            public void onDisconect() {
                System.out.println("Disconnecting");
            }
        });
        
        src.connect();
        src.put("count", 31);
        src.flush();
        
        src.put("count", 40);
        src.flush();
        src.close();
    }
Open Source DataTurbine Initiative © 2016 Frontier Theme