Ten years ago, Netflix introduced the concept of reactive programming for Java programmers. It took years for the Core Java community to mature the RxJava stack. It took me a few more years of the cloud to evolve to realize how effective reactive programming can be. Here I write about, how reactive programming has helped in my running world.
The code snippet is an excellent sample for a headless API stack, the consumer here will pull the data from the ArrayList, and the consumer is waiting to do anything else.
class ListPull {
public static void main(String ring[] args) {
List<String> arrStrs = Arrays.asList("1", "2", "3");
for (String arrStr : arrStrs) {
System.out.println("The pull is for value "+ arrStr);
}
}
}
Now imagine where the data flows as a stream to the consumer. The consumer will not have to wait to consume this information. Sometimes this overlap with the concept of concurrent programming introduced in JAVA 8. Reactive streams are a different paradigm where instead of the consumer waiting to pull the data, the stream pushes data when it is ready to the consumer.
Here is a sample reactive stack project for the infamous running modules I have been blogging about. The use-case I want to talk about is I use Garmin Connect to track my run, the gear tracking information that comes default sometimes needs to be a more intuitive reminder where I have to switch the shoes. So I wanted to track the runs I made and put that into a shoe tracker bucket based on the day of the run. I alternate between 4 shoes for my training. So Monday runs, I want this tracker program to add the miles I put into the Red Shoes automatically. Similar Wednesday and Thursday runs, I want them to add to White and Green shoes. Saturday is my long run day, so I like the miles to be added to the grey shoe bucket. I run the shoe buckets in the cloud, so to save costs, I had to think of ways where I don’t have to poll the tracker changes. I had to flip my thoughts to a reactive stack where the tracker module will push the data to the cloud function when there is run on Monday, Wednesday, Thursday, or Saturday.

Am using Mongo for taking the data to store immediately am reading from the Garmin Connect.
package me.sathish.runcalcservice.cityofsp.repositories;
import me.sathish.runcalcservice.cityofsp.entities.CityOfSPRun;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CityOfSPRunRepository extends ReactiveMongoRepository<CityOfSPRun, String> {}
Spring Boot provides native support to the Mongo stack, extending the reactive MongoRepo. It immediately provides all the goods that you need to perform CRUD operations.
To automatically add the runs that I make based on the day, here is the declared service method.
public Mono<CityOfSPRunDTO> insertCitySPRun(
@RequestBody Mono<CityOfSPRunDTO> cityOfSPRunDTOMono) {
return cityOfSPRunDTOMono
.map(CityOfSPRunDTOUtil::toEntityCitySPRun)
.flatMap(this.cityOfSpRunRepository::insert)
.map(CityOfSPRunDTOUtil::toCitySPRunDTO);
}
The usage of Mono as you can imagine, am using a one record insert philosophy and if this has to be flux based for batch insert, spring reactive stack provided support.
Finally the controller code that is consumed by the native aws and azure serverless stack looks something like this below.
@GetMapping("{id}")
public Mono<ResponseEntity<CityOfSPRunDTO>> getCityOfSPRunByID (@PathVariable String id) {
return this.cityOfSpRunService
.findCitySPRunByID(id)
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.badRequest().build());
}
If you will need to see the entire project stack, the code is in GITHUB

You must be logged in to post a comment.