Receive Incoming Data From Oracle IoT Cloud Service

After you’ve successfully registered your enterprise application with Oracle IoT Cloud Service, ensure that it is able to receive the data streams that are being sent from Oracle IoT Cloud Service.

If you haven’t registered your enterprise application yet, see Integrating Enterprise Applications with Oracle IoT Cloud Service for information.

Your enterprise application is required to have a Web listener accessible from Oracle IoT Cloud Service in order for your application to receive any data stream being sent by the service.

Note:

If an integration to an Enterprise Application is disconnected from the Oracle IoT Cloud Service instance, messages that were scheduled to be dispatched to that enterprise application will not be delivered. However, they will be persisted if the Enable Message Storage property is selected in the Settings tab of the Management Console. Oracle IoT Cloud Service will withdraw the message dispatch attempt, wait for 10 seconds, and then try to resume normal dispatching again.

The following sections provide some guidelines with code snippets to illustrate what you need to do, and a sample of a simple standalone Java Web application that prints out the data that it receives.

General Steps and Sample Code Snippets

Below is a set of general steps and sample code snippets that you can use as a guideline:
  1. Choose a type of Web server where your enterprise application will run as a Web listener. Some Web server examples are Java EE Glassfish, WebLogic, node.js, Apache HTTP Server, or Jetty.

  2. Choose the programming model that matches best to the Web server you chose to use in step 1 above. For example, you can use Java using JAX-RS for Java EE Glassfish, JavaScript for node.js, PHP and JavaScript for Apache HTTP Server.

  3. Create an HTTP POST call handler according to the programming model you chose to use in step 2 above.

    Below is an example of a Java source code using JAX-RS for Glassfish.
     /**      
     * POST method for creating an instance of DataListenerResource      
     * @param content representation for the new resource      
     * @return an HTTP response with content of the created resource      
     */     
     @POST     
     @Consumes("application/json")     
     @Produces("application/json")     
     public Response postJson(String content) throws IOException {           
        /*          
         * PARSE AND PROCESS THE JSON MESSAGE BODY HERE          
         */          
    
        /* Ex. JSONArray json = new JSONArray(context)... */         
    
         /* Return HTTP OK */         
         String returnStr = "{" +
                     " \"type\": \"HttpRequestMessage\",\n" +
                     " \"method\": \"POST\",\n" +
                     " \"body\": \"OK\"\n" +
                     " \n" +
                     " }";
             return Response.ok((Object) returnStr).build();     
     }
  4. In that HTTP POST call handler in step 3 above, process the JSON body of the data stream that is sent from Oracle IoT Cloud Service as a result of subscribing to receive data stream from the service.

    Below is an example of the JSON body of a message:
    
    {    "id": "fbba219d-f30a-4faf-bfd9-a8777961f861",
         "clientId": "402d2487-9c25-435b-8f6f-464b7e1c512e",
         "source": "0-FM",
         "destination": "",
         "priority": "LOW",
         "reliability": "BEST_EFFORT",
         "eventTime": 1444438809722,
         "eventTimeAsString": "2015-10-10T01:00:09Z",
         "sender": "",
         "type": "DATA",
         "properties": {},
         "direction": "FROM_DEVICE",
         "receivedTime": 1444438809827,
         "receivedTimeAsString": "2015-10-10T01:00:09Z",
         "payload": {
             "format": "urn:oracle-ebsdemo:iot:device:data:3dprintersensors",
             "data": {
                 "temperature": 25.21332000000001,
                 "humidity": 45.0912,
                 "vibrations": 255.60516426707815,
                 "led": true
             }
         }
     }
  5. Return the correct HTTP Response according to how the processing worked in step 4 above.

    Below is an example code snippet:
         /* Return HTTP 200 OK */
             String returnStr = "{" +
                     " \"type\": \"HttpRequestMessage\",\n" +
                     " \"method\": \"POST\",\n" +
                     " \"body\": \"OK\"\n" +
                     " \n" +
                     " }";
                
              return Response.ok((Object) returnStr).build();

Sample of a Standalone Java Web Application

Below is an example of a simple standalone Java Web application that prints the data it receives. In this example, you need to use http://YOUR_MACHINES_IP:9000/httpConnector that is used in the sample code below, as the value to enter in the URL field when setting up the enterprise application integration via the Oracle IoT Cloud Service Management Console. Where this sample application simply prints the body of the received data stream, your application will most likely make use of a JSON parser library to process the data.

import com.sun.net.httpserver.HttpExchange; 
import com.sun.net.httpserver.HttpServer;  

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.InetSocketAddress;  

public class IoTDataReceiver {
  
    public static void main(String[] args) throws IOException {
         System.out.println("Listening on http://localhost:9000/httpConnector");
         HttpServer server = HttpServer.create(new InetSocketAddress(9000), 0);
         server.createContext("/httpConnector", IoTDataReceiver::handle);
         server.setExecutor(null); // creates a default executor
         server.start();
     }

      public static void handle(HttpExchange t) {
         System.out.println("HvacDataReceiver.handle(" + "t = [" + t + "]" + ")");

          try {
             System.out.println("====================================================");
             BufferedReader bodyReader = new BufferedReader(new InputStreamReader(t.getRequestBody()));
             String line;
             while((line = bodyReader.readLine()) != null) {
                 System.out.println(line);
             }
             // Send 200
             t.sendResponseHeaders(200, 0);
             t.close();
         } catch (Throwable e) {
             e.printStackTrace();
         }
     }
 }