Skip to main content

Hands on Project Reactor

Cold Publisher
Creating publisher which emits update every second:

Flux.interval(Duration.ofSeconds(1L))
  .map(element->{
   return new Update(element.toString(),UUID.randomUUID().toString());
  }).doOnNext(onNext->{
   print("Publishing new element with key "+onNext.getKey());
  });

Note: complete code is availeble at following git repository git-repo
Above flux will only start emitting elements post subscription only, and for multiple subscriptions it will replay the elements from start.

Hot Publisher
Creating published which doesn't wait for subscribers to subscribe, instead keeps on emitting elements and subscribers will be able catch elements which are emitted post subscription as below:
  hotUpdate=DirectProcessor.create();
  new Thread(() -> {
   int state=0;
   do {
     print("generator with state "+state);
     hotUpdate.onNext(new Update(Integer.toString(state),UUID.randomUUID().toString())); 
     hotUpdate.delaySequence(Duration.ofSeconds(3));
     try {
     Thread.sleep(5000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    /*
     * if (state == 1000) { hotUpdate.onComplete(); break; }
     */ hotUpdate.doOnCancel(()->{
      print("doOnCancel event on hot Source");
      hotUpdate.onComplete();
     });
         state++;
         print("Is Terminated "+hotUpdate.isTerminated());
         if(hotUpdate.hasCompleted()) {
          break;
         }
   }while(!hotUpdate.isTerminated());
  }).start();



Comments

Popular posts from this blog

Communication among micro-services

Below are good articles to read on communication among micro-services. 1.  docs@microsoft.com 2.  https://dzone.com/articles/microservices-why-asynchronous-communications 3.  microservice-design-patterns 4.  https://vertx.io/docs/vertx-amqp-bridge/java/ 5.  developers.redhat.com/blog

Using Activiti with spring mvc

Follow below steps to create web application using spring mvc and activiti in background steps :1 create maven project with following pom 4.0.0 com.gk activitiMakerChecker war 0.0.1-SNAPSHOT activitiMakerChecker Maven Webapp http://maven.apache.org 4.2.4.RELEASE 1.6 4.0.5.RELEASE 1.2 3.1.0 4.0.1.Final 3.5.6-Final org.activiti activiti-engine 5.14 org.activiti activiti-spring 5.14 org.springframework spring-beans ${org.springframework.version} org.springframework spring-context 4.2.4.RELEASE org.springframework spring-jdbc 4.2.4.RELEASE org.springframework spring-tx 4.2.4.RELEASE org.springframework spring-orm ${org.springframework.version} org.slf4j slf4j-api 1.7.2 org.hibernate hibernate-core ${org.hibernate.version} org.hibernate hibernate-entitymanager ${org.h...

Reactive programming

As per wikipedia reactive programming is declarative programming paradigm concerned with data streams and the propagation of change. Reactive programming is about having an architecture which has below four key components: Message driven  Scalable Resilient Responsive There lot of libraries/frameworks available which enable doing reactive programming across programming languages for ex in java we have RxJava, Project Reactor. Also spring 5 on wards spring framework also supports reactive programming.