The RSS reader tutorial (Step 3)

Now that Vert.x 3.6.0 has been re­leased, it’s the per­fect time to con­clude our Vert.x Cas­san­dra Client tu­to­r­ial!

In the pre­vi­ous step, we have suc­cess­fully im­ple­mented the sec­ond end­point of the RSS reader app.

The RSS reader ex­am­ple as­sumes im­ple­ment­ing three end­points. This ar­ti­cle is ded­i­cated to im­ple­ment­ing the last GET /articles/by_rss_link?link={rss_link} end­point.

Be­fore com­plet­ing this step, make sure your are in the step_3 git branch:

$ git checkout step_3

Implementing the 3rd endpoint

The 3rd end­point serves a list of ar­ti­cles re­lated to a spe­cific RSS chan­nel. In a re­quest, we spec­ify RSS chan­nel by pro­vid­ing a link. On the ap­pli­ca­tion side, after re­ceiv­ing a re­quest we ex­e­cute the fol­low­ing query:

SELECT title, article_link, description, pubDate FROM articles_by_rss_link WHERE rss_link = RSS_LINK_FROM_REQUEST ;

Implementation

For ob­tain­ing ar­ti­cles by RSS link we need to pre­pare a re­lated state­ment first. Change AppVerticle#prepareSelectArticlesByRssLink in this way:

private Future<Void> prepareSelectArticlesByRssLink() {
    return Util.prepareQueryAndSetReference(client,
            "SELECT title, article_link, description, pubDate FROM articles_by_rss_link WHERE rss_link = ? ;",
            selectArticlesByRssLink
    );
}

And now, we can im­ple­ment the AppVerticle#getArticles method. Ba­si­cally, it will use the selectArticlesByRssLink state­ment for find­ing ar­ti­cles by the given link. Here’s the im­ple­men­ta­tion:

private void getArticles(RoutingContext ctx) {
    String link = ctx.request().getParam("link");
    if (link == null) {
        responseWithInvalidRequest(ctx);
    } else {
        client.executeWithFullFetch(selectArticlesByRssLink.bind(link), handler -> {
            if (handler.succeeded()) {
                List<Row> rows = handler.result();

                JsonObject responseJson = new JsonObject();
                JsonArray articles = new JsonArray();

                rows.forEach(eachRow -> articles.add(
                        new JsonObject()
                                .put("title", eachRow.getString(0))
                                .put("link", eachRow.getString(1))
                                .put("description", eachRow.getString(2))
                                .put("pub_date", eachRow.getTimestamp(3).getTime())
                ));

                responseJson.put("articles", articles);
                ctx.response().end(responseJson.toString());
            } else {
                log.error("failed to get articles for " + link, handler.cause());
                ctx.response().setStatusCode(500).end("Unable to retrieve the info from C*");
            }
        });
    }
}

Conclusion

Dur­ing the se­ries, we have shown how the RSS reader app can be im­ple­mented with the Vert.x Cas­san­dra client.

Thanks for read­ing this. I hope you en­joyed this se­ries. See you soon on our Git­ter chan­nel!

Next post

HTTP response validation with the Vert.x Web Client

Learn how to use response predicates from the Vert.x Web module to validate HTTP responses and to automatically generate error messages.

Read more
Previous post

Eclipse Vert.x 3.6.0 released!

We are pleased to announce the Eclipse Vert.x 3.6.0 release.

Read more
Related posts

The RSS reader tutorial

In this tutorial, you will learn how to use the Eclipse Vert.x Cassandra client in practice. We will develop an RSS reader with three HTTP endpoints.

Read more

The RSS reader tutorial (Step 2)

In this second installment of our Vert.x Cassandra Client tutorial, we will add an endpoint that produces an array of RSS channels for a given user ID.

Read more

Building a real-time web app with Angular/Ngrx and Vert.x

There are multiple tech stacks to build a real-time web app. What are the best choices to build Angular client apps, connected to a JVM-based backend?

Read more