<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://codeaches.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://codeaches.com/" rel="alternate" type="text/html" /><updated>2021-05-30T01:50:02-07:00</updated><id>https://codeaches.com/feed.xml</id><title type="html">Codeaches</title><subtitle>Spring Boot, Spring Cloud and Web Development tutorials
</subtitle><author><name>Pavan Gurudutt</name></author><entry><title type="html">Setup Kubernetes Cluster on Google Cloud Platform (GCP) and connect using Cloud SDK</title><link href="https://codeaches.com/kubernetes/setup-kubernetes-on-google-cloud-platform-and-connect-using-cloud-sdk" rel="alternate" type="text/html" title="Setup Kubernetes Cluster on Google Cloud Platform (GCP) and connect using Cloud SDK" /><published>2020-01-07T00:00:00-07:00</published><updated>2020-01-07T00:00:00-07:00</updated><id>https://codeaches.com/kubernetes/setup-kubernetes-on-google-cloud-platform-and-connect-using-cloud-sdk</id><content type="html" xml:base="https://codeaches.com/kubernetes/setup-kubernetes-on-google-cloud-platform-and-connect-using-cloud-sdk">&lt;p&gt;In this guide, let’s create a Kubernetes cluster using Google Cloud Platform (GCP) account and connect to this cluster using GCP Cloud SDK.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;&lt;strong&gt;Table of contents&lt;/strong&gt;&lt;/h3&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#create-a-google-cloud-platform-gcp-account&quot; id=&quot;markdown-toc-create-a-google-cloud-platform-gcp-account&quot;&gt;&lt;strong&gt;Create a Google Cloud Platform (GCP) account&lt;/strong&gt;&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#sing-up-for-google-cloud-platform&quot; id=&quot;markdown-toc-sing-up-for-google-cloud-platform&quot;&gt;&lt;strong&gt;Sing up for Google Cloud Platform&lt;/strong&gt;.&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#create-a-new-project&quot; id=&quot;markdown-toc-create-a-new-project&quot;&gt;&lt;strong&gt;Create a new project&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#create_kubernetes_cluster&quot; id=&quot;markdown-toc-create_kubernetes_cluster&quot;&gt;&lt;strong&gt;Create a new Kubernetes cluster on GCP&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#download_configure_sdk&quot; id=&quot;markdown-toc-download_configure_sdk&quot;&gt;&lt;strong&gt;Download and configure Google Cloud SDK on local PC&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#connect_to_kubernetes_cluster&quot; id=&quot;markdown-toc-connect_to_kubernetes_cluster&quot;&gt;&lt;strong&gt;Connect to GCP Kubernetes cluster using Google Cloud SDK&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#kubernetes-commandline-setup&quot; id=&quot;markdown-toc-kubernetes-commandline-setup&quot;&gt;&lt;strong&gt;Kubernetes commandline setup&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#try-out-basic-kubernetes-commands&quot; id=&quot;markdown-toc-try-out-basic-kubernetes-commands&quot;&gt;&lt;strong&gt;Try out basic kubernetes commands&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;create-a-google-cloud-platform-gcp-account&quot;&gt;&lt;strong&gt;Create a Google Cloud Platform (GCP) account&lt;/strong&gt;&lt;/h3&gt;

&lt;h4 id=&quot;sing-up-for-google-cloud-platform&quot;&gt;&lt;strong&gt;Sing up for Google Cloud Platform&lt;/strong&gt;.&lt;/h4&gt;

&lt;p&gt;The first step is to create an account in Google Cloud Platform. Log into Google Cloud Platform using your Gmail/Google account credentials &lt;strong&gt;&lt;a href=&quot;https://console.cloud.google.com/getting-started?login=true&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/kubernetes_cluster/gcp_signup.png&quot; alt=&quot;GCP Signup&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;create-a-new-project&quot;&gt;&lt;strong&gt;Create a new project&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;Traverse to &lt;strong&gt;Kubernetes Engine &amp;gt; Clusters&lt;/strong&gt; and create a new &lt;strong&gt;Project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/kubernetes_cluster/gcp_to_kubernetes_navigation.png&quot; alt=&quot;GCP Signup&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a new Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/kubernetes_cluster/gcp_to_create_project.png&quot; alt=&quot;GCP Signup&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here, I have created a new project called &lt;strong&gt;codeaches-project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/kubernetes_cluster/gcp_create_project.png&quot; alt=&quot;GCP Signup&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I have opted for a free trial to start using Kubernetes Engine&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/kubernetes_cluster/gcp_new_project.png&quot; alt=&quot;GCP Signup&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;create_kubernetes_cluster&quot;&gt;&lt;strong&gt;Create a new Kubernetes cluster on GCP&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Once, you subscribe to free trial, traverse to &lt;strong&gt;Kubernetes Engine &amp;gt; Clusters&lt;/strong&gt; and click on &lt;strong&gt;Create Cluster&lt;/strong&gt; to create a new kubernetes cluster.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/kubernetes_cluster/gcp_kubernetes_create_cluster.png&quot; alt=&quot;GCP Signup&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I have opted for default values where we get 3 nodes in a cluster.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/kubernetes_cluster/gcp_kubernetes_create_cluster_page_2.png&quot; alt=&quot;GCP Signup&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now we have a kubernetes cluster ready for us!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/kubernetes_cluster/gcp_kubernetes_create_cluster_created.png&quot; alt=&quot;GCP Signup&quot; /&gt;&lt;/p&gt;

&lt;p&gt;At this point, we have a cluster &lt;strong&gt;codeaches-cluster-1&lt;/strong&gt; in our project &lt;strong&gt;codeaches-project&lt;/strong&gt; on &lt;strong&gt;Google Cloud Platform&lt;/strong&gt;. Let’s move on to downloading the &lt;strong&gt;Google Cloud SDK&lt;/strong&gt; on our local PC which will help us with connecting to the above created cluster.&lt;/p&gt;

&lt;h3 id=&quot;download_configure_sdk&quot;&gt;&lt;strong&gt;Download and configure Google Cloud SDK on local PC&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Download the Google Cloud SDK installer using the link &lt;strong&gt;&lt;a href=&quot;https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once the installation is complete, open the command prompt and vefiry that the Cloud SDK is setup.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; gcloud version
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Google Cloud SDK 275.0.0
bq 2.0.51
core 2020.01.03
gsutil 4.46
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;connect_to_kubernetes_cluster&quot;&gt;&lt;strong&gt;Connect to GCP Kubernetes cluster using Google Cloud SDK&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Use the gcloud init command to perform several common SDK setup tasks. These include authorizing the SDK tools to access Google Cloud Platform using your user account credentials and setting up the default SDK configuration.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; gcloud init
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

You must log in to continue. Would you like to log in (Y/n)?  y

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?....................


You are logged in as: [mail@somedomain.com].

Pick cloud project to use:
 [1] secret-argon-123456
 [2] Create a new project
Please enter numeric choice or text value (must exactly match list
item):  1

Your current project has been set to: [secret-argon-123456].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note that the project ID &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;secret-argon-123456&lt;/code&gt; corresponds to the earlier created project with name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;codeaches-project&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;secret-argon-123456&lt;/code&gt; is a random ID assigned by Google Cloud Platform.&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; gcloud projects list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;PROJECT_ID           NAME               PROJECT_NUMBER
secret-argon-123456  codeaches-project  654321123456
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;kubernetes-commandline-setup&quot;&gt;&lt;strong&gt;Kubernetes commandline setup&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Earlier, we had created a kubernetes cluster named &lt;strong&gt;codeaches-cluster-1&lt;/strong&gt; using the Google Cloud Platform UI.&lt;/p&gt;

&lt;p&gt;To access the Kubernetes Engine cluster, we need kubernetes commandline. Let’s install the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; gcloud components &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;kubectl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once, kubectl is installed successfully, configure the local &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl&lt;/code&gt; to point to &lt;strong&gt;codeaches-cluster-1&lt;/strong&gt; on GCP.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; gcloud container clusters get-credentials codeaches-cluster-1 &lt;span class=&quot;nt&quot;&gt;--zone&lt;/span&gt; us-central1-a &lt;span class=&quot;nt&quot;&gt;--project&lt;/span&gt; secret-argon-123456
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Fetching cluster endpoint and auth data.
kubeconfig entry generated for codeaches-cluster-1.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Now you are all set to use the locally installed Cloud SDK to interact with Kubernetes Cluster on GCP&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;try-out-basic-kubernetes-commands&quot;&gt;&lt;strong&gt;Try out basic kubernetes commands&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Get cluster details&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; kubectl cluster-info
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Kubernetes master is running at https://33.211.100.10
GLBCDefaultBackend is running at https://33.211.100.10/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy
Heapster is running at https://33.211.100.10/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://33.211.100.10/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://33.211.100.10/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Get node details&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; kubectl get nodes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;NAME                                                 STATUS   ROLES    AGE     VERSION
gke-codeaches-cluster-1-default-pool-38905352-080c   Ready    &amp;lt;none&amp;gt;   8m31s   v1.12.8-gke.10
gke-codeaches-cluster-1-default-pool-38905352-km5p   Ready    &amp;lt;none&amp;gt;   8m31s   v1.12.8-gke.10
gke-codeaches-cluster-1-default-pool-38905352-mk4c   Ready    &amp;lt;none&amp;gt;   8m31s   v1.12.8-gke.10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;summary&quot;&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;This concludes our guide to creating creating Kubernetes cluster on GCP and connecting to it through Cloud SDK running on our local machine!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Google Cloud Platform" /><category term="Kubernetes" /><summary type="html">In this guide, let’s create a Kubernetes cluster using Google Cloud Platform (GCP) account and connect to this cluster using GCP Cloud SDK.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Integrate embedded Apache ActiveMQ 5 (Classic) JMS Broker with Spring Boot application</title><link href="https://codeaches.com/spring-boot/embedded-activemq-5-jms-broker" rel="alternate" type="text/html" title="Integrate embedded Apache ActiveMQ 5 (Classic) JMS Broker with Spring Boot application" /><published>2020-01-06T00:00:00-07:00</published><updated>2020-02-01T00:00:00-07:00</updated><id>https://codeaches.com/spring-boot/spring-boot-embedded-activemq-5-jms-integration</id><content type="html" xml:base="https://codeaches.com/spring-boot/embedded-activemq-5-jms-broker">&lt;p&gt;Apache ActiveMQ is the most popular open source JMS server. ActiveMQ supports Spring for configuration of the JMS client side as well as for configuring the JMS Message Broker.&lt;/p&gt;

&lt;p&gt;In this post, let’s integrate an embedded ActiveMQ JMS 5 broker (Classic version as they call it) with a simple Spring Boot application which sends/recieves data from the embedded ActiveMQ broker.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;&lt;strong&gt;Table of contents&lt;/strong&gt;&lt;/h3&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#create-spring-boot-starter-application&quot; id=&quot;markdown-toc-create-spring-boot-starter-application&quot;&gt;&lt;strong&gt;Create spring boot starter application&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#configure-additional-project-dependencies-in-pomxml&quot; id=&quot;markdown-toc-configure-additional-project-dependencies-in-pomxml&quot;&gt;&lt;strong&gt;Configure additional project dependencies in pom.xml&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#configure-embedded-activemq-broker&quot; id=&quot;markdown-toc-configure-embedded-activemq-broker&quot;&gt;&lt;strong&gt;Configure Embedded ActiveMQ Broker&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#configure-jms-producer&quot; id=&quot;markdown-toc-configure-jms-producer&quot;&gt;&lt;strong&gt;Configure JMS Producer&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#configure-jms-consumer&quot; id=&quot;markdown-toc-configure-jms-consumer&quot;&gt;&lt;strong&gt;Configure JMS Consumer&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#test-jms-producer-and-consumer&quot; id=&quot;markdown-toc-test-jms-producer-and-consumer&quot;&gt;&lt;strong&gt;Test JMS producer and consumer&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;create-spring-boot-starter-application&quot;&gt;&lt;strong&gt;Create spring boot starter application&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Building the bare bone Spring Boot Service is simple when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spring Initializr&lt;/code&gt; is used. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spring Initializr&lt;/code&gt; generates spring boot project with just what you need to start quickly! Let’s start off with one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a Spring Boot starter project using Spring Initializr&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s utilize the pre-configured &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spring Initializr&lt;/code&gt; which is available &lt;strong&gt;&lt;a href=&quot;https://start.spring.io/#!type=maven-project&amp;amp;language=java&amp;amp;platformVersion=2.2.4.RELEASE&amp;amp;packaging=jar&amp;amp;jvmVersion=13&amp;amp;groupId=com.codeaches&amp;amp;artifactId=embedded-activemq-5-jms-broker&amp;amp;name=embedded-activemq-5-jms-broker&amp;amp;description=demo%20project%20for%20spring%20boot&amp;amp;packageName=com.codeaches.activmq.embedded&amp;amp;dependencies=web,activemq&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt; to create &lt;strong&gt;embedded-activemq-5-jms-broker&lt;/strong&gt; starter project.&lt;/p&gt;

&lt;p&gt;Click on Generate Project. This downloads a zip file containing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;embedded-activemq-5-jms-broker&lt;/code&gt; project. Import the project to your IDE.&lt;/p&gt;

&lt;h3 id=&quot;configure-additional-project-dependencies-in-pomxml&quot;&gt;&lt;strong&gt;Configure additional project dependencies in pom.xml&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;We need an additional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;activemq-broker&lt;/code&gt; dependency which will give all the classed needed for setting up an embedded ActiveMQ JMS 5 broker.&lt;/p&gt;

&lt;p&gt;Uptate pom.xml with below dependencies. Here we are adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;activemq-pool&lt;/code&gt; dependency as well to create a pooled connection factory.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pom.xml&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.activemq&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;activemq-broker&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.activemq&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;activemq-pool&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s add broker URL in application.properties. In this guide, we shall embed an active mq broker to run on port 61616. 
We also need a queue to send/recieve messages. We shall configure this queue name as well here.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/main/resources/application.properties&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-properties highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;py&quot;&gt;activemq.broker.url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;tcp://0.0.0.0:61616&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;activemq.queue.name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;my-queue-1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;configure-embedded-activemq-broker&quot;&gt;&lt;strong&gt;Configure Embedded ActiveMQ Broker&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Create an Active MQ embedded broker as shown below. This broker will be started when the application starts up.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.codeaches.activmq.embedded.JmsConfig.java&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Configuration&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JmsConfig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@Value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${activemq.broker.url}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brokerUrl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@Bean&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BrokerService&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;broker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nc&quot;&gt;BrokerService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;broker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BrokerService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;broker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setPersistent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;broker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setUseJmx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;broker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;addConnector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brokerUrl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;broker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;configure-jms-producer&quot;&gt;&lt;strong&gt;Configure JMS Producer&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s use Spring’s JmsTemplate to send messages to the embedded broker. We shall also use a pooled connection factory for our JmsTemplate as shown below.&lt;/p&gt;

&lt;p&gt;Update &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsConfig.java&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsTemplate&lt;/code&gt; bean.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.codeaches.activmq.embedded.JmsConfig.java&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;nd&quot;&gt;@Bean&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JmsTemplate&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;jmsTemplate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;JmsTemplate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PooledConnectionFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brokerUrl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Create a producer class which uses the above created &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsTemplate&lt;/code&gt;  to send the data to embedded Active MQ server.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.codeaches.activmq.embedded.JmsProducer.java&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Service&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JmsProducer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nc&quot;&gt;Logger&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LoggerFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JmsProducer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@Autowired&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JmsTemplate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jmsTemplate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@Value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${activemq.queue.name}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;destination&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;jmsTemplate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;convertAndSend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;destination&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Sent message='{}'&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;configure-jms-consumer&quot;&gt;&lt;strong&gt;Configure JMS Consumer&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s use Spring’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsListener&lt;/code&gt; to read the messages from the JMS. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsListener&lt;/code&gt; needs a connection factory. We shall use pooled connection factory in the consumer as well.&lt;/p&gt;

&lt;p&gt;Update &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsConfig.java&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultJmsListenerContainerFactory&lt;/code&gt; bean.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.codeaches.activmq.embedded.JmsConfig.java&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;nd&quot;&gt;@Bean&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultJmsListenerContainerFactory&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;jmsListenerContainerFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nc&quot;&gt;DefaultJmsListenerContainerFactory&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factory&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultJmsListenerContainerFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;factory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setConnectionFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PooledConnectionFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brokerUrl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Create a consumer class which uses Spring’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsListener&lt;/code&gt; to recieve the data from embedded Active MQ server.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.codeaches.activmq.embedded.JmsConsumer.java&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Service&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JmsConsumer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nc&quot;&gt;Logger&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LoggerFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JmsConsumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@JmsListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;destination&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;${activemq.queue.name}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;receive&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Received message='{}'&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that we have both producer and consumer, let’s build a simple webservice method which uses earlier created JMS producer to send data to the queue &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my-queue-1&lt;/code&gt; as shown below.&lt;/p&gt;

&lt;p&gt;Here, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sendDataToJms(message)&lt;/code&gt; webservice method forwards the message sent by the caller to JMS queue.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.codeaches.activmq.embedded.MyRestController.java&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@RestController&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyRestController&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@Autowired&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;JmsProducer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jmsProducer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@PostMapping&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/send&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sendDataToJms&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@RequestParam&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;jmsProducer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Start the application&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Tomcat console log:&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
Using Persistence Adapter: MemoryPersistenceAdapter
Apache ActiveMQ 5.15.10 (localhost, ID:DESKTOP-MYPCNAME-53882-1325267078594-0:1) is starting
Listening for connections at: tcp://DESKTOP-MYPCNAME:61616
Connector tcp://DESKTOP-MYPCNAME:61616 started
Apache ActiveMQ 5.15.10 (localhost, ID:DESKTOP-MYPCNAME-53882-1325267078594-0:1) started
For help or more information please see: http://activemq.apache.org
Initializing ExecutorService 'applicationTaskExecutor'
Tomcat started on port(s): 8080 (http) with context path ''
Started EmbeddedJmsBrokerApplication in 2.17 seconds (JVM running for 3.17)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;test-jms-producer-and-consumer&quot;&gt;&lt;strong&gt;Test JMS producer and consumer&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s invoke the webservice by sending a hello message. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsProducer&lt;/code&gt; would send the message and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JmsConsumer&lt;/code&gt; will consume the hello message.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl -i -X POST http://localhost:8080/send?message=hello
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Tomcat Log&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;INFO com.codeaches.activmq.embedded.JmsProducer   : Sent message='hello'
INFO com.codeaches.activmq.embedded.JmsConsumer   : Received message='hello'
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;summary&quot;&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;This concludes our guide to integrating an embedded Apache ActiveMQ JMS with a simple Spring Boot application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Apache ActiveMQ" /><summary type="html">Apache ActiveMQ is the most popular open source JMS server. ActiveMQ supports Spring for configuration of the JMS client side as well as for configuring the JMS Message Broker. In this post, let’s integrate an embedded ActiveMQ JMS 5 broker (Classic version as they call it) with a simple Spring Boot application which sends/recieves data from the embedded ActiveMQ broker.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Create a stream on Pivotal Cloud Foundry (PCF) using Spring Cloud Data Flow (SCDF) server</title><link href="https://codeaches.com/spring-cloud/create-a-stream-on-pcf-using-spring-cloud-data-flow-server" rel="alternate" type="text/html" title="Create a stream on Pivotal Cloud Foundry (PCF) using Spring Cloud Data Flow (SCDF) server" /><published>2020-01-03T00:00:00-07:00</published><updated>2020-01-03T00:00:00-07:00</updated><id>https://codeaches.com/spring-cloud/create-a-stream-on-pcf-using-data-flow-server</id><content type="html" xml:base="https://codeaches.com/spring-cloud/create-a-stream-on-pcf-using-spring-cloud-data-flow-server">&lt;p&gt;In this tutorial, let’s use Spring provided HTTP source and LOG sink applications to create a stream and deploy it to cloudfoundry. We shall use SCDF server running locally on our machine to deploy the stream to cloudfoundry.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;&lt;strong&gt;Table of contents&lt;/strong&gt;&lt;/h3&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#prerequisites&quot; id=&quot;markdown-toc-prerequisites&quot;&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#start-scdf-shell-cli-on-your-pc&quot; id=&quot;markdown-toc-start-scdf-shell-cli-on-your-pc&quot;&gt;&lt;strong&gt;Start SCDF Shell CLI on your PC&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#register-the-out-of-the-box-http-and-log-spring-boot-apps&quot; id=&quot;markdown-toc-register-the-out-of-the-box-http-and-log-spring-boot-apps&quot;&gt;&lt;strong&gt;Register the out-of-the-box &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; spring boot apps&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#create-the-stream&quot; id=&quot;markdown-toc-create-the-stream&quot;&gt;&lt;strong&gt;Create the stream&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deploy-the-stream&quot; id=&quot;markdown-toc-deploy-the-stream&quot;&gt;&lt;strong&gt;Deploy the stream&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#validate-the-deployed-stream&quot; id=&quot;markdown-toc-validate-the-deployed-stream&quot;&gt;&lt;strong&gt;Validate the deployed stream&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#test-httplogstream-stream-using-pcf-cli&quot; id=&quot;markdown-toc-test-httplogstream-stream-using-pcf-cli&quot;&gt;&lt;strong&gt;Test &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;httpLogStream&lt;/code&gt; stream using PCF CLI&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;prerequisites&quot;&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;An account on Pivotal Cloud Foundry (PCF). You can create one &lt;strong&gt;&lt;a href=&quot;https://console.run.pivotal.io/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;PCF Command Line Interface (CLI) installed on your computer. PCF CLI can be found in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tools&lt;/code&gt; section of your &lt;strong&gt;&lt;a href=&quot;https://console.run.pivotal.io/tools&quot; target=&quot;_blank&quot;&gt;PCF account&lt;/a&gt;&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;An up and running SCDF server on PCF. You can follow the tutorial &lt;strong&gt;&lt;a href=&quot;/spring-cloud/install-spring-cloud-dataflow-server-and-skipper-server&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt; to install SCDF server on PCF.&lt;/li&gt;
  &lt;li&gt;Download SCDF CLI jar to your PC. You can follow the tutorial &lt;strong&gt;&lt;a href=&quot;/spring-cloud/install-spring-cloud-dataflow-server-and-skipper-server&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt; to download.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;start-scdf-shell-cli-on-your-pc&quot;&gt;&lt;strong&gt;Start SCDF Shell CLI on your PC&lt;/strong&gt;&lt;/h3&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;java &lt;span class=&quot;nt&quot;&gt;-jar&lt;/span&gt; spring-cloud-dataflow-shell-2.3.0.RELEASE.jar &lt;span class=&quot;nt&quot;&gt;--dataflow&lt;/span&gt;.uri&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;https://my-codeaches-scdf-server.cfapps.io &lt;span class=&quot;nt&quot;&gt;--dataflow&lt;/span&gt;.username&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;user001 &lt;span class=&quot;nt&quot;&gt;--dataflow&lt;/span&gt;.password&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;pass001 &lt;span class=&quot;nt&quot;&gt;--skip-ssl-validation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;  ____                              ____ _                __
 / ___| _ __  _ __(_)_ __   __ _   / ___| | ___  _   _  __| |
 \___ \| '_ \| '__| | '_ \ / _` | | |   | |/ _ \| | | |/ _` |
  ___) | |_) | |  | | | | | (_| | | |___| | (_) | |_| | (_| |
 |____/| .__/|_|  |_|_| |_|\__, |  \____|_|\___/ \__,_|\__,_|
  ____ |_|    _          __|___/                 __________
 |  _ \  __ _| |_ __ _  |  ___| | _____      __  \ \ \ \ \ \
 | | | |/ _` | __/ _` | | |_  | |/ _ \ \ /\ / /   \ \ \ \ \ \
 | |_| | (_| | || (_| | |  _| | | (_) \ V  V /    / / / / / /
 |____/ \__,_|\__\__,_| |_|   |_|\___/ \_/\_/    /_/_/_/_/_/

2.3.0.RELEASE

Welcome to the Spring Cloud Data Flow shell. For assistance hit TAB or type &quot;help&quot;.
Successfully targeted https://my-codeaches-scdf-server.cfapps.io
dataflow:&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;register-the-out-of-the-box-http-and-log-spring-boot-apps&quot;&gt;&lt;strong&gt;Register the out-of-the-box &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; spring boot apps&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Cloudfoundry provides with few out-of-the-box source and sink spring boot applications which can be used for stream creation. Lets register the out-of-the-box &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; spring boot apps, specific to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit&lt;/code&gt; messaging broker, in SCDF server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Register HTTP Source Spring Boot App&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dataflow:&amp;gt;app register &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt; http &lt;span class=&quot;nt&quot;&gt;--type&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--uri&lt;/span&gt; maven://org.springframework.cloud.stream.app:http-source-rabbit:2.1.1.RELEASE
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Successfully registered application 'source:http'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Register LOG Sink Spring Boot App&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dataflow:&amp;gt;app register &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt; log &lt;span class=&quot;nt&quot;&gt;--type&lt;/span&gt; sink &lt;span class=&quot;nt&quot;&gt;--uri&lt;/span&gt; maven://org.springframework.cloud.stream.app:log-sink-rabbit:2.1.2.RELEASE
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Successfully registered application 'sink:log'
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;create-the-stream&quot;&gt;&lt;strong&gt;Create the stream&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s utilize the above registered apps &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; to create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http|log&lt;/code&gt; stream using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stream create&lt;/code&gt; command on SCDF shell terminal.&lt;/p&gt;

&lt;p&gt;This stream  will take HTTP POST request and prints the body in log file.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dataflow:&amp;gt;stream create &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt; httpLogStream &lt;span class=&quot;nt&quot;&gt;--definition&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;http | log&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Created new stream 'httpLogStream'
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;deploy-the-stream&quot;&gt;&lt;strong&gt;Deploy the stream&lt;/strong&gt;&lt;/h3&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dataflow:&amp;gt;stream deploy httpLogStream &lt;span class=&quot;nt&quot;&gt;--platformName&lt;/span&gt; dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Deployment request has been sent for stream 'httpLogStream'
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;validate-the-deployed-stream&quot;&gt;&lt;strong&gt;Validate the deployed stream&lt;/strong&gt;&lt;/h3&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dataflow:&amp;gt;stream validate &lt;span class=&quot;s1&quot;&gt;'httpLogStream'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;╔═════════════╤═════════════════╗
║ Stream Name │Stream Definition║
╠═════════════╪═════════════════╣
║httpLogStream│http | log       ║
╚═════════════╧═════════════════╝


httpLogStream is a valid stream.
╔═══════════╤═════════════════╗
║ App Name  │Validation Status║
╠═══════════╪═════════════════╣
║source:http│valid            ║
║sink:log   │valid            ║
╚═══════════╧═════════════════╝
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dataflow:&amp;gt;stream info &lt;span class=&quot;s1&quot;&gt;'httpLogStream'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;╔═════════════╤═════════════════╤═══════════╤════════╗
║ Stream Name │Stream Definition│Description│ Status ║
╠═════════════╪═════════════════╪═══════════╪════════╣
║httpLogStream│http | log       │           │deployed║
╚═════════════╧═════════════════╧═══════════╧════════╝

Stream Deployment properties: {
  &quot;log&quot; : {
    &quot;resource&quot; : &quot;maven://org.springframework.cloud.stream.app:log-sink-rabbit:jar&quot;,
    &quot;spring.cloud.deployer.group&quot; : &quot;httpLogStream&quot;,
    &quot;version&quot; : &quot;2.1.2.RELEASE&quot;
  },
  &quot;http&quot; : {
    &quot;resource&quot; : &quot;maven://org.springframework.cloud.stream.app:http-source-rabbit:jar&quot;,
    &quot;spring.cloud.deployer.group&quot; : &quot;httpLogStream&quot;,
    &quot;version&quot; : &quot;2.1.1.RELEASE&quot;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;test-httplogstream-stream-using-pcf-cli&quot;&gt;&lt;strong&gt;Test &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;httpLogStream&lt;/code&gt; stream using PCF CLI&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;Log into PCF using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cf login&lt;/code&gt; command.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cf login &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; api.run.pivotal.io &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;you@some-domain.com&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;yourpassword&quot;&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;your-org&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;dev&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;API endpoint: api.run.pivotal.io
Authenticating...
OK

Targeted org your-org
Targeted space dev

API endpoint:   https://api.run.pivotal.io (API version: 2.138.0)
User:           you@some-domain.com
Org:            your-org
Space:          dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Once the stream creation and deployment is successful, PCF creates random routes (urls) for both log and sink applications which can be validated using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cf apps&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cf apps
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Getting apps in org your-org / space dev as you@some-domain.com...
OK

name                            requested state   instances   memory   disk   urls
NEe1JMP-httpLogStream-http-v1   started           1/1         2G       1G     NEe1JMP-httpLogStream-http-v1.cfapps.io
NEe1JMP-httpLogStream-log-v1    started           1/1         2G       1G     NEe1JMP-httpLogStream-log-v1.cfapps.io
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Post a sample message to the stream&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Post a sample &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello world&lt;/code&gt; message to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http&lt;/code&gt; application using the route &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NEe1JMP-httpLogStream-http-v1.cfapps.io&lt;/code&gt; as shown below. The message will be picked up by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http&lt;/code&gt; app and passed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; application.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Content-Type:application/text&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; POST &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'hello world'&lt;/span&gt; https://NEe1JMP-httpLogStream-http-v1.cfapps.io
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;HTTP/1.1 202 Accepted
Date: Sat, 04 Jan 2020 01:58:19 GMT
X-Vcap-Request-Id: c94b6a06-f3e2-4982-7f4b-34fbe3664713
Content-Length: 0
Connection: keep-alive
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once the message is posted successfully, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello world&lt;/code&gt; will be printed in the logs of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tail the log of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; application using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cf logs&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tail the log of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data-flow-server-hd6lIb0-httpLogStream-log&lt;/code&gt; application using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cf logs&lt;/code&gt; command.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cf logs &lt;span class=&quot;nt&quot;&gt;--recent&lt;/span&gt; NEe1JMP-httpLogStream-log-v1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;[httpLogStream-1] NEe1JMP-httpLogStream-log-v1             : hello world
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;summary&quot;&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Congratulations! You just deployed a stream to cloudfoundry using Spring Cloud Dataflow Server (SCDF).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Spring Cloud Stream" /><category term="Spring Cloud Dataflow" /><category term="Spring Cloud Skipper" /><category term="Pivotal Cloud Foundry" /><summary type="html">In this tutorial, let’s use Spring provided HTTP source and LOG sink applications to create a stream and deploy it to cloudfoundry. We shall use SCDF server running locally on our machine to deploy the stream to cloudfoundry.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Install Spring Cloud Data Flow (SCDF) server and Skipper Server on Pivotal Cloud Foundry (PCF)</title><link href="https://codeaches.com/spring-cloud/install-spring-cloud-dataflow-server-and-skipper-server" rel="alternate" type="text/html" title="Install Spring Cloud Data Flow (SCDF) server and Skipper Server on Pivotal Cloud Foundry (PCF)" /><published>2020-01-03T00:00:00-07:00</published><updated>2020-01-03T00:00:00-07:00</updated><id>https://codeaches.com/spring-cloud/install-data-flow-server-and-skipper-server</id><content type="html" xml:base="https://codeaches.com/spring-cloud/install-spring-cloud-dataflow-server-and-skipper-server">&lt;p&gt;In this tutorial, let’s configure and deploy spring cloud data flow server to pivotal cloudfoundry (PCF). For deploying streams, the Data Flow Server delegates the deployment work to Skipper Server. Hence, we shall configure and deploy Skipper server as well.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;&lt;strong&gt;Table of contents&lt;/strong&gt;&lt;/h3&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#prerequisites&quot; id=&quot;markdown-toc-prerequisites&quot;&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#provision-services-needed-for-skipper-server&quot; id=&quot;markdown-toc-provision-services-needed-for-skipper-server&quot;&gt;&lt;strong&gt;Provision services needed for Skipper server&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deploy-skipper-server&quot; id=&quot;markdown-toc-deploy-skipper-server&quot;&gt;&lt;strong&gt;Deploy Skipper server&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#provision-services-needed-for-scdf-server&quot; id=&quot;markdown-toc-provision-services-needed-for-scdf-server&quot;&gt;&lt;strong&gt;Provision services needed for SCDF server&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deploy-scdf-server&quot; id=&quot;markdown-toc-deploy-scdf-server&quot;&gt;&lt;strong&gt;Deploy SCDF server&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#setup-scdf-shell-cli-on-your-pc&quot; id=&quot;markdown-toc-setup-scdf-shell-cli-on-your-pc&quot;&gt;&lt;strong&gt;Setup SCDF Shell CLI on your PC&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#setup-skipper-shell-cli-on-your-pc&quot; id=&quot;markdown-toc-setup-skipper-shell-cli-on-your-pc&quot;&gt;&lt;strong&gt;Setup Skipper Shell CLI on your PC&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;prerequisites&quot;&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;An account on Pivotal Cloud Foundry (PCF). You can create one &lt;a href=&quot;https://console.run.pivotal.io/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;PCF Command Line Interface (CLI) installed on your computer. PCF CLI can be found in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tools&lt;/code&gt; section of your &lt;a href=&quot;https://console.run.pivotal.io/tools&quot; target=&quot;_blank&quot;&gt;PCF account&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;provision-services-needed-for-skipper-server&quot;&gt;&lt;strong&gt;Provision services needed for Skipper server&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Log into your PCF account using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cf&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cf login &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; api.run.pivotal.io &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;you@some-domain.com&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;yourpassword&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;your-org&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;util&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;API endpoint: api.run.pivotal.io
Authenticating...
OK
Targeted org your-org
Targeted space util

API endpoint:   https://api.run.pivotal.io (API version: 2.128.0)
User:           you@some-domain.com
Org:            your-org
Space:          util
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Provision Postgresql&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Skipper server uses an RDBMS to store state. For our example, we shall use Postgresql. Schemas on Postgresql DB will be created on Skipper server startup.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cf create-service elephantsql turtle my_postgresql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Creating service instance my_postgresql in org your-org / space util as you@some-domain.com...
OK
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;deploy-skipper-server&quot;&gt;&lt;strong&gt;Deploy Skipper server&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Download Skipper server&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://repo.spring.io/release/org/springframework/cloud/spring-cloud-skipper-server/2.2.1.RELEASE/spring-cloud-skipper-server-2.2.1.RELEASE.jar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Configure Skipper properties&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;skipper-server-manifest.yml&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-yml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;applications&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;skipper-server&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;random-route&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1G&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;disk_quota&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1G&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;spring-cloud-skipper-server-2.2.1.RELEASE.jar&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;buildpack&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;java_buildpack_offline&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my-codeaches-skipper-server.cfapps.io&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;JBP_CONFIG_OPEN_JDK_JRE&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;jre:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;version:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;13.+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;}'&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;SPRING_APPLICATION_NAME&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;skipper-server&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;SPRING_PROFILES_ACTIVE&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cloud&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;JBP_CONFIG_SPRING_AUTO_RECONFIGURATION&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{enabled:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;false}'&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_ENABLE_LOCAL_PLATFORM&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;false&lt;/span&gt;

    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_CONNECTION_URL&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;https://api.run.pivotal.io&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_CONNECTION_ORG&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;your-org&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_CONNECTION_SPACE&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;dev&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_DEPLOYMENT_DOMAIN&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cfapps.io&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_CONNECTION_USERNAME&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;you@some-domain.com&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_CONNECTION_PASSWORD&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;yourpassword&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_CONNECTION_SKIP_SSL_VALIDATION&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_DEPLOYMENT_DELETE_ROUTES&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_DEPLOYMENT_SERVICES&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my_rabbit&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_DEPLOYMENT_STREAM_ENABLE_RANDOM_APP_NAME_PREFIX&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[dev]_DEPLOYMENT_MEMORY&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2048m&lt;/span&gt;

    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_CONNECTION_URL&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;https://api.run.pivotal.io&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_CONNECTION_ORG&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;your-org&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_CONNECTION_SPACE&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;qa&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_DEPLOYMENT_DOMAIN&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cfapps.io&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_CONNECTION_USERNAME&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;you@some-domain.com&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_CONNECTION_PASSWORD&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;yourpassword&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_CONNECTION_SKIP_SSL_VALIDATION&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_DEPLOYMENT_DELETE_ROUTES&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_DEPLOYMENT_SERVICES&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my_rabbit&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_DEPLOYMENT_STREAM_ENABLE_RANDOM_APP_NAME_PREFIX&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;SPRING_CLOUD_SKIPPER_SERVER_PLATFORM_CLOUDFOUNDRY_ACCOUNTS[qa]_DEPLOYMENT_MEMORY&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2048m&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my_postgresql&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Push Skipper server to cloudfoundry using PCF CLI&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cf push &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; skipper-server-manifest.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Pushing from manifest to org your-org / space util as you@some-domain.com...
Using manifest file M:\scdf-basics\skipper-server-manifest.yml
...
...
Waiting for app to start...

name:              skipper-server
requested state:   started
routes:            my-codeaches-skipper-server.cfapps.io
...
...
     state     since                  cpu      memory         disk           details
#0   running   2020-01-03T04:39:02Z   166.0%   280.8M of 1G   228.7M of 1G
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;provision-services-needed-for-scdf-server&quot;&gt;&lt;strong&gt;Provision services needed for SCDF server&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Provision Redis&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since analytics feature is enabled by default, the Data Flow server is expected to have a valid Redis store available as analytic repository.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cf create-service rediscloud 30mb my_redis
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Creating service instance my_redis in org your-org / space util as you@some-domain.com...
OK
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Provision MySQL&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A relational database is used to store stream and task definitions as well as the state of executed tasks. For our example, we shall use MySQL server.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;M:&lt;span class=&quot;se&quot;&gt;\s&lt;/span&gt;cdf-basics&amp;gt;cf create-service cleardb spark my_mysql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Creating service instance my_mysql in org your-org / space util as you@some-domain.com...
OK
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;deploy-scdf-server&quot;&gt;&lt;strong&gt;Deploy SCDF server&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Download Data Flow server&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://repo.spring.io/release/org/springframework/cloud/spring-cloud-dataflow-server/2.3.0.RELEASE/spring-cloud-dataflow-server-2.3.0.RELEASE.jar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Configure Data Flow properties&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataflow-server-manifest.yml&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-yml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;applications&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;dataflow-server&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;random-route&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2G&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;disk_quota&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2G&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;spring-cloud-dataflow-server-2.3.0.RELEASE.jar&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my-codeaches-scdf-server.cfapps.io&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;SPRING_APPLICATION_NAME&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;dataflow-server&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;JBP_CONFIG_OPEN_JDK_JRE&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;jre:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;version:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;13.+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;}'&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;SPRING_PROFILES_ACTIVE&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cloud&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;JBP_CONFIG_SPRING_AUTO_RECONFIGURATION&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{enabled:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;false}'&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;SPRING_CLOUD_SKIPPER_CLIENT_SERVER_URI&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;https://my-codeaches-skipper-server.cfapps.io/api&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;SPRING_APPLICATION_JSON&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{&quot;maven&quot;:{&quot;remote-repositories&quot;:{&quot;repo1&quot;:{&quot;url&quot;:&quot;https://repo.spring.io/libs-release&quot;},&quot;repo2&quot;:{&quot;url&quot;:&quot;https://oss.sonatype.org/content/repositories/snapshots&quot;},&quot;repo3&quot;:{&quot;url&quot;:&quot;https://oss.sonatype.org/content/repositories/releases&quot;}}}}'&lt;/span&gt;

    &lt;span class=&quot;s&quot;&gt;security.basic.enabled&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;security.user.name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;user001&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;security.user.password&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;pass001&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;security.user.role&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;VIEW,CREATE,MANAGE&lt;/span&gt;

  &lt;span class=&quot;na&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my_mysql&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my_redis&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Push Data Flow to cloudfoundry using PCF CLI&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cf push &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; dataflow-server-manifest.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Pushing from manifest to org your-org / space util as you@some-domain.com...
Using manifest file M:\scdf-basics\dataflow-server-manifest.yml
...
...
Waiting for app to start...

name:              dataflow-server
requested state:   started
routes:            my-codeaches-scdf-server.cfapps.io
...
...
     state     since                  cpu      memory         disk           details
#0   running   2020-01-03T04:41:41Z   148.3%   343.8M of 2G   230.3M of 2G
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;setup-scdf-shell-cli-on-your-pc&quot;&gt;&lt;strong&gt;Setup SCDF Shell CLI on your PC&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Download SCDF Command Line Interface (CLI)&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://repo.spring.io/release/org/springframework/cloud/spring-cloud-dataflow-shell/2.3.0.RELEASE/spring-cloud-dataflow-shell-2.3.0.RELEASE.jar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Start the SCDF Shell CLI&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;java &lt;span class=&quot;nt&quot;&gt;-jar&lt;/span&gt; spring-cloud-dataflow-shell-2.3.0.RELEASE.jar &lt;span class=&quot;nt&quot;&gt;--dataflow&lt;/span&gt;.uri&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;https://my-codeaches-scdf-server.cfapps.io &lt;span class=&quot;nt&quot;&gt;--dataflow&lt;/span&gt;.username&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;user001 &lt;span class=&quot;nt&quot;&gt;--dataflow&lt;/span&gt;.password&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;pass001 &lt;span class=&quot;nt&quot;&gt;--skip-ssl-validation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;  ____                              ____ _                __
 / ___| _ __  _ __(_)_ __   __ _   / ___| | ___  _   _  __| |
 \___ \| '_ \| '__| | '_ \ / _` | | |   | |/ _ \| | | |/ _` |
  ___) | |_) | |  | | | | | (_| | | |___| | (_) | |_| | (_| |
 |____/| .__/|_|  |_|_| |_|\__, |  \____|_|\___/ \__,_|\__,_|
  ____ |_|    _          __|___/                 __________
 |  _ \  __ _| |_ __ _  |  ___| | _____      __  \ \ \ \ \ \
 | | | |/ _` | __/ _` | | |_  | |/ _ \ \ /\ / /   \ \ \ \ \ \
 | |_| | (_| | || (_| | |  _| | | (_) \ V  V /    / / / / / /
 |____/ \__,_|\__\__,_| |_|   |_|\___/ \_/\_/    /_/_/_/_/_/

2.3.0.RELEASE

Welcome to the Spring Cloud Data Flow shell. For assistance hit TAB or type &quot;help&quot;.
Successfully targeted https://my-codeaches-scdf-server.cfapps.io
dataflow:&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;setup-skipper-shell-cli-on-your-pc&quot;&gt;&lt;strong&gt;Setup Skipper Shell CLI on your PC&lt;/strong&gt;&lt;/h3&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://repo.spring.io/release/org/springframework/cloud/spring-cloud-skipper-shell/2.2.1.RELEASE/spring-cloud-skipper-shell-2.2.1.RELEASE.jar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Start the Skipper Shell CLI&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;java &lt;span class=&quot;nt&quot;&gt;-jar&lt;/span&gt; spring-cloud-skipper-shell-2.2.1.RELEASE.jar &lt;span class=&quot;nt&quot;&gt;--spring&lt;/span&gt;.cloud.skipper.client.serverUri&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;https://my-codeaches-skipper-server.cfapps.io/api
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;skipper:&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;summary&quot;&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Congratulations! You just deployed  both Skipper Server and SCDF Server to Pivotal cloudfoundry (PCF).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Spring Cloud Stream" /><category term="Spring Cloud Dataflow" /><category term="Spring Cloud Skipper" /><category term="Pivotal Cloud Foundry" /><summary type="html">In this tutorial, let’s configure and deploy spring cloud data flow server to pivotal cloudfoundry (PCF). For deploying streams, the Data Flow Server delegates the deployment work to Skipper Server. Hence, we shall configure and deploy Skipper server as well.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Create and deploy Spring Cloud Config Client on cloudfoundry (PCF)</title><link href="https://codeaches.com/spring-cloud-config/spring-cloud-config-client-on-cloudfoundry" rel="alternate" type="text/html" title="Create and deploy Spring Cloud Config Client on cloudfoundry (PCF)" /><published>2020-01-02T00:00:00-07:00</published><updated>2020-01-02T00:00:00-07:00</updated><id>https://codeaches.com/spring-cloud-config/spring-cloud-config-client</id><content type="html" xml:base="https://codeaches.com/spring-cloud-config/spring-cloud-config-client-on-cloudfoundry">&lt;p&gt;Config Server for Pivotal Cloud Foundry (PCF) is an externalized configuration service, which gives us with a central place to manage an application’s external properties across all environments.&lt;/p&gt;

&lt;p&gt;Spring cloud config client applications can use Config Server to manage configurations across environments.&lt;/p&gt;

&lt;p&gt;In this tutorial, let’s deploy a sample spring cloud config client application (say..Pet Store App) which binds to Config Server.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;&lt;strong&gt;Table of contents&lt;/strong&gt;&lt;/h3&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#prerequisites&quot; id=&quot;markdown-toc-prerequisites&quot;&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#create-spring-boot-starter-application&quot; id=&quot;markdown-toc-create-spring-boot-starter-application&quot;&gt;&lt;strong&gt;Create spring boot starter application&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deploy-the-application-to-pcf&quot; id=&quot;markdown-toc-deploy-the-application-to-pcf&quot;&gt;&lt;strong&gt;Deploy the application to PCF&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#test&quot; id=&quot;markdown-toc-test&quot;&gt;&lt;strong&gt;Test&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#summary&quot; id=&quot;markdown-toc-summary&quot;&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;prerequisites&quot;&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;An account on Pivotal Cloud Foundry (PCF). You can create one &lt;strong&gt;&lt;a href=&quot;https://console.run.pivotal.io/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;PCF Command Line Interface (CLI) installed on your computer. PCF CLI can be found in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tools&lt;/code&gt; section of your &lt;strong&gt;&lt;a href=&quot;https://console.run.pivotal.io/tools&quot; target=&quot;_blank&quot;&gt;PCF account&lt;/a&gt;.&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;For this tutorial I will be using cloudfoundry config service named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my-config-server&lt;/code&gt;. You can refer the guide &lt;strong&gt;&lt;a href=&quot;/spring-cloud-config/register-spring-cloud-config-server-on-cloudfoundry&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt; to cregister the config service.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my-config-server&lt;/code&gt; is registered with the properties file which is in &lt;strong&gt;&lt;a href=&quot;https://github.com/codeaches/config-files-example&quot; target=&quot;_blank&quot;&gt;GIT&lt;/a&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;create-spring-boot-starter-application&quot;&gt;&lt;strong&gt;Create spring boot starter application&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Building the bare bone Spring Boot Service is simple when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spring Initializr&lt;/code&gt; is used. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spring Initializr&lt;/code&gt; generates spring boot project with just what you need to start quickly! Let’s start off with one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a Spring Boot starter project using Spring Initializr&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s utilize the pre-configured &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spring Initializr&lt;/code&gt; which is available &lt;strong&gt;&lt;a href=&quot;https://start.spring.io/#!type=maven-project&amp;amp;language=java&amp;amp;platformVersion=2.2.4.RELEASE&amp;amp;packaging=jar&amp;amp;jvmVersion=13&amp;amp;groupId=com.codeaches&amp;amp;artifactId=pcf-config-client-pet-service&amp;amp;name=pcf-config-client-pet-service&amp;amp;description=demo%20project%20for%20spring%20boot&amp;amp;packageName=com.codeaches.pet&amp;amp;dependencies=web,scs-config-client&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt; to create &lt;strong&gt;pcf-config-client-pet-service&lt;/strong&gt; starter project.&lt;/p&gt;

&lt;p&gt;Click on Generate Project. This downloads a zip file containing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pcf-config-client-pet-service&lt;/code&gt; project. Import the project to your IDE.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add a Rest Controller to read the value from properties file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s add a rest controller class which exposes a HTTP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GET&lt;/code&gt; method pet(). Here, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pet()&lt;/code&gt; method returns the value of the variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pet&lt;/code&gt; which is configured to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dog&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dev environement&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.codeaches.pet.MyController.java&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@RestController&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@RefreshScope&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyController&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@Value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${pet}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@GetMapping&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/pet&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;My pet is %s&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Add a rest controller to read data from json file&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s Update the rest controller class with another GET service &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;petDetails()&lt;/code&gt;. Here, the &lt;strong&gt;petDetails()&lt;/strong&gt; returns the contents of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;petDetails.json&lt;/code&gt; file of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dev environment&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.codeaches.configclientpcfapp.JSONFileDemoController.java&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;nd&quot;&gt;@Autowired&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PlainTextConfigClient&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;configClient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@GetMapping&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/petDetails&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;petDetails&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IOException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;InputStream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;configClient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getConfigFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;petDetails.json&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInputStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;copyToString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Charset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;defaultCharset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;deploy-the-application-to-pcf&quot;&gt;&lt;strong&gt;Deploy the application to PCF&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Create manifest.yml file for deployment&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s create &lt;strong&gt;manifest.yml&lt;/strong&gt; file and specify the configuration details like JRE version, path of the application jar file, properties files being referenced as shown below. This file will be used for deployment to PCF.&lt;/p&gt;

&lt;div class=&quot;language-yml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;applications&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;pet-service&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;random-route&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;target/pcf-config-client-pet-service-0.0.1-SNAPSHOT.jar&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1G&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;spring.application.name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;petDetails&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;JBP_CONFIG_OPEN_JDK_JRE&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;jre:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;version:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;13.+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;}'&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my-config-server&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;path&lt;/strong&gt;:&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;spring.application.name&lt;/strong&gt;:&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;services&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Deploy Pet Store App to cloudfoundry&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Deploy &lt;strong&gt;pcf-config-client-pet-service-0.0.1-SNAPSHOT.jar&lt;/strong&gt; to PCF using the &lt;strong&gt;cf push&lt;/strong&gt; command.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cf push &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; manifest.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-log&quot;&gt;Pushing from manifest to your-org / space dev as you@some-domain.com...
Using manifest file M:\pcf-config-client-pet-service\manifest.yml
Getting app info...
Creating app with these attributes...
+ name:        pcf-config-client-pet-service
  path:        M:\pcf-config-client-pet-service\target\pcf-config-client-pet-service-0.0.1-SNAPSHOT.jar
+ instances:   1
+ memory:      1G
  services:
+   my-config-server
  env:
+   JBP_CONFIG_OPEN_JDK_JRE
+   spring.application.name
  routes:
+   pcf-config-client-pet-service-appreciative-gerenuk-qe.cfapps.io

Creating app pcf-config-client-pet-service...
Mapping routes...
Binding services...
...
Waiting for app to start...
...

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check the route of the Pet Store App&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once the deployment is completed successfully, you can check the assigned route of the app using &lt;strong&gt;cf app&lt;/strong&gt; command.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cf app pcf-config-client-pet-service

....
routes: pcf-config-client-pet-service-appreciative-gerenuk-qe.cfapps.io
....
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;test&quot;&gt;&lt;strong&gt;Test&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test pet() method&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; GET https://pcf-config-client-pet-service-appreciative-gerenuk-qe.cfapps.io/pet
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;My pet is cat
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test petDetails() method&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; GET https://pcf-config-client-pet-service-appreciative-gerenuk-qe.cfapps.io/petDetails
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Dog people know dog language
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;summary&quot;&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Congratulations! You just created a new spring boot rest application which utilizes Config Server for it’s configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Spring Cloud Config" /><category term="Pivotal Cloud Foundry" /><summary type="html">Config Server for Pivotal Cloud Foundry (PCF) is an externalized configuration service, which gives us with a central place to manage an application’s external properties across all environments. Spring cloud config client applications can use Config Server to manage configurations across environments. In this tutorial, let’s deploy a sample spring cloud config client application (say..Pet Store App) which binds to Config Server.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Collectors in Java</title><link href="https://codeaches.com/java/collectors" rel="alternate" type="text/html" title="Collectors in Java" /><published>2020-01-01T00:00:00-07:00</published><updated>2020-01-01T00:00:00-07:00</updated><id>https://codeaches.com/java/java-collectors</id><content type="html" xml:base="https://codeaches.com/java/collectors">&lt;p&gt;In this article let’s go through different ways of accumulating the elements of a &lt;strong&gt;Stream&lt;/strong&gt; using &lt;strong&gt;Collectors&lt;/strong&gt;. This can be achived by using the &lt;strong&gt;collect&lt;/strong&gt; method of  Stream interface. &lt;strong&gt;collect&lt;/strong&gt; takes a Collector, which is an interface for reduction operations.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Let’s use &lt;a href=&quot;https://github.com/codeaches/java-8-examples/blob/master/src/main/java/com/codeaches/java8/examples/Car.java&quot;&gt;cars&lt;/a&gt; as our data set for examples below&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;cars-inventory&quot;&gt;&lt;strong&gt;Cars Inventory:&lt;/strong&gt;&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Manufacturer&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Model&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Year&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Price&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Toyota&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Corolla&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2013&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;21000.00 $&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Toyota&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Camry&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2018&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;24000.00 $&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mercedes&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Benz&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2019&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;40000.00 $&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;Table of contents&lt;/h2&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#filtering-a-stream&quot; id=&quot;markdown-toc-filtering-a-stream&quot;&gt;&lt;strong&gt;Filtering a Stream&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#converting-a-stream-to-a-map&quot; id=&quot;markdown-toc-converting-a-stream-to-a-map&quot;&gt;&lt;strong&gt;Converting a Stream to a Map&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#calculating-averages&quot; id=&quot;markdown-toc-calculating-averages&quot;&gt;&lt;strong&gt;Calculating averages&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#calculating-sum&quot; id=&quot;markdown-toc-calculating-sum&quot;&gt;&lt;strong&gt;Calculating sum&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#calculating-sum-across-each-group&quot; id=&quot;markdown-toc-calculating-sum-across-each-group&quot;&gt;&lt;strong&gt;Calculating sum across each group&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#get-costliest-and-cheapest-car&quot; id=&quot;markdown-toc-get-costliest-and-cheapest-car&quot;&gt;&lt;strong&gt;Get costliest and cheapest car&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#joining-the-values&quot; id=&quot;markdown-toc-joining-the-values&quot;&gt;&lt;strong&gt;Joining the values&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#stream-partitioning-based-on-a-condition&quot; id=&quot;markdown-toc-stream-partitioning-based-on-a-condition&quot;&gt;&lt;strong&gt;Stream partitioning based on a condition&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;filtering-a-stream&quot;&gt;&lt;strong&gt;Filtering a Stream&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s get all the &lt;strong&gt;Toyota cCars&lt;/strong&gt; in our inventory using the filter method and collect the filtered cars using &lt;strong&gt;Collectors.toList&lt;/strong&gt;. This is pretty basic.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;toyotaCars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getManufacturer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Toyota&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[Car [manufacturer=Toyota, model=Corolla, year=2013, price=21000.0], 
Car [manufacturer=Toyota, model=Camry, year=2018, price=24000.0]]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That was simple! We used &lt;strong&gt;Collectors.toList&lt;/strong&gt; which returns a collector that is sent to the collect method.&lt;/p&gt;

&lt;h3 id=&quot;converting-a-stream-to-a-map&quot;&gt;&lt;strong&gt;Converting a Stream to a Map&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Now, let’s create a map with &lt;em&gt;model&lt;/em&gt; as the key and &lt;em&gt;Car&lt;/em&gt; as the value.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;carMap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Car:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getModel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;identity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
    Benz=Car [manufacturer=Mercedes, model=Benz, year=2019, price=40000.0], 
    Camry=Car [manufacturer=Toyota, model=Camry, year=2018, price=24000.0], 
    Corolla=Car [manufacturer=Toyota, model=Corolla, year=2013, price=21000.0]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we used &lt;strong&gt;Collectors.toMap&lt;/strong&gt; which takes method for creating the keys and a function for creating the values. In our example we used &lt;em&gt;Car::getModel&lt;/em&gt; to say that we want the &lt;em&gt;model&lt;/em&gt; as the keys.&lt;/p&gt;

&lt;p&gt;For the value mapping, we used &lt;strong&gt;Function.identity&lt;/strong&gt;. This simply returns a function that always returns its input parameter — in our case the Car — as output.&lt;/p&gt;

&lt;h3 id=&quot;calculating-averages&quot;&gt;&lt;strong&gt;Calculating averages&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s look at calculating the averages.&lt;/p&gt;

&lt;p&gt;Let’s get an average price of a car in our inventory. We can achive this using &lt;strong&gt;Collectors.averagingInt&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;averagePrice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;averagingDouble&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Car:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getPrice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;28333.333333333332
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;This arithmetic collector exists for int and long as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;calculating-sum&quot;&gt;&lt;strong&gt;Calculating sum&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;If we want to get total price of all of my cars in inventroy, we can use &lt;strong&gt;Collectors.summingDouble&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;totalPrice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;summingDouble&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Car:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getPrice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;85000.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;This arithmetic collector exists for int and long as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;calculating-sum-across-each-group&quot;&gt;&lt;strong&gt;Calculating sum across each group&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;If we want to get total price for each of the manufacturer of our cars in inventroy, we can use &lt;strong&gt;Collectors.groupingBy&lt;/strong&gt; and sum the balances using &lt;strong&gt;Collectors.summingDouble&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;totalPriceByManufacturer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;groupingBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Car:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getManufacturer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;summingDouble&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Car:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getPrice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{Toyota=45000.0, Mercedes=40000.0}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;get-costliest-and-cheapest-car&quot;&gt;&lt;strong&gt;Get costliest and cheapest car&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s say I want to get the costliest car. This can be achieved by using &lt;strong&gt;Collectors.maxBy&lt;/strong&gt; which takes Comparator as it’s input argument.&lt;/p&gt;

&lt;p&gt;Let’s define a &lt;strong&gt;Comparator&lt;/strong&gt; which determines the car with highest price. We shall use this comparator in &lt;strong&gt;Collectors.maxBy&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Comparator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costliestCarComparator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;car1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;car2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compare&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;car1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getPrice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;car2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getPrice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costliestCar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;maxBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;costliestCarComparator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Car [manufacturer=Mercedes, model=Benz, year=2019, price=40000.0]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Similarly, cheapest car can be found by using &lt;strong&gt;Collectors.minBy&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cheapestCar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;minBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;costliestCarComparator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Car [manufacturer=Toyota, model=Corolla, year=2013, price=21000.0]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;joining-the-values&quot;&gt;&lt;strong&gt;Joining the values&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s say I want a comma seperated &lt;em&gt;models&lt;/em&gt; of all the cars. This can be achieved by &lt;strong&gt;Collectors.joining&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;models&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accounts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Account:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getAccountNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;joining&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;, &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Corolla, Camry, Benz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;stream-partitioning-based-on-a-condition&quot;&gt;&lt;strong&gt;Stream partitioning based on a condition&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s say I want list of cars based on &lt;em&gt;Manufacturer&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This can be achieved by combination of &lt;strong&gt;Predicate&lt;/strong&gt; and &lt;strong&gt;Collectors.partitioningBy&lt;/strong&gt; methods.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Predicate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;manufacturerPredicate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getManufacturer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Toyota&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapyByManufacturer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;partitioningBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manufacturerPredicate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
    false=[
           Car [manufacturer=Mercedes, model=Benz, year=2019, price=40000.0]
          ], 
    true=[
           Car [manufacturer=Toyota, model=Corolla, year=2013, price=21000.0], 
           Car [manufacturer=Toyota, model=Camry, year=2018, price=24000.0]
         ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will result in a map with two entries.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;One with key &lt;strong&gt;true&lt;/strong&gt; which contains the cars that satisfied the &lt;em&gt;toyota&lt;/em&gt; car predicate.&lt;/li&gt;
  &lt;li&gt;One with key &lt;strong&gt;false&lt;/strong&gt; which contains the cars that did not satisfy the &lt;em&gt;toyota&lt;/em&gt; car predicate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This concludes the features of &lt;strong&gt;Collectors&lt;/strong&gt; class. I hope you like this aticle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Java" /><summary type="html">In this article let’s go through different ways of accumulating the elements of a Stream using Collectors. This can be achived by using the collect method of Stream interface. collect takes a Collector, which is an interface for reduction operations.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Completable Future in Java</title><link href="https://codeaches.com/java/completable-future" rel="alternate" type="text/html" title="Completable Future in Java" /><published>2020-01-01T00:00:00-07:00</published><updated>2020-01-01T00:00:00-07:00</updated><id>https://codeaches.com/java/java-completable-future</id><content type="html" xml:base="https://codeaches.com/java/completable-future">&lt;p&gt;Java 8 introduced a lot of awesome features, one of them being &lt;strong&gt;CompletableFuture class&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Beside implementing the Future interface, &lt;em&gt;CompletableFuture&lt;/em&gt; also implements the CompletionStage interface. &lt;strong&gt;CompletionStage&lt;/strong&gt; promises that the computation eventually will be done.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;p&gt;The great thing about the CompletionStage is that it offers a vast selection of methods that let us attach callbacks which will be executed on completion.&lt;/p&gt;

&lt;p&gt;This way we can build programs in a non-blocking fashion. Let’s take few examples to see some use cases of &lt;em&gt;CompletableFuture&lt;/em&gt;.&lt;/p&gt;

&lt;h2 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;Table of contents&lt;/h2&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#the-simplest-asynchronous-computation&quot; id=&quot;markdown-toc-the-simplest-asynchronous-computation&quot;&gt;&lt;strong&gt;The simplest asynchronous computation&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#attach-a-callback&quot; id=&quot;markdown-toc-attach-a-callback&quot;&gt;&lt;strong&gt;Attach a callback&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#chaining-multiple-callbacks&quot; id=&quot;markdown-toc-chaining-multiple-callbacks&quot;&gt;&lt;strong&gt;Chaining multiple callbacks&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#parallel-callbacks&quot; id=&quot;markdown-toc-parallel-callbacks&quot;&gt;&lt;strong&gt;Parallel callbacks&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#handling-exceptions-using-exceptionally&quot; id=&quot;markdown-toc-handling-exceptions-using-exceptionally&quot;&gt;&lt;strong&gt;Handling Exceptions using &lt;em&gt;exceptionally&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#handling-exceptions-using-whencomplete&quot; id=&quot;markdown-toc-handling-exceptions-using-whencomplete&quot;&gt;&lt;strong&gt;Handling Exceptions using &lt;em&gt;whenComplete&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#callback-on-multiple-computations-using-thencombine&quot; id=&quot;markdown-toc-callback-on-multiple-computations-using-thencombine&quot;&gt;&lt;strong&gt;Callback on multiple computations using &lt;em&gt;thenCombine&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#callback-on-multiple-computations-using-runafterboth&quot; id=&quot;markdown-toc-callback-on-multiple-computations-using-runafterboth&quot;&gt;&lt;strong&gt;Callback on multiple computations using &lt;em&gt;runAfterBoth&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#callback-on-either-of-computations-using-runafterboth&quot; id=&quot;markdown-toc-callback-on-either-of-computations-using-runafterboth&quot;&gt;&lt;strong&gt;Callback on either of computations using &lt;em&gt;runAfterBoth&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;the-simplest-asynchronous-computation&quot;&gt;&lt;strong&gt;The simplest asynchronous computation&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s start with the basics — creating a simple asynchronous computation.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Some heavy computation which eventually returns an Integer&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/* Print the result returned by heavyMethod */&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;  
&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, &lt;strong&gt;supplyAsync&lt;/strong&gt; takes a Supplier containing the heavy code we want to execute asynchronously. Once, the execution of &lt;em&gt;heavyMethod&lt;/em&gt; is completed, the result will be printed.&lt;/p&gt;

&lt;p&gt;Well, we can take it further by attaching a call back method which will print the result once &lt;em&gt;heavyMethod&lt;/em&gt; returns the result.&lt;/p&gt;

&lt;h3 id=&quot;attach-a-callback&quot;&gt;&lt;strong&gt;Attach a callback&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;callback&lt;/em&gt; executes after the asynchronous computation is done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;thenAccept&lt;/strong&gt; is one one option to add a callback. It takes a Consumer — &lt;em&gt;printer&lt;/em&gt; — which prints the result of the &lt;em&gt;heavyMethod&lt;/em&gt; when it’s done.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an Integer&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Print the request&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                                                      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenAccept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What if we want to pass values from one call back to another and so on…Like a chain? &lt;strong&gt;thenAccept&lt;/strong&gt; won’t help here as it takes an input and returns nothing. In these cases, we can use another call back feature - &lt;strong&gt;thenApply&lt;/strong&gt;. &lt;strong&gt;thenApply&lt;/strong&gt; takes an input and returns an output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;thenApply&lt;/strong&gt; takes a Function which accepts an input and returns a result.&lt;/p&gt;

&lt;p&gt;Let’s put all these into a code to see how we can use these functionalities.&lt;/p&gt;

&lt;h3 id=&quot;chaining-multiple-callbacks&quot;&gt;&lt;strong&gt;Chaining multiple callbacks&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s extend our earlier example by addig a new method which multiplies the input and returns the result - &lt;strong&gt;multiply&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an Integer&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// multiply the input and return the result&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Print the request&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                                                      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenApply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                                                      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenAccept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;100
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;Here, the response of &lt;em&gt;heavyMethod&lt;/em&gt; will be consumed by &lt;em&gt;multiply&lt;/em&gt; and the response of &lt;em&gt;multiply&lt;/em&gt; will be consumed by &lt;em&gt;printer&lt;/em&gt;, which eventually prints the value 100.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;parallel-callbacks&quot;&gt;&lt;strong&gt;Parallel callbacks&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s say, once our &lt;em&gt;heavyMethod&lt;/em&gt; is completed, we want to add as well as multiply on the same result paralelly. This can be achieved using &lt;strong&gt;thenApplyAsync&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an Integer&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// add&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// multiply&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Print the request&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenApplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenAccept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenApplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenAccept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;100
20
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;add&lt;/em&gt; and &lt;em&gt;multiply&lt;/em&gt; will submitted as separate tasks to the ForkJoinPool.commonPool(). This results in both the &lt;em&gt;add&lt;/em&gt; and &lt;em&gt;multiply&lt;/em&gt; callbacks being executed when the preceding &lt;em&gt;heavyMethod&lt;/em&gt; is completed.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Asynchronous version is a good option when we have multiple callbacks dependent on the same computation result.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;handling-exceptions-using-exceptionally&quot;&gt;&lt;strong&gt;Handling Exceptions using &lt;em&gt;exceptionally&lt;/em&gt;&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s consider a scenario where &lt;em&gt;heavyMethod&lt;/em&gt; might throw an exception. We can use &lt;em&gt;exceptionally&lt;/em&gt; function to catch the exception and handle it gracefully. &lt;em&gt;exceptionally&lt;/em&gt; is termed as a recovery method.&lt;/p&gt;

&lt;p&gt;In the below example, we are returning a string value &lt;strong&gt;NOTHING TO PRINT&lt;/strong&gt; in &lt;em&gt;exceptionally&lt;/em&gt; method. &lt;strong&gt;NOTHING TO PRINT&lt;/strong&gt; will be our recovery output when ever our &lt;em&gt;heavyMethod fails&lt;/em&gt;.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually throws NULL POINTER EXCEPTION&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Print the message&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PRINT MSG: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;exceptionally&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;heavyMethod threw an exception: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLocalizedMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;NOTHING TO PRINT&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenAccept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;heavyMethod threw an exception: java.lang.NullPointerException
PRINT MSG: NOTHING TO PRINT
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;handling-exceptions-using-whencomplete&quot;&gt;&lt;strong&gt;Handling Exceptions using &lt;em&gt;whenComplete&lt;/em&gt;&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;whenComplete&lt;/em&gt;&lt;/strong&gt; gives us more flexiblity to handle both exceptions and the results. In the below example, we are using &lt;em&gt;whenComplete&lt;/em&gt; to print the exception to error console before &lt;em&gt;exceptionally&lt;/em&gt; recoveres be sending a default message to &lt;em&gt;printer&lt;/em&gt;.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually throws NULL POINTER EXCEPTION&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Print the message&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PRINT MSG: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;whenComplete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Throwable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;heavyMethod threw an exception: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLocalizedMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;exceptionally&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;NOTHING TO PRINT&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenAccept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;heavyMethod threw an exception: java.lang.NullPointerException
PRINT MSG: NOTHING TO PRINT
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;callback-on-multiple-computations-using-thencombine&quot;&gt;&lt;strong&gt;Callback on multiple computations using &lt;em&gt;thenCombine&lt;/em&gt;&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Sometimes it would be really helpful to be able to create a callback that is dependent on the result of two computations. This is where thenCombine becomes handy.&lt;/p&gt;

&lt;p&gt;thenCombine allows us to register a BiFunction callback depending on the result of two CompletionStages.&lt;/p&gt;

&lt;p&gt;To see how this is done, let’s in addition to finding a receiver also execute the heavy job of creating some content before sending a message.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an Integer&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an Integer&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Print the request&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;BiFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;combinedFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenCombine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asyncFunction2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenAccept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;combinedFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;25
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the above example, we started two asynchronous methods — heavyMethod1 and heavyMethod2. &lt;em&gt;thenCombine&lt;/em&gt; is used to trigger &lt;em&gt;sum&lt;/em&gt; which takes the result of both asynchronous methods and returns the result. &lt;em&gt;printer&lt;/em&gt; prints this result.&lt;/p&gt;

&lt;h3 id=&quot;callback-on-multiple-computations-using-runafterboth&quot;&gt;&lt;strong&gt;Callback on multiple computations using &lt;em&gt;runAfterBoth&lt;/em&gt;&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;runAfterBoth&lt;/em&gt;&lt;/strong&gt; is another variant of thenCombine. &lt;em&gt;runAfterBoth&lt;/em&gt; takes a Runnable not caring about the actual values of the preceding computation — only that they both are actually complete.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an Integer&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an Integer&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heavyMethod2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavyMethod2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;Runnable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Heavy Load methods completed successfully&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;combinedFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncFunction1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runAfterBoth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asyncFunction2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;combinedFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Heavy Load methods completed successfully
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;callback-on-either-of-computations-using-runafterboth&quot;&gt;&lt;strong&gt;Callback on either of computations using &lt;em&gt;runAfterBoth&lt;/em&gt;&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s say we have two sources of finding car details either through &lt;em&gt;carfax&lt;/em&gt; or &lt;em&gt;autocheck&lt;/em&gt;. We can ask both and take the result from who ever returns first.&lt;/p&gt;

&lt;p&gt;This can be achieved by &lt;strong&gt;acceptEither&lt;/strong&gt; as seen below. the consumer &lt;em&gt;carDetails&lt;/em&gt; will be executed when either &lt;em&gt;carfax&lt;/em&gt; or &lt;em&gt;autocheck&lt;/em&gt; returns the result.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an car details&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;carfax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;2013 Toyota Corolla&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Heavy computation which eventually returns an car details&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Supplier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;autocheck&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;2013 Toyota Corolla&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;carfaxResult&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carfax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;autocheckResult&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;supplyAsync&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;autocheck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;carDetails&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Car details: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;either&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;carfaxResult&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;acceptEither&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;autocheckResult&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;carDetails&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;either&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Car details: 2013 Toyota Corolla
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This concludes the features of &lt;strong&gt;CompletableFuture&lt;/strong&gt; class. I hope you like this aticle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Java" /><summary type="html">Java 8 introduced a lot of awesome features, one of them being CompletableFuture class. Beside implementing the Future interface, CompletableFuture also implements the CompletionStage interface. CompletionStage promises that the computation eventually will be done.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Function Composition in Java</title><link href="https://codeaches.com/java/functional-composition" rel="alternate" type="text/html" title="Function Composition in Java" /><published>2020-01-01T00:00:00-07:00</published><updated>2020-01-01T00:00:00-07:00</updated><id>https://codeaches.com/java/java-function-composition</id><content type="html" xml:base="https://codeaches.com/java/functional-composition">&lt;p&gt;In this post let’s look at function composition using two compose functions provided in the Function interface - &lt;strong&gt;compose&lt;/strong&gt; and &lt;strong&gt;andThen&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Function composition results in a reuseable function which itself is a combination of other functions.&lt;/p&gt;

&lt;p&gt;Basically, there are two ways to achieve Function composition. They are &lt;strong&gt;compose&lt;/strong&gt; and &lt;strong&gt;andThen&lt;/strong&gt;.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;p&gt;Let’s create two functions. One which sums up and other squares up itself.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, let’s combine them, using compose and andThen.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sumAndSquareResult&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Returns 18&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;squareAndSumResult&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;andThen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Returns 36&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;sum.compose(square): &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sumAndSquareResult&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;sum.andThen(square): &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;squareAndSumResult&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;18
36
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The difference between &lt;strong&gt;compose&lt;/strong&gt; and &lt;strong&gt;andThen&lt;/strong&gt; is the order in which they execute the functions.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;compose&lt;/strong&gt; executes the parameter first (square) followed by the caller (sum)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;andThen&lt;/strong&gt; executes the caller first (sum) followed by the parameter (square)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s take it further and see how we can apply composition on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BiFunction&lt;/code&gt; functional interfaces.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Let’s use &lt;a href=&quot;https://github.com/codeaches/java-8-examples/blob/master/src/main/java/com/codeaches/java8/examples/Car.java&quot;&gt;cars&lt;/a&gt; as our data set for examples below&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;cars-inventory&quot;&gt;&lt;strong&gt;Cars Inventory:&lt;/strong&gt;&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Manufacturer&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Model&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Year&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Price&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Toyota&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Corolla&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2013&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;21000.00 $&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Toyota&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Camry&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2018&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;24000.00 $&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mercedes&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Benz&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2019&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;40000.00 $&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;br /&gt;
Let’s start by introducing a basic function - &lt;strong&gt;byManufacturer&lt;/strong&gt; that filters cars based on Manufacturer.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;BiFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;byManufacturer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manufacturer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getManufacturer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manufacturer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;byManufacturer&lt;/strong&gt; is a BiFunction. They take two arguments.&lt;/p&gt;

&lt;p&gt;Let’s also create few basic functions that sorts a list of cars from costliest to cheapest(affordable I must say!) price and a function that returns the top most car in a list.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Comparator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;carPriceComparator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 
                    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;car1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;car2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compare&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;car2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getPrice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;car1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getPrice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sortByPrice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carPriceComparator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;findFirst&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that we have few functions built, let’s see how we can use them to compose new functions.&lt;/p&gt;

&lt;h3 id=&quot;the-most-expensive-car&quot;&gt;&lt;strong&gt;The Most Expensive Car&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;We can find the most expensive car in our inventroy by sorting the cars by price and then taking the top one from the sorted list.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costliest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sortByPrice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costliestCar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costliest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;costliestCar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isPresent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costliestCar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Car [manufacturer=Mercedes, model=Benz, year=2019, price=40000.0]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;the-most-expensive-car-by-toyota&quot;&gt;&lt;strong&gt;The Most Expensive Car By Toyota&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s see how to get the most expensive car by Toyota in our cars inventroy.&lt;/p&gt;

&lt;p&gt;In our previous example, we wrote a function to get us the most expensive car. We can use this function in composition with a filter functions which gives all the cars based on car type to achieve this..&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;BiFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;highestByCarType&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;byManufacturer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;andThen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;costliest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costliestToyotaCar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;highestByCarType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Toyota&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;costliestToyotaCar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isPresent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costliestToyotaCar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Car [manufacturer=Toyota, model=Camry, year=2018, price=24000.0]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Above are few examples of functional interfaces and functional compositions. The use cases around composition of functions are endless and can be customised for our needs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Java" /><summary type="html">In this post let’s look at function composition using two compose functions provided in the Function interface - compose and andThen. Function composition results in a reuseable function which itself is a combination of other functions. Basically, there are two ways to achieve Function composition. They are compose and andThen.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Setup Apache Kafka on Windows</title><link href="https://codeaches.com/softwares/setup-apache-kafka-on-windows" rel="alternate" type="text/html" title="Setup Apache Kafka on Windows" /><published>2020-01-01T00:00:00-07:00</published><updated>2020-01-01T00:00:00-07:00</updated><id>https://codeaches.com/softwares/install-apache-zookeeper-and-apache-kafka</id><content type="html" xml:base="https://codeaches.com/softwares/setup-apache-kafka-on-windows">&lt;p&gt;In this guide, let’s download and install Apache Zookeeper and Apache Kafka on Windows PC. We shall also create a topic and publish/subscribe a message to this topic.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;&lt;strong&gt;Table of contents&lt;/strong&gt;&lt;/h3&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#download-and-configure-apache-zookeeper&quot; id=&quot;markdown-toc-download-and-configure-apache-zookeeper&quot;&gt;&lt;strong&gt;Download and configure Apache ZooKeeper&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#start-the-zookeeper-server&quot; id=&quot;markdown-toc-start-the-zookeeper-server&quot;&gt;&lt;strong&gt;Start the ZooKeeper Server&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#download-and-configure-apache-kafka&quot; id=&quot;markdown-toc-download-and-configure-apache-kafka&quot;&gt;&lt;strong&gt;Download and configure Apache Kafka&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#start-the-kafka-server&quot; id=&quot;markdown-toc-start-the-kafka-server&quot;&gt;&lt;strong&gt;Start the Kafka Server&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#create-a-topic&quot; id=&quot;markdown-toc-create-a-topic&quot;&gt;&lt;strong&gt;Create a topic&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#validate-topic-creation&quot; id=&quot;markdown-toc-validate-topic-creation&quot;&gt;&lt;strong&gt;Validate Topic Creation&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#test-kafka-producer-and-consumer&quot; id=&quot;markdown-toc-test-kafka-producer-and-consumer&quot;&gt;&lt;strong&gt;Test Kafka Producer and Consumer&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;download-and-configure-apache-zookeeper&quot;&gt;&lt;strong&gt;Download and configure Apache ZooKeeper&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Download Apache ZooKeeper&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The stable version of Apache ZooKeeper can be downloaded from &lt;strong&gt;&lt;a href=&quot;https://www-eu.apache.org/dist/zookeeper/current/&quot; target=&quot;_blank&quot;&gt;Apache Zookeeper Website&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For this tutorial, I am using ZooKeeper 3.5.6 version which was downloaded from &lt;strong&gt;&lt;a href=&quot;https://www-eu.apache.org/dist/zookeeper/current/apache-zookeeper-3.5.6-bin.tar.gz&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once you download the apache-zookeeper-3.5.6-bin.tar.gz, unzip it and you should be able to see the folder &lt;strong&gt;apache-zookeeper-3.5.6-bin&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I have it under &lt;strong&gt;D:\apache-zookeeper-3.5.6-bin&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;D:&lt;span class=&quot;se&quot;&gt;\a&lt;/span&gt;pache-zookeeper-3.5.6-bin
&lt;span class=&quot;nb&quot;&gt;dir&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Directory of D:\apache-zookeeper-3.5.6-bin

06/10/2019  09:02 PM    &amp;lt;DIR&amp;gt;          .
06/10/2019  09:02 PM    &amp;lt;DIR&amp;gt;          ..
04/09/2019  04:13 AM    &amp;lt;DIR&amp;gt;          bin
06/10/2019  09:03 PM    &amp;lt;DIR&amp;gt;          conf
05/03/2019  05:07 AM    &amp;lt;DIR&amp;gt;          docs
06/10/2019  09:02 PM    &amp;lt;DIR&amp;gt;          lib
02/15/2019  05:55 AM            11,358 LICENSE.txt
04/09/2019  04:13 AM               432 NOTICE.txt
05/03/2019  04:41 AM             1,560 README.md
04/02/2019  06:05 AM             1,347 README_packaging.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Configure Apache ZooKeeper&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have downloaded Apache ZooKeeper, let’s configure some basic default values so that we can run the zookeeper server.&lt;/p&gt;

&lt;p&gt;Installation comes with a sample configuration file named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo_sample.cfg&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;conf&lt;/code&gt; directory. Rename this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo_sample.cfg&lt;/code&gt; file to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo.cfg&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;ZooKeeper bootstrap broker runs on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2181&lt;/code&gt; by default. You can change the port, if you want, by modifying the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;clientPort&lt;/code&gt; variable in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo.cfg&lt;/code&gt; file.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Also, ZooKeeper Admin runs on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080&lt;/code&gt;. You can change, if you want, by adding a new configuration &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;admin.serverPort&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo.cfg&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Data file will be written to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/zookeeper&lt;/code&gt;. Change this to a valid folder in your PC by modifying the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataDir&lt;/code&gt; variable in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo.cfg&lt;/code&gt; file. I have updated it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataDir=D:/tmp/zookeeperdata&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;By default, transaction logs will be written to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataDir&lt;/code&gt;. You can change this behavior by adding a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataLogDir&lt;/code&gt; config in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo.cfg&lt;/code&gt;I have set it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataLogDir=D:/tmp/zookeeper-logs&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;start-the-zookeeper-server&quot;&gt;&lt;strong&gt;Start the ZooKeeper Server&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s go to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D:\apache-zookeeper-3.5.6-bin\bin&lt;/code&gt; folder and execute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zkServer.cmd&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;D:&lt;span class=&quot;se&quot;&gt;\a&lt;/span&gt;pache-zookeeper-3.5.6-bin&lt;span class=&quot;se&quot;&gt;\b&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in
&lt;/span&gt;zkServer.cmd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console output&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[main:JettyAdminServer@112] - Started AdminServer on address 0.0.0.0, port 2081 and command URL /commands
[main:ServerCnxnFactory@135] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
[main:NIOServerCnxnFactory@673] - Configuring NIO connection handler with 10s sessionless connection timeout, 2 selector thread(s), 24 worker threads, and 64 kB direct buffers.
[main:NIOServerCnxnFactory@686] - binding to port 0.0.0.0/0.0.0.0:2080
[main:ZKDatabase@117] - zookeeper.snapshotSizeFactor = 0.33
[main:FileSnap@83] - Reading snapshot \tmp\zookeeper\version-2\snapshot.105
[main:FileTxnSnapLog@372] - Snapshotting: 0x105 to \tmp\zookeeper\version-2\snapshot.105
[main:ContainerManager@64] - Using checkIntervalMs=60000 maxPerMinute=10000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Now that the ZooKeeper is up and running, let’s go ahead with Kafka server setup&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;download-and-configure-apache-kafka&quot;&gt;&lt;strong&gt;Download and configure Apache Kafka&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Download Apache Kafka&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The stable version of Apache Kafka can be downloaded from Apache Kafka Website &lt;strong&gt;&lt;a href=&quot;https://kafka.apache.org/downloads&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For this tutorial, I am using Kafka 2.4.0 version which was downloaded using this &lt;strong&gt;&lt;a href=&quot;https://www-eu.apache.org/dist/kafka/2.4.0/kafka_2.12-2.4.0.tgz&quot; target=&quot;_blank&quot;&gt;link&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once you download the kafka_2.12-2.4.0.tgz, unzip it and you should be able to see the folder &lt;strong&gt;kafka_2.12-2.4.0&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I have it under &lt;strong&gt;D:\kafka_2.12-2.4.0&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;D:&lt;span class=&quot;se&quot;&gt;\k&lt;/span&gt;afka_2.12-2.4.0
&lt;span class=&quot;nb&quot;&gt;dir&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Directory of D:\kafka_2.12-2.4.0

06/10/2019  09:09 PM    &amp;lt;DIR&amp;gt;          .
06/10/2019  09:09 PM    &amp;lt;DIR&amp;gt;          ..
05/13/2019  09:18 AM    &amp;lt;DIR&amp;gt;          bin
05/13/2019  09:18 AM    &amp;lt;DIR&amp;gt;          config
05/13/2019  09:18 AM    &amp;lt;DIR&amp;gt;          libs
05/13/2019  09:10 AM            32,216 LICENSE
06/11/2019  05:25 PM    &amp;lt;DIR&amp;gt;          logs
05/13/2019  09:10 AM               336 NOTICE
05/13/2019  09:18 AM    &amp;lt;DIR&amp;gt;          site-docs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Configure Apache Kafka&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have downloaded Apache Kafka, let’s configure some basic default values so that we can run the Kafka server.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Kafka needs a running ZooKeeper instance. The default destination (ZooKeeper server and client port) is pre-configured in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config\server.properties&lt;/code&gt;. Change thie value as per your zookeeper settings.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Kafka broker runs on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9092&lt;/code&gt; by default. You can change the port, if you want, by adding a configuration value for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listeners&lt;/code&gt; variable in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config\server.properties&lt;/code&gt;. As an example, if you like to run kafka broker on port 9999, you need to add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listeners=PLAINTEXT://localhost:9999&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config\server.properties&lt;/code&gt; file so that my Kafka broker runs on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9999&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Logs will be written to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/kafka-logs&lt;/code&gt;. Change this to a valid folder in your PC by modifying the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log.dirs&lt;/code&gt; variable in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config\server.properties&lt;/code&gt; file. I have updated it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log.dirs=D:/tmp/kafka-logs&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;We now have a basic valid configuration. Time to start the Kafka server&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;start-the-kafka-server&quot;&gt;&lt;strong&gt;Start the Kafka Server&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s go to &lt;strong&gt;D:\kafka_2.12-2.4.0\bin\windows&lt;/strong&gt; folder and execute &lt;strong&gt;kafka-server-start.bat&lt;/strong&gt; giving the configuration file path in command line.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;D:&lt;span class=&quot;se&quot;&gt;\k&lt;/span&gt;afka_2.12-2.4.0&lt;span class=&quot;se&quot;&gt;\b&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\w&lt;/span&gt;indows
kafka-server-start.bat &lt;span class=&quot;s2&quot;&gt;&quot;D:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\k&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;afka_2.12-2.4.0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\c&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;onfig&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;erver.properties&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Once the start up completes without any error, you are all set up with a running Kafka server listening on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2082&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;create-a-topic&quot;&gt;&lt;strong&gt;Create a topic&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Kafka installation comes with utilites which can be used to create topics. Let’s utilize it to create a simple topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;My-Test-Topic&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;My-Test-Topic&lt;/code&gt; topic using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kafka-server-start.bat&lt;/code&gt; utility found in the folder &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D:\kafka_2.12-2.4.0\bin\windows&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;D:&lt;span class=&quot;se&quot;&gt;\k&lt;/span&gt;afka_2.12-2.4.0&lt;span class=&quot;se&quot;&gt;\b&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\w&lt;/span&gt;indows
kafka-topics.bat &lt;span class=&quot;nt&quot;&gt;--create&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--bootstrap-server&lt;/span&gt; localhost:9092 &lt;span class=&quot;nt&quot;&gt;--replication-factor&lt;/span&gt; 1 &lt;span class=&quot;nt&quot;&gt;--partitions&lt;/span&gt; 1 &lt;span class=&quot;nt&quot;&gt;--topic&lt;/span&gt; My-Test-Topic
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;validate-topic-creation&quot;&gt;&lt;strong&gt;Validate Topic Creation&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Validate that the topic is created successfully using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kafka-topics.bat&lt;/code&gt; utility.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;D:&lt;span class=&quot;se&quot;&gt;\k&lt;/span&gt;afka_2.12-2.4.0&lt;span class=&quot;se&quot;&gt;\b&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\w&lt;/span&gt;indows
kafka-topics.bat &lt;span class=&quot;nt&quot;&gt;--list&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--bootstrap-server&lt;/span&gt; localhost:9092
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;My-Test-Topic
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;test-kafka-producer-and-consumer&quot;&gt;&lt;strong&gt;Test Kafka Producer and Consumer&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Now that we have a kafka topic, it’s time to test the producer and consumer on this topic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Start the producer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Kafka comes with a command line utility that will take input from a file or from standard input and send it out as messages to the Kafka cluster. By default, each line will be sent as a separate message.&lt;/p&gt;

&lt;p&gt;Start the producer and then type a few messages into the console to send to Kafka topic.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;D:&lt;span class=&quot;se&quot;&gt;\k&lt;/span&gt;afka_2.12-2.4.0&lt;span class=&quot;se&quot;&gt;\b&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\w&lt;/span&gt;indows
kafka-console-producer.bat &lt;span class=&quot;nt&quot;&gt;--broker-list&lt;/span&gt; localhost:9092 &lt;span class=&quot;nt&quot;&gt;--topic&lt;/span&gt; My-Test-Topic
Hello from Codeaches
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Start the consumer to read from Kafka topic&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Kafka also provides a command line utility which that will dump out messages to standard output. This is the Kafka consumer.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;D:&lt;span class=&quot;se&quot;&gt;\k&lt;/span&gt;afka_2.12-2.4.0&lt;span class=&quot;se&quot;&gt;\b&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\w&lt;/span&gt;indows
kafka-console-consumer.bat &lt;span class=&quot;nt&quot;&gt;--bootstrap-server&lt;/span&gt; localhost:9092 &lt;span class=&quot;nt&quot;&gt;--topic&lt;/span&gt; My-Test-Topic &lt;span class=&quot;nt&quot;&gt;--from-beginning&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Hello from Codeaches
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;summary&quot;&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;This concludes our guide to installing ZooKeeper and Kafka on Windows PC.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Apache Kafka" /><summary type="html">In this guide, let’s download and install Apache Zookeeper and Apache Kafka on Windows PC. We shall also create a topic and publish/subscribe a message to this topic.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Load Balancing Tomcat Instances using NGINX on Windows</title><link href="https://codeaches.com/softwares/load-balancing-tomcat-instances-using-nginx" rel="alternate" type="text/html" title="Load Balancing Tomcat Instances using NGINX on Windows" /><published>2020-01-01T00:00:00-07:00</published><updated>2020-01-01T00:00:00-07:00</updated><id>https://codeaches.com/softwares/load-balancing-apache-tomcat-server-instances-using-nginx</id><content type="html" xml:base="https://codeaches.com/softwares/load-balancing-tomcat-instances-using-nginx">&lt;p&gt;In this post, let’s download NGINX software and setup NGINX as a load balancer for a sample application running on two instances of tomcat. In this scenario, NGINX will be setup to act as a reverse proxy.&lt;!-- excerpt end --&gt;&lt;/p&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;table-of-contents&quot;&gt;&lt;strong&gt;Table of contents&lt;/strong&gt;&lt;/h3&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#prerequisites&quot; id=&quot;markdown-toc-prerequisites&quot;&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#download-apache-nginx&quot; id=&quot;markdown-toc-download-apache-nginx&quot;&gt;&lt;strong&gt;Download Apache NGINX&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#configure-nginx-with-tomcat-instances&quot; id=&quot;markdown-toc-configure-nginx-with-tomcat-instances&quot;&gt;&lt;strong&gt;Configure NGINX with tomcat instances&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#test-the-sample-web-app-using-nginx-port-80&quot; id=&quot;markdown-toc-test-the-sample-web-app-using-nginx-port-80&quot;&gt;&lt;strong&gt;Test the sample web app using NGINX port 80&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;prerequisites&quot;&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;As a prerequisite, one more more instances of tomcat with a deployed sample app is required for this guide. If you don’t have one, please go through this link &lt;strong&gt;&lt;a href=&quot;/softwares/setup-multiple-tomcat-instances&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt; to setup multiple instances of tomcat and deploy a sample app.&lt;/p&gt;

&lt;h3 id=&quot;download-apache-nginx&quot;&gt;&lt;strong&gt;Download Apache NGINX&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Let’s start off by downloading open source windows version of NGINX web server software. The latest version can be downloaded &lt;strong&gt;&lt;a href=&quot;http://nginx.org/en/download.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For this tutorial, I have downloaded NGINX windows version (nginx/Windows-1.17.6) &lt;strong&gt;&lt;a href=&quot;http://nginx.org/download/nginx-1.17.6.zip&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt;. Unzip the downloaded file to your working directory. I have downloaded it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M:\nginx-tomcat-load-balancer-setup&lt;/code&gt; folder and my folder structure looks like the one shown below.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;cd M:\nginx-tomcat-load-balancer-setup\nginx-1.17.6
dir
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Directory of M:\nginx-tomcat-load-balancer-setup\nginx-1.17.6

11/19/2019  07:19 AM    &amp;lt;DIR&amp;gt;          .
11/19/2019  07:19 AM    &amp;lt;DIR&amp;gt;          ..
11/19/2019  07:19 AM    &amp;lt;DIR&amp;gt;          conf
11/19/2019  07:19 AM    &amp;lt;DIR&amp;gt;          contrib
11/19/2019  07:19 AM    &amp;lt;DIR&amp;gt;          docs
11/19/2019  07:19 AM    &amp;lt;DIR&amp;gt;          html
12/21/2019  10:43 PM    &amp;lt;DIR&amp;gt;          logs
11/19/2019  06:26 AM         3,710,464 nginx.exe
12/21/2019  10:40 PM    &amp;lt;DIR&amp;gt;          temp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Start NGINX server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On command line terminal, execute the below mentioned commands to start the NGINX server.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;M:\nginx-tomcat-load-balancer-setup\nginx-1.17.6
nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Validate the startup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Test the NGINX server in the browser using the URL &lt;a href=&quot;http://localhost:80/&quot; target=&quot;_blank&quot;&gt;http://localhost:80/&lt;/a&gt; or simply &lt;a href=&quot;http://localhost/&quot; target=&quot;_blank&quot;&gt;http://localhost/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;NGINX server will render a welcome page as shown below.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/nginx/nginx_80.png&quot; alt=&quot;Data Flow&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now we are all set to configure our NGINX server to work as a load balancer for tomcat instances.&lt;/p&gt;

&lt;h3 id=&quot;configure-nginx-with-tomcat-instances&quot;&gt;&lt;strong&gt;Configure NGINX with tomcat instances&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Add tomcat server details&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I have 2 instances of tomcat running on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7070&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9090&lt;/code&gt;. Each of these instances also has a sample web app deployed.&lt;/p&gt;

&lt;p&gt;Before we proceed with NGINX setup, let’s make sure these tomcat server instances are up and running by checking the sample app url for both tomcat the instances.&lt;/p&gt;

&lt;p&gt;Test the tomcat instance 1 app in the browser using the URL &lt;strong&gt;&lt;a href=&quot;http://localhost:7070/sample/&quot; target=&quot;_blank&quot;&gt;http://localhost:7070/sample/&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/nginx/nginx_sample_app_7070.png&quot; alt=&quot;Data Flow&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Similarly, test the tomcat instance 2 app in the browser using the URL &lt;strong&gt;&lt;a href=&quot;http://localhost:9090/sample/&quot; target=&quot;_blank&quot;&gt;http://localhost:9090/sample/&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/nginx/nginx_sample_app_9090.png&quot; alt=&quot;Data Flow&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now that the instances are up, let’s proceed with NGINX configuration setup.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Update &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nginx.conf&lt;/code&gt; file with below configuration which needs to be within the existing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http {...}&lt;/code&gt; block as shown &lt;strong&gt;&lt;a href=&quot;https://github.com/codeaches/nginx-tomcat-load-balancer-setup/blob/master/nginx-1.17.6/conf/nginx.conf&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;###########################################
# Default round robin load balancing config
###########################################
upstream my_tomcat_setup {
    server localhost:7070;
    server localhost:9090;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Add server details&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;###########################################
#
###########################################
server {
    listen 80;
    listen [::]:80;
    location / {
            proxy_redirect      off;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto $scheme;
            proxy_set_header    Host $host;
            proxy_pass          http://my_tomcat_setup;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Reload NGINX Config&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s reload NGINX configuration for the above changes to take effect by executing the below command in the command line.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;cd M:\nginx-tomcat-load-balancer-setup\nginx-1.17.6
nginx.exe -s reload
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;test-the-sample-web-app-using-nginx-port-80&quot;&gt;&lt;strong&gt;Test the sample web app using NGINX port 80&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Now, test the NGINX setup by hitting the sample app URI using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port 80&lt;/code&gt; in the browser using the URL &lt;strong&gt;&lt;a href=&quot;http://localhost:80/sample/&quot; target=&quot;_blank&quot;&gt;http://localhost:80/sample/&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Refresh the URL and the page will be rendered by instance 1 and 2 in round robin way.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/nginx/nginx_sample_app_80.png&quot; alt=&quot;Data Flow&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stop the NGINX server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use the below command to stop the NGINX service.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;cd M:\nginx-tomcat-load-balancer-setup\nginx-1.17.6
nginx.exe -s stop
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nginx.exe -h&lt;/code&gt; will give you list of all the commands which can be used with NGINX.&lt;/p&gt;

&lt;h3 class=&quot;no_toc&quot; id=&quot;summary&quot;&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;This concludes our guide to setting up NGINX as a reverse proxy for multiple tomcat instances.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your feedback is always appreciated. Happy coding!&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pavan Gurudutt</name></author><category term="Apache Tomcat" /><category term="NGINX" /><summary type="html">In this post, let’s download NGINX software and setup NGINX as a load balancer for a sample application running on two instances of tomcat. In this scenario, NGINX will be setup to act as a reverse proxy.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codeaches.com/assets/images/codeaches.png" /><media:content medium="image" url="https://codeaches.com/assets/images/codeaches.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>