The RSS reader tutorial
This tutorial is dedicated for users who’d like to know how to use the Eclipse Vert.x Cassandra client in practice.
Before you start this tutorial
Before starting, you should
-
be familiar with Eclipse Vert.x. If you are not, here is the guide for Java developers.
-
have some basic understanding of databases. Extended knowledge of Cassandra is not required.
You also may find it useful to read the RSS 2.0 specification, because the resulted app is, basically, a storage of RSS 2.0 feeds.
To give you an idea of what the App is about, here is how it looks like from the fronted side:
In the image, we see that browser space is split into 2 parts:
- Saved feed names
- List of articles for the selected feed
Here you also can enter a link to a new feed, so the App will fetch and parse the feed. After that, it will appear in the left column along with other saved feeds.
Requirements
For completing this tutorial you need:
- Java 8 or higher
- Git
- 1 hour of your time
- You favorite code editor
For running the example you should ensure that Cassandra service is running locally on port 9042. As an option, you can run Cassandra with ccm(Cassandra Cluster Manager). Follow this instructions for installing ccm. After installing you will be able to run a single node cluster:
Before completing this step make sure that you have successfully cloned the RSS reader repository and checked out the step_1
branch:
Now you can try to tun this example and see if it works:
Schema
If you are familiar with Apache Cassandra, you should know that the way your data is stored in Cassandra is dependent on queries you are running. It means that you need first to figure out what kind of queries you will be running, and then you can produce a storage scheme.
In our case, we’d like our application to have 3 endpoints:
POST /user/{user_id}/rss_link
- for adding links to a user’s feedGET /user/{user_id}/rss_channels
- for retrieving information about RSS channels a user subscribed onGET /articles/by_rss_link?link={rss_link}
- for retrieving information about articles on a specific RSS channel
For implementing this endpoints, the schema should look as follows:
What to do in this step
In this step, we will implement only the first endpoint
Project overview
There are two notable classes in the project: AppVerticle
and FetchVerticle
. The first one is a Verticle responsible for HTTP request handling and storage schema initialization. The second one is a Verticle as well, but responsible for RSS feeds fetching.
The idea is simple. When the application is starting the AppVerticle
is deployed, then it tries to initialize storage schema, described in src/main/resources/schema.cql
file by reading it and executing listed queries line by line. After the schema initialization the AppVerticle
deploys FetchVerticle
and starts a HTTP server.
Implementing the endpoint
Now, it is time to implement the first endpoint. Pay attention to TODO
s, they are for pointing you out about where changes should be made.
Now, let’s have a look at the AppVerticle#postRssLink
method. This method is called each time the first endpoint is called, so we can figure out what is the posted body and id of the user, who performed the request, directly there. There are 2 main things we want to do in this method:
- Notifying via the Event Bus the
FetchVerticle
to fetch given by user link link to an RSS feed. - Inserting an entry to the
rss_by_user
table.
This is how the AppVerticle#postRssLink
method should be implemented:
You may notice that insertNewLinkForUser
is a PreparedStatement
, and should be initialized before the AppVerticle
start. Let’s do it in the AppVerticle#prepareNecessaryQueries
method:
Also, we should not forget to fetch a RSS by the link sent to FetchVerticle
via the Event Bus. We can do it in the FetchVerticle#startFetchEventBusConsumer
method:
And, finally, this code would not work if insertChannelInfo
and insertArticleInfo
statements will not be initialized at verticle start. Let’s to this in the FetchVerticle#prepareNecessaryQueries
method:
Observing
After all these changes, you should ensure that the first endpoint is working correctly. You need to run the application, go to localhost:8080 insert a link to a rss feed there(BBC UK feed news for example) and then click the ENTER button. Now you can connect to your local Cassandra instance, for instance with cqlsh, and find out how RSS feed data had been saved in the rss_reader keyspace:
Conclusion
In this article we figured out how to implement the first endpoint of RSS-reader app. If you have any problems with completing this step you can checkout to step_2
, where you can find all changes made for completing this step:
Thanks for reading this. I hope you enjoyed reading this article. See you soon on our Gitter channel!