Go back to Read free chapters

Download Messaging application using Hashstag Component Tutorial

Understand and learn how Messaging using hashtags works as a backend application, download and run the application for your own Social Networking innovation

How to program a Messaging application using Hashtag component working as a Microservice. Download the code for free, run it and learn by doing.

Now that you have a good understanding in how the hashtag component functions we can continue to look it's structure in the coding level. In this chapter you can find a complete downloadable Messaging application that you can use as a component for your own personal learning purposes. The Messaging application works also as a base for any message delivery component. By putting it simply it parses the message, extracts the hashtags and stores both. It also opens up an API to find messages with certain hashtags.

 

The messaging application component is intended to be run inside a secured host machine. The data validation and application security are done before calling the Messaging application interface. So in this application we handle only verified requests. If you need to restrict access and validate the input messages more, do it before calling this internal messaging application API.

 

Learn Java, Spring Framework, Spring Boot, Databases, JSON REST API development

The downloadable project is created with Spring Framework Spring Boot technology and the hashtag component structure is created using Java with Hibernate language (JPA Java Persistent API) and Java Persistence Query Language (JPQL). The Spring Boot application runs Tomcat Servlet container web server with Default settings. And the data is stored in memory inside a H2 Database Engine. When the messaging component application starts it creates a new database in memory. When the service is shut down the data in memory is lost. The persistent database storage instructions will be offered to you in the following chapters.

 

Learn Docker containers and how to build an architecture using microservices

 

In the following chapter we will also introduce Docker container that you can easily use to start a new virtual host server running the messaging application using hashtags. This is the future architecture of Social Networking and in the following chapters we will introduce this new architecture in more depth.

 

How to start programming with Java, what will you need

 

We'll start of with what you're going to need in software. All of the software is free to download. So go on and get these.

- Java SE, Choose JDK Download from http://www.oracle.com/technetwork/java/javase/downloads/index.html

- Maven, https://maven.apache.org/download.cgi

- Eclipse IDE, https://eclipse.org/downloads

 

After installing these you're ready to launch!

Get the World's No 1 hashtag component for #free

 

And probably you're wondering where is this going? Let's say that for the beginning we're going to show you the structure of the world's number one pattern at the moment - and it's called Hashtags.

 

The idea of this book is that we are going to show you how to build is that you can combine the components together in an infinite number of ways to create totally new Social Netwoking applications with your own twist of lemon juice and spices.

 

Start now with Spring Boot

 

Yup.. there you go. Now let us get ahead and generate a new project. This will show you Spring Boot project generation.

 

How to make a new Spring Framework Spring Boot project

 

Head on to the site http://start.spring.io

 

And choose the following settings: Generate a Maven Project with Spring Boot 1.4.3 with these dependencies in the search box: Web, JPA, H2

 

Hit the Generate project button to make a new Spring boot project template. After the generation unzip the downloaded file and Import the project into Eclipse using Import Existing Maven Project. You can find help for all these steps in Google by searhing when needed.

 

Spring Framework has my favourite confuguration files of all time, the POM file (it's writter with CAPS because without it looks different). It holds all the necessary information on how to build and handle your project and what dependencies your project relies on.

 

How to setup Spring Boot with H2 in memory database

 

So let's Tweak the generated files a bit. First of all find the file named application.properties and add these configurations to it

 

spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=false

 

logging.level.org.hibernate.SQL=debug

server.port=8080

 

These properties will first generate or update the database schema files for you, open up a console for your in memory database to browse it. Use Debug logging when developing so you will see the output of the generated SQL sentences.

 

The server port is essential. On the host machine you have to choose an open port that the service reserves when it launches. If you have any problems launching a service, try to change the service port to a different one like 8181.

 

The H2 is an in memory database, so whenever you restart the service all data will disappear. I'll show you another way to configure the database with MySQL or PostgreSQL in the future chapters.

 

Now this is what you have been waiting for, go on and Download Messaging application with Hashtag component

 

Now you have a clean new project that you can start working on yourself and progress with your innovation. We have built an application for your convinience that you can download at the end of this chapter. This application is intended for learning and testing purposes. If you want to change the application's confurations or source files please write to info@weall.com and tell about your project. We will offer you another lisence that is suitable for your specific needs.

 

Now we have gotten into a point of no return. To examine the concept of Microservices in more detail. The Messaging application microservice is intended for short messages typically 140 characters and generally the messages are distributed with an Observer pattern.

 

The example code is written in mind that you can use it in your existing  or totally new applications. Use this method when you are innovating new services, building a startup or you need to implement new functionalities in your existing service. If you're new to coding this is an excellent way to start learning core structures in social networks.

 

Now let's go deeper into the technical side of the application.

 

How to store message data and hashtags into the database.

 

In this implementation we use Hibernate or Java Persistence API that maps the representation of the object to the database that can store the data. This is done with the Hashtag entity class. Entity means any object that can be stored for later use.

 

How to store hashtags into the database

 

The com.weall.domain.entity.hashtag.Hashtag class file holds a String called name. This is the key name of the hashtag.

 

@Column(nullable = false, length = 255)

private String name;

 

How Hashtags are indexed in the database

 

And the bread and butter in this example is the database index that's described in the previous chapter. The following annotation creates a new search index that can be used to make the search with the hashtag name faster.

 

@Table(name = "hashtag", indexes = { @Index(name = "nameindex", columnList = "name", unique = true) })

 

Generic content object can hold messages, images, videos, voice, location

 

Then we have the com.weall.domain.entity.content.Content class that represents any content that can be searched or grouped together with the hashtags name. Content can have messages, images, videos, voice, location or basically anything that you can think and build inside it.

 

In the messaging application we only use the message inside the content.

 

@Column(length = 1000)

private String message;

 

The message field is the textual representation that holds the hashtags inside. Another thing we need is to set up the link to the actual hashtag object. It's done with the following

 

@ManyToMany(fetch = FetchType.LAZY)

private List<Hashtag> hashtags = new ArrayList<Hashtag>();

 

This creates a new mapping table between the Content and Hashtag. You can read the detailed description of the mapping table in the article Content marking Implementation.

 

WOW!

 

That's basically the whole backend structure for the hashtags. The next thing that we need is a way how to fetch the hashtags and how to store them. So we need a way to create the new Content object, read and separate (parse) the hashtags from the text and link them to the Hashtag object.

 

How to fetch, parse and store hashtags into database

 

We have the service located in com.weall.service.content.ContentServiceImpl class file under the method addContent. This one method takes the message as input and uses the HashtagParserUtil to parse the text.

 

The HashtagParserUtil.getHashtagsFromMessage tokenizes (splits between whitespaces) the whole message so that we get an array of words.

 

StringTokenizer tokenizer = new StringTokenizer(message);

 

Then the method examines each word and looks what character does the word begin with. The HASHTAG is a constant variable that hold one character, the # hashtag character.

 

while (tokenizer.hasMoreTokens()) {

String hashtag = tokenizer.nextToken();

if (hashtag.startsWith(HASHTAG)) {

 

So if the word starts with a hashtag character then add the word into the list of hashtags.

 

List<String> hashtags = new ArrayList<String>();

 

...

hashtags.add(hashtag);

 

 

The class has also a method to get all the unique hashtags from the list getUniqueHashtags. So there will be no duplicate words in the list.

 

How hashtag logic works? Here's the answer

 

So back to the main method ContentServiceImpl.addContent where we will go through the list of unique hashtags. The logic is following: For each hashtag we search it from the database to find if it exists.

 

oldHashtag = hashtagRepository.findHashtagByName(hashtag);

 

If it exists then we add the hashtag that we found to the Content object's hashtags list.

 

newContent.getHashtags().add(oldHashtag);

 

If the hashtag isn't found in the database means that it's a new one so we create it, store it to the database and add it to the Content object's hashtags list.

 

Hashtag newHashtag = new Hashtag(hashtag);

newHashtag = hashtagRepository.save(newHashtag);

newContent.getHashtags().add(newHashtag);

 

And finally we save the Content object to the database. This also returns the saved Content object that actually is a new object. This means that the returned object instance is different from the object that we created with new Hashtag(). The returned object has the generated database ID that is the primary key for the database table.

 

return contentRepository.save(newContent);

 

How to find messages with a hashtag

 

Now we have the hashtag handling under control! So next we need a way to find those little buggers. And the search is the key.

 

In the next articles we will also describe a pattern for the content handling. To sort the content by it's relevancy.

 

To find the content we look into the future.. the future Conan? No actually not the Future but to the Content database table.

 

Let's create a query object that finds the content by joining the Content table to the Hashtags table by the hashtag id.

 

In the class ContentRepository there's one method that accesses the database and makes an SQL query to the database to find content based on the hashtags it's attached on.

 

List<Content> findFreshByHashtags(@Param("searchtags") List<Hashtag> searchtags, Pageable pageable);

 

How to page large data sets with Pageable

The findFreshByHashtags method's paramers use the Pageable object. With this object you can set the limit of returned objects from the database and an offset of the page number that the search will start. This is used in the ContentRepository to add the limit and offset to the SQL sentence. The ContentRepository extends the PagingAndSortingRepository from Spring Data that makes this possible.

Spring Data JPA @Query annotation to build a query for @ManyToMany relation

 

The method is annotated with the @Query annotation. The Content object has @ManyToMany reference to the Hashtag object so we must use join keyword to connect the content to the hashtags. Then we can use the hashtagjoin keword as a reference.

 

@Query("select e from Content e join e.hashtags hashtagjoin where hashtagjoin in :searchtags order by e.created desc")

 

You can leave some aliases out of the SQL and JPQL can handle it anyway. JPQL also handles the mathing of the database entities automatically by the primary keys of the entities, which are in this case the id fields. To make it more clear as SQL it will look like:

 

select e from content e join e.hashtags as hashtagjoin where hashtagjoin.id in (1,2,3) order by e.created desc

 

So there's the select from the Content table and it uses the table alias e. So in later expressions e will be the Content word.

 

And what the JPA does, it generates the whole SQL sentence using the current database scema language. In this example we have HQ in memory database but it can be just a normal MySQL or Oracle database instead.

 

Actually from the debugger you can see that the generated sentence will look like this.

 

select content0_.id as id1_0_, content0_.created as created2_0_, content0_.updated as updated3_0_, content0_.message as message4_0_ from content content0_ inner join content_hashtags hashtags1_ on content0_.id=hashtags1_.content_id inner join hashtag hashtag2_ on hashtags1_.hashtags_id=hashtag2_.id where hashtag2_.id in (?) order by content0_.created desc limit ?

 

We can make it a bit clearer if we replace the column names with a star. In SQL * means to select all columns.

 

select * from content content0_ inner join content_hashtags hashtags1_ on content0_.id=hashtags1_.content_id inner join hashtag hashtag2_ on hashtags1_.hashtags_id=hashtag2_.id where hashtag2_.id in (?) order by content0_.created desc limit ?

The @ManyToMany annotation generates a new content_hashtags juction table between the two entities and this query joins the first entity Content with the junction table and then with the Hashtag table.

 

How to build a JSON REST Messaging API with Hashtags

 

Now we have the core functions described, let's focus next on the REST interface that we have chosen to be the Spring MVC. In the example we have designed a REST interface based on the RESTful Service Best Practices recommendation By Todd Fredrich.

 

Thecom.weall.web.resource.MessagesResourceclass has all the interfaces to the content service. The getFreshContent method can be called with an HTTP GET request.

 

@RequestMapping(value = "/" + API_VERSION + "/messages", method = RequestMethod.GET)

public@ResponseBody List<Content> getFreshContent(@RequestParam(required = false) Integer limit,@RequestParam(required = false) Integer offset) {

returncontentService.getFreshContent(defaultPaging.createPageRequest(limit, offset));

}

 

And to add new content we use the addContent HTTP POST request.

 

@RequestMapping(value = "/" + API_VERSION + "/messages", method = RequestMethod.POST)

public@ResponseBody Content addContent(@RequestParam(required = true) String message) {

returncontentService.addContent(message);

}

 

Testing out the Messaging API

 

Start the service by opening up the command line and change the directory to the root folder where you have unzipped the source code. Run the application with Maven by the following command.

 

mvn spring-boot:run

 

Additional way is to run the application in Eclipse by starting the MessagingApplication with the Run as Java Application. 

 

The Messaging application launches and reserves by default the port 8080. You can change this in the application.properties file. To test the application open up your browser and head over to this link

 

http://localhost:8080/v0.1/messages

 

Sending a POST message to add message with hashtags using Postman

 

To send a POST request you will need an application to send a request. You can use for example the Google's Postman application for this. You can download it here https://www.getpostman.com/docs/introduction

 

Create a new POST request to your localhost with the address.

 

localhost:8080/v0.1/messages

 

And then write the message that you want to the POST message in the Body section with form-data.

 

Enter ”message” to the key field and ”#weall <3 code” to value field. And click Send.

 

Sending a GET request to query the newest messages with hashtags

 

After that you can call the http://localhost:8080/v0.1/messages with your browser to see the first message.

 

Sending a GET request to query the newest messages with a certain hashtag

 

By calling the folloging URL you do a query with the hashtag name as the last parameter in the URL. This returns a list of all messages that are connected to the hashtag.

 

localhost:8080/v0.1/messages/hashtags/weall

 

And so… You can also write us about your feedback or to the weall.com code lover site and we'll continue making the content based of your feedback.

 

In the following chapters we will discuss about building a mobile and web client application that uses the Messaging application interfaces. That chapter's intention is to allow you to build a mobile application and download it to your phone.

 

Below is the project that you can download and run with the instructions in this chapter.

 

DOWNLOAD HASHTAG MESSAGING APPLICATION API Version 0.0.1

DOWNLOAD HASHTAG MESSAGING APPLICATION API Version 0.0.1.3

- Fixed JUnit tests when running the package target with Docker build. Caused the subsequenced tests to fail.

DOWNLOAD HASHTAG MESSAGING APPLICATION API Version 0.0.2.1

- Changed messaging API version to v0.1, fixed the HQL query by adding the alias e to the select sentence

- Changes in article

Old Version New Version
@Query("from Content e join e.hashtags hashtagjoin where hashtagjoin in :searchtags order by e.created desc")  @Query("select e from Content e join e.hashtags hashtagjoin where hashtagjoin in :searchtags order by e.created desc")
API version v0.0.1 eg.  localhost:8080/v0.0.1/messages  API version v0.1 eg.  localhost:8080/v0.1/messages

 DOWNLOAD HASHTAG MESSAGING APPLICATION API Version 0.0.3

- Fixed error in setting default maximum offset for fetching messages. The offset was always 1000.

DOWNLOAD HASHTAG MESSAGING APPLICATION API Version 0.0.4

- Added Validation with twitter-text, custom validator and Java Validators, added HTML character escaping

 


What new ideas or thoughts this chapter gave you?

Hello
Fri 13.10.2017 10:29:25 from  Anonymous coder