<?php require_once( dirname(dirname(dirname( __FILE__ ))) . '/wp-load.php' ); ?>
<!-- START of header -->
<?php get_header(); ?>
<!-- END of header -->

<!-- warapper -->

<div class="docs-content">
<!-- START of page navigation -->
<?php get_template_part( 'docs_navigation' ); ?>
<!-- END of page navigation -->
<!-- START of pusher -->
<div class="docs-content-body">
<div id="content" class="docs-content-body__inner">

<h1 class="title">5.1.17 Trigger (REST)</h1>
<h2 id="overview">Overview</h2>
<p>This chapter describes how to notify events by REST method in trigger function.</p>
<h2 id="trigger-function">Trigger function</h2>
<p>A trigger function is an automatic notification function when an operation (add/update or delete) is carried out on the row data of a container. Event notifications can be received without the need to poll and monitor the database by application.</p>
<p>There are 2 ways of notifying events to the application.</p>
<ul>
<li>REST</li>
<li>Java Messaging Service (JMS)</li>
</ul>
<p>Here we describe how to notify the events by REST method.</p>
<h2 id="setting-the-trigger">Setting the Trigger</h2>
<p>Following information should be set to notify events by REST method.</p>
<p><strong>Table.1 Trigger settings (REST)</strong></p>
<div class="griddb" markdown="1"></div>
<table>
<colgroup>
<col style="width: 23%" />
<col style="width: 76%" />
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Item</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Name</td>
<td style="text-align: left;">Trigger name.</td>
</tr>
<tr class="even">
<td style="text-align: left;">Trigger type</td>
<td style="text-align: left;"><code>TriggerInfo.Type.REST</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;">Notification condition</td>
<td style="text-align: left;"><code>TriggerInfo.EventType.PUT</code> or <code>TriggerInfo.EventType.DELETE</code></td>
</tr>
<tr class="even">
<td style="text-align: left;">Notification destination URI</td>
<td style="text-align: left;">It should be described in the following format. (method name)://(host name):(port number)/(path)</td>
</tr>
</tbody>
</table>
<strong>List.1 Setting the Trigger</strong> (TriggerRest.java)
<pre class="prettyprint linenums:32">
// Create Trigger Settings
TriggerInfo trigger = new TriggerInfo();
trigger.setName("InstrumentLogRESTTrigger");
trigger.setType(Type.REST);
trigger.setTargetEvents(EnumSet.of(EventType.PUT));
trigger.setURI(URI.create("http://127.0.0.1/api"));

// Get TimeSeries Container
TimeSeries&lt;InstrumentLog&gt; logTs =
        store.getTimeSeries("weather_station_1", InstrumentLog.class);

logTs.createTrigger(trigger);

// Update Data for call Trigger
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.US);
InstrumentLog log = logTs.get(format.parse("2016/07/02 12:00"));
log.temperture = 90;
ogTs.put(log);
</pre>
<ul>
<li>L.33: Instantiate the <code>TriggerInfo</code> which represents the trigger setting.</li>
<li>L.34−37: Set trigger properties in Table.1</li>
<li>L.43: Set <code>TriggerInfo</code> into Timeseries Container. Not only Timeseries Container, but also Collection could be used for trigger function.</li>
<li>L.46-49: Update the Timeseries Container to enable trigger notification.</li>
</ul>
<br>
<h2 id="rest-server-startup">REST server startup</h2>
<p>You need to prepare the REST server to receive notifications. In this sample, we will show you how to run the simple Web server in Python which can be used to display the logs.</p>
<br>    
<strong>List.2 REST Server Script</strong> (restserver.py)
<pre class="prettyprint linenums">
import sys
import json
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class JsonResponseHandler(BaseHTTPRequestHandler):

    def do_POST(self):
        content_len = int(self.headers.get('content-length'))
        body = self.rfile.read(content_len).decode('UTF-8')
        jsonBody = json.loads(body)
        print(json.dumps(jsonBody, indent=4))
        self.send_response(200)
        self.send_header('Content-type', 'text/json')
        self.end_headers()

server = HTTPServer(('', 80), JsonResponseHandler)
server.serve_forever()
</pre>
<p>This REST server can be run by executing the command below.</p>
<strong>List.3 Run REST Server Script</strong>
<pre class="prettyprint linenums">
$ sudo python restserver.py
</pre>
<p>Please note that you should run the REST server on the machine which represents notification destination URI.</p>
<h2 id="result-of-the-trigger-execution">Result of the Trigger execution</h2>
<p>You can see the result below on REST server when <code>TriggerRest.java</code> is executed.</p>
<strong>List.4 Result of Update the Row</strong>
<pre class="prettyprint linenums">
{
    "container": "weather_station_1",
    "event": "put"
}
gsnode2.griddb_default - - [13/Sep/2016 10:18:01] "POST /api HTTP/1.1" 200 -
</pre>
<p>In this case, Container <code>weather_station_1</code> exists in <code>gsnode2.griddb_default</code> and you can see the the notification content.</p>
<h2 id="source-code">Source Code</h2>
<p>Complete source code used in this sample can be downloaded from the following.</p>
<p>Download: <a href="img/trigger-rest.zip">trigger-rest.zip</a></p>

</div>
</div>
</div>
</div>

</div>

<!-- / main -->

<?php get_footer(); ?>
