Java API

Contents[Hide]

1. Spring JDBC Template example

Normal Spring REST service

@RequestMapping("/employeesWithSpringREST")
public List<Employee> getPersonDetailAsSpringREST() {

    String sql = getSQL();

    List<Employee> res = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Employee.class));
    return res;
}

 

Same, with LightLink Streaming API (ResultSet to JSON, without Java Beans mapping)

@RequestMapping("/employeesStreamWithoutMapping")
public void getPersonDetailAsStreamWithoutMapping() {

    ResponseStream responseStream = LightLinkFilter.getCurrentResponseStream();
    responseStream.writePropertyArrayStart("resultSet");  // write opening JSON array declaration : "resultSet":[

    jdbcTemplate.query(getSQL(), new StreamingMapper(responseStream));

    responseStream.writePropertyArrayEnd();         // write closing array bracket : ]
                                                    // LightLinkFilter will complete the JSON response on method exit
}
For LightLink streming REST service(s) in plain Java, to become available as Browser-side JS API, we need to declare the URLs in 
\WEB-INF\lightlink\#customServices.js like this:
({
    "employeeController.employeesStreamWithMapping":"/action/data/employeesStreamWithMapping",
    "employeeController.employeesStreamWithoutMapping":"/action/data/employeesStreamWithoutMapping"
})
The declaration syntax might appear strange, but it's actually a JavaScript expression returning an array. Such expression mode allows automating services declaration programically instead of having to hardcode each service.

Same, with LightLink Streaming and Java Beans mapping using BeanPropertyRowMapper

@RequestMapping("/employeesStreamWithMapping")
public void getPersonDetailAsStream() {

    ResponseStream responseStream = LightLinkFilter.getCurrentResponseStream();
    responseStream.writePropertyArrayStart("resultSet");  // write opening JSON array declaration : "resultSet":[

    jdbcTemplate.query(getSQL(), new StreamingMapper(responseStream, BeanPropertyRowMapper.newInstance(Employee.class)));

    responseStream.writePropertyArrayEnd();         // write closing array bracket : ]
                                                    // LightLinkFilter will complete the JSON response on method exit
}

 

2. Plain Java/JDBC Example

 

@RequestMapping("/employeesStreamWithMapping") // This method is called with Spring MVC, but it can be used with any other framework
public void getPersonDetailAsStreamInPlainJDBC() throws SQLException {

    Connection connection = null;
    try {
        connection = dataSource.getConnection();

        ResultSet rs = connection.createStatement().executeQuery(getSQL());

        ResponseStream responseStream = LightLinkFilter.getCurrentResponseStream();
        responseStream.writePropertyArrayStart("resultSet");

        while(rs.next()){
            responseStream.writeObjectStart();
            responseStream.writeProperty("id", rs.getString("id"));
            responseStream.writeProperty("gender",rs.getString("gender"));
            responseStream.writeProperty("name",rs.getString("name"));
            responseStream.writeProperty("age",rs.getInt("age"));
            // ....
            responseStream.writeObjectEnd();

        }

        responseStream.writePropertyArrayEnd();             

    } finally {
        if (connection != null)
            connection.close();
    }

}

3. Benefits :

- Lower latency
- Higher general response time
- Good Bye OutOfMemory risk for large data sets. Data volume can even exceed the JVM heap size
- Possibility to use progressive AJAX loading, allowing to display the first page of data while the database is still fetching the remaining rows.

See the Demo : Streaming vs Buffering