Prabhu Sunderaraman

Fullstack Engineer, Programmer, Writer, Trainer

prabhu.bits@gmail.com,     GitHub     Youtube     LinkedIn
  • Home
  • Profile
  • Books
  • Reading List
Browsing: / Home
Shortlink

DigitalOcean and Docker

By Prabhu Sunderaraman on October 19, 2017 in Cloud, Docker

I used to set up docker manually in an ubuntu instance in AWS and host the services. Very well knowing that it’s a tedious task, went through the grind because it didn’t give me problems. All good(or bad?) things have to come to and end some day. Right? While installing a service the instance was giving me issues of lack of space. So when I was trying to prune the volumes and clear some space, not sure what I did, my docker crashed.

“That’s it”, I said to myself. I am not going to setup docker anymore manually. Thought now was the time to pick up ECS(Elastic Container Service) in AWS for setting up docker instances. For some reason, I haven’t been able to get a hang of it. Common, I don’t want to learn a new syntax, play with more new configuration templates etc., All I want is an ubuntu instance with docker installed.

I created an account in Docker Cloud. But it still asked me to hook-up to somebody like AWS and enable a lot of things. I finally decided to check with DigitalOcean.

DigitalOcean has always marketed itself to be a 55 seconds setup person. All I wanted was create an instance(or droplet in DigitalOcean) with Docker installed, so I can focus on hosting my services. And yes, I found this.

I was able to create this droplet in about 55 seconds time. It took 2 minutes to install and have my service running. Most importantly the basic version I needed for this service costed me only 5$ per month.

Now, I am beginning to dig deeper into digitalocean for a number of services I am running. It’s just too simple.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Buffering responses in RxJava2

By Prabhu Sunderaraman on October 15, 2017 in .NET, Java 8, Reactive, Spring

In the previous post we created an Observable and emitted data using the onNext() method. We have a subscriber who receives the data when the publisher pushes the data. How about play with the data-receival a bit?

We have the temperature of four cities pushed one after the other to the subscriber. Let’s say, we would like to control the data received. We’ll use a simple yet effective method called buffer(). buffer() method has a plenty of overloaded versions. Let’s use some of them in this post.

Let’s buffer 2 results and release them to the subscriber as shown in the code below.

 public static void main(String[] args) throws Exception{
        Observable<String> producer = Observable.create(emitter -> {
                for(String city : cities){
                    String result = "Temperature of " + city + " is " + Math.random()*50 + " deg celsius";
                    emitter.onNext(result);
                }
        });
        producer
                .buffer(2)
                .subscribe((result) -> {
                    System.out.println("Received result");
                    result.forEach(r -> System.out.println(r));
                });
    }

In the code we have used the buffer(int) method to buffer two results. When you run the code, the output is here.

Received result
Temperature of Chennai is 21.098286787127048 deg celsius
Temperature of London is 36.91353784588999 deg celsius
Received result
Temperature of Paris is 41.135339668859615 deg celsius
Temperature of Rome is 29.937183626664833 deg celsius

We can buffer with time also. Let’s buffer the results for a particular timespan say 2 seconds as shown here.

 producer
                .buffer(500, TimeUnit.MILLISECONDS)
                .subscribe((result) -> {
                    System.out.println("Received result");
                    result.forEach(r -> System.out.println(r));
                });
        Thread.sleep(3000);

We are buffering the output for 500 milliseconds and releasing them. To avoid the main thread-exit I have added a sleep statement for 3 seconds. Here’s the output of this code.

Received result
Temperature of Chennai is 18.942744552146106 deg celsius
Temperature of London is 42.56709977653646 deg celsius
Temperature of Paris is 0.5756428440447103 deg celsius
Temperature of Rome is 10.926217595325788 deg celsius
Received result
Received result
Received result
Received result
Received result

So what we notice is, the subscriber’s result handler is invoked every 500 milliseconds. When there’s an output, it’s displayed. We can use both the count and timespan options as shown below.

producer
                .buffer(1,TimeUnit.SECONDS,3)
                .subscribe((result) -> {
                    System.out.println("Received result");
                    result.forEach(r -> System.out.println(r));
                });
        Thread.sleep(3000);

In this case, the publisher buffers for 1 second or till 3 items are received. The output of this code is shown below.

Received result
Temperature of Chennai is 15.55913859792542 deg celsius
Temperature of London is 49.949073211549475 deg celsius
Temperature of Paris is 32.1107414407108 deg celsius
Received result
Temperature of Rome is 15.025921328198798 deg celsius
Received result
Received result

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Creating Observable RxJava2

By Prabhu Sunderaraman on October 7, 2017 in Java 8, Reactive, Spring

In the previous post we wrote a HelloWorld program where we created a publisher using Observable and subscribed to it. Let’s extend this.

Let’s have a collection of cities. We would like to get the temperature of these cities. To begin with let’s keep it simple, and just generate a random number as the temperature of these cities. So our publisher will loop through the collection and generate a random number for each city and push it to the subscriber. And here’s the code.

package com.healthycoder;


import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

import java.util.Arrays;
import java.util.List;

public class HelloWorld {

    private static List<String> cities = Arrays.asList("Chennai","London","Paris","Rome");
    public static void main(String[] args) throws Exception{
        Observable<String> producer = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
                for(String city : cities){
                    String result = "Temperature of " + city + " is " + Math.random()*50 + " deg celsius";
                    observableEmitter.onNext(result);
                }
            }
        });
        producer.subscribe((result) -> System.out.println(result));
    }
}

We have created an Observable using the create() method. The create() method accepts an instance of ObservableOnSubscribe that has a subscribe() method. The subscribe() method is called when we create a subscriber. What’s interesting is the subscribe() method itself. If you want to push data to the subscriber we call the onNext() method.
Here’s the output when I ran the code.

Temperature of Chennai is 29.35073996653142 deg celsius
Temperature of London is 43.78882107284912 deg celsius
Temperature of Paris is 46.73974371173081 deg celsius
Temperature of Rome is 43.40540311264642 deg celsius

Let’s bring in lambda expressions and make this code more readable. My publisher with lambda syntax looks like this now.

  Observable<String> producer = Observable.create(emitter -> { 
        for(String city : cities){
             String result = "Temperature of " + city + " is " + Math.random()*50 + " deg celsius";
             emitter.onNext(result);
        }
  });

The code is lot better now. :)

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Hello Reactive programming with RxJava2

By Prabhu Sunderaraman on October 6, 2017 in Reactive, Spring

RxJava2 is the library for Reactive programming in Java. Let’s get started with a very simple piece of code.
In Reactive Programming, we have a publisher and a subscriber. Publisher publishes(or Pushes) data to a subscriber. When the subscriber receives data, it Reacts.

Include the RxJava2 library. The pom.xml is shown below. We need Java 8 and above.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.healthycoder</groupId>
    <artifactId>Reactive_Java</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>io.reactivex.rxjava2</groupId>
            <artifactId>rxjava</artifactId>
            <version>2.1.4</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

We’ll create a class HelloWorld and create a publisher and subscriber as shown below.

package com.healthycoder;

import io.reactivex.Observable;

public class HelloWorld {
    public static void main(String[] args) throws Exception{
        Observable<String> producer = Observable.just("Hello World"); 
        producer.subscribe(System.out::println); 
    }
}

Yes!!! You’re right. We are implementing Observer Pattern here. Reactive programming to begin with is termed as “Observer pattern done right”. We have created an Observable stream that pushes strings. We have a subscriber who just prints it!!!
Alright we have taken the first step into reactive world. Let’s take baby steps. One piece at a time :)

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Some useful points – React JS

By Prabhu Sunderaraman on June 26, 2017 in React JS

  • Component name should start in uppercase otherwise React cannot differentiate between normal HTML tag and React Component
  • Do not call setState() method in render
  • Do not modify the members of the state using this.state.xyz because render() doesn’t get called
  • If you modify a property of an object in state say, this.state.person.name, you don’t really have to call setState() if there’s any need for UI update
  • Calling setState() will not update the real DOM. It will create the Virtual DOM and diffing algorithm tells you whether the real DOM needs to be modified or not
  • Comments inside JSX are really harmful
  • Don’t over-complicate shouldComponentUpdate() by writing complex conditional statements to check if you have to call render or not. Keep it simple
  • If you want to write AJAX calls write them in componentDidMount() or componentDidUpdate(). These two functions should satisfy your needs, in most of the cases
  • Do not ignore the warning of not having unique “keys” in collection of JSX elements. The UI behaviour can get weird when you are trying to add/remove items dynamically
  • The setState() method on parent component automatically calls the render() method of the child components
Share this on: Mixx Delicious Digg Facebook Twitter
« Previous 1 … 20 21 22 … 64 Next »

Youtube Channel




Categories

  • JavaScript (48)
    • RequireJS (5)
  • Go (44)
  • Golang (44)
  • Ext JS (23)
  • Spring (22)
  • Mobile (21)
  • Scala (20)
    • Play (3)
  • Uncategorized (19)
  • Video Sessions (18)
  • GoG (17)
  • Sencha Touch (16)
  • jQuery (14)
  • Languages (13)
  • Java 8 (12)
  • React JS (11)
  • Kotlin (11)
  • HealthyCodeMagazine (9)
  • Video (9)
  • Objective-C (8)
  • NoSQL (8)
  • Android (7)
  • MongoDB (7)
  • GWT (6)
  • Tools (6)
  • HTML 5 (5)
  • Cloud (5)
  • General (5)
  • Micro services (5)
  • Java (5)
  • Books (4)
  • AWS (4)
  • Software Architecture (4)
  • .NET (3)
  • Elixir (3)
  • Docker (3)
  • Reactive (3)
  • NodeJS (2)
  • RoR (2)
  • Backbone (1)
  • AngularJS (1)

Archives

  • 2020 (49)
  • 2019 (7)
  • 2018 (34)
  • 2017 (15)
  • 2016 (7)
  • 2015 (18)
  • 2014 (31)
  • 2013 (55)
  • 2012 (100)

Search

Subscribe




Copyright © 2025 Prabhu Sunderaraman.

Powered by WordPress and News.