<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shubham Kumar</title>
    <description>The latest articles on Forem by Shubham Kumar (@schwiftycold).</description>
    <link>https://forem.com/schwiftycold</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1137496%2F90733bdb-98d7-4607-9ed7-33540b3b73e7.png</url>
      <title>Forem: Shubham Kumar</title>
      <link>https://forem.com/schwiftycold</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/schwiftycold"/>
    <language>en</language>
    <item>
      <title>Working on mulitple braches at once using git worktree</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sun, 02 Mar 2025 14:50:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/working-on-mulitple-braches-at-once-using-git-worktree-2k18</link>
      <guid>https://forem.com/schwiftycold/working-on-mulitple-braches-at-once-using-git-worktree-2k18</guid>
      <description>&lt;h2&gt;
  
  
  Working on multiple branches at once
&lt;/h2&gt;

&lt;p&gt;Git worktree enables you to have multiple working directories linked to the same repository. Each directory can have its own branch checked out. And you can edit the code for each feature without changing or stashing the changes you did on each branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Worktree creation
&lt;/h2&gt;

&lt;p&gt;The following command will create a new directory, &lt;code&gt;debug&lt;/code&gt; with &lt;code&gt;branch1&lt;/code&gt; checked out.&lt;/p&gt;

&lt;p&gt;You can browse the &lt;code&gt;../debug&lt;/code&gt; directory and change the code as per your development for &lt;code&gt;branch1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree add ../debug branch1

&lt;span class="nb"&gt;pwd
echo &lt;/span&gt;somenewfeature on branch 1 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; somenewfeature
&lt;span class="nb"&gt;cat &lt;/span&gt;somenewfeature

/home/cold/Projects/Personal/gitworktreedemo/debug
somenewfeature on branch 1

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

&lt;/div&gt;



&lt;p&gt;In the meanwhile you can keep editing the root directory with the &lt;code&gt;main&lt;/code&gt; branch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;pwd
echo &lt;/span&gt;somenewfeature on main 1 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; somenewfeatureonmain
&lt;span class="nb"&gt;cat &lt;/span&gt;somenewfeatureonmain

/home/cold/Projects/Personal/gitworktreedemo/main
somenewfeature on main 1

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

&lt;/div&gt;



&lt;p&gt;You can run the git commands going inside each directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;pwd
&lt;/span&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
git status

/home/cold/Projects/Personal/gitworktreedemo/main
On branch main
Changes to be committed:
  &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git restore --staged &amp;lt;file&amp;gt;..."&lt;/span&gt; to unstage&lt;span class="o"&gt;)&lt;/span&gt;
    new file: somenewfeatureonmain


&lt;span class="nb"&gt;pwd
&lt;/span&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
git status

/home/cold/Projects/Personal/gitworktreedemo/debug
On branch branch1
Changes to be committed:
  &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git restore --staged &amp;lt;file&amp;gt;..."&lt;/span&gt; to unstage&lt;span class="o"&gt;)&lt;/span&gt;
    new file: somenewfeature

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  List existing worktrees
&lt;/h2&gt;

&lt;p&gt;You can list your worktrees from the &lt;code&gt;main&lt;/code&gt; as well as the &lt;code&gt;debug&lt;/code&gt; directories.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree list

/home/cold/Projects/Personal/gitworktreedemo/main e0ccbca &lt;span class="o"&gt;[&lt;/span&gt;main]
/home/cold/Projects/Personal/gitworktreedemo/debug 42ad28a &lt;span class="o"&gt;[&lt;/span&gt;branch1]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Remove a worktree
&lt;/h2&gt;

&lt;p&gt;When you are finally done with the feature branch, you can delete the worktree using &lt;code&gt;remove&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree remove ../debug
&lt;span class="nb"&gt;ls&lt;/span&gt; ..
git worktree list

main
/home/cold/Projects/Personal/gitworktreedemo/main e0ccbca &lt;span class="o"&gt;[&lt;/span&gt;main]

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

&lt;/div&gt;



&lt;p&gt;Any commit you did on the feature branch will automatically be reflected in the main codebase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout branch1
git log &lt;span class="nt"&gt;--oneline&lt;/span&gt;

A   somenewfeatureonmain
82cad2d &lt;span class="k"&gt;done
&lt;/span&gt;42ad28a A commit on branch1
e0ccbca A commit on main

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advice on working with worktrees
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;As the new directory will not be ignored by default, it is best to create your worktree directories out of git root&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The way I create my worktrees is I create a new directory with project name. Then I clone my actual repository inside this directory and keep my worktrees on the same level as project.&lt;/p&gt;

&lt;p&gt;For example, my main project is cloned inside main directory and I created debug directory in the same level as main.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tree &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="nb"&gt;.&lt;/span&gt;
├── debug
│   └── somenewfeature
└── main
    └── somenewfeatureonmain

2 directories, 2 files

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

&lt;/div&gt;



</description>
      <category>git</category>
      <category>worktrees</category>
    </item>
    <item>
      <title>Displaying full content in rss.xml in Hugo</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sun, 02 Mar 2025 13:50:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/displaying-full-content-in-rssxml-in-hugo-15l2</link>
      <guid>https://forem.com/schwiftycold/displaying-full-content-in-rssxml-in-hugo-15l2</guid>
      <description>&lt;p&gt;Create a new file at &lt;code&gt;layouts/_default/rss.xml&lt;/code&gt;. You can define your &lt;code&gt;rss.xml&lt;/code&gt; as per your liking.&lt;/p&gt;

&lt;p&gt;By default &lt;code&gt;rss.xml&lt;/code&gt; only displays the summary of your articles. But I want to display the whole content. This helps me in syncing my posts with my &lt;code&gt;dev.to&lt;/code&gt; site.&lt;/p&gt;

&lt;p&gt;You can find the Hugo’s default &lt;code&gt;rss.xml&lt;/code&gt; at &lt;a href="https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml" rel="noopener noreferrer"&gt;Hugo’s Github repo&lt;/a&gt;. Let’s copy the contents to our &lt;code&gt;rss.xml&lt;/code&gt; file that we just created.&lt;/p&gt;

&lt;p&gt;Then change the &lt;code&gt;description&lt;/code&gt; to show content instead of summary as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-&amp;lt;description&amp;gt;{{ .Summary | transform.XMLEscape | safeHTML }}&amp;lt;/description&amp;gt;
&lt;/span&gt;&lt;span class="gi"&gt;+&amp;lt;description&amp;gt;{{ .Content | transform.XMLEscape | safeHTML }}&amp;lt;/description&amp;gt;
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before the changes there were only 504 characters in the characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl localhost:1313/index.xml | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"description"&lt;/span&gt; | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 3 | &lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 1 | &lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt;

: 504
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the changes the number of characters increased significantly as the whole blog is being rendered in the description.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl localhost:1313/index.xml | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"description"&lt;/span&gt; | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 3 | &lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 1 | &lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt;

: 14582
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Partials in Hugo</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Tue, 25 Feb 2025 14:39:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/partials-in-hugo-34m5</link>
      <guid>https://forem.com/schwiftycold/partials-in-hugo-34m5</guid>
      <description>&lt;h2&gt;
  
  
  Partials and code reusability
&lt;/h2&gt;

&lt;p&gt;Hugo provides a nice mechanism to separate your components and stitch them at different places. This means you can design your components like nav-bar or menu bar and stitch them in all of your layouts. Earlier, we wrote some code to load tailwind in our index page. But we do not want to write this boilerplate code for all our pages. Instead we should have defined the tailwind loader code in a component and call it from all the layouts. Actually, we should have called the boilerplate code from &lt;code&gt;baseof.html&lt;/code&gt; from which all other layouts are inherited.&lt;/p&gt;

&lt;h2&gt;
  
  
  baseof.html
&lt;/h2&gt;

&lt;p&gt;When we created the theme, a special file was created for us, &lt;code&gt;baseof.html&lt;/code&gt;. Every other layout inherits &lt;code&gt;baseof.html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you look at the contents of &lt;code&gt;baseof.html&lt;/code&gt; which was generated for use. You will find 3 partial sections, head, header and footer. These are the default standards but you will go on creating partials as your code base becomes more complex.&lt;/p&gt;

&lt;p&gt;The main block denoted by &lt;code&gt;{{ block "main" . }}&lt;/code&gt;, is where your content from other pages will be inserted. You can define your layout as main in other layout files and that block will be inserted here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    {{- partial "head.html" . -}}
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        {{- partial "header.html" . -}}
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        {{- block "main" . }}{{- end }}
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        {{- partial "footer.html" . -}}
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can ignore the dashed infront and at last of the decalrations. These are the guide for Hugo to trim spaces infront and back.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  head.html
&lt;/h2&gt;

&lt;p&gt;After &lt;code&gt;html&lt;/code&gt; tag we have a partial &lt;code&gt;head.html&lt;/code&gt; instead of a head tag. Hugo is smart enough to generate &lt;code&gt;head&lt;/code&gt; attribute from the partial. Looking closely, it just process your partials and replaces the partial block with it.&lt;/p&gt;

&lt;p&gt;We could have defined out tailwind loading code here as follows. Now we got the support for tailwind everywhere in our site. You can also define some meta tags or other libraries here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  {{ with resources.Get "css/main.css" }}
  {{ with . | css.TailwindCSS }}
      &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"{{ .RelPermalink }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  {{ end }}
  {{ end }}
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Defining custom partials
&lt;/h2&gt;

&lt;p&gt;Defining partials is as easy as it can be. You just need to create a file under &lt;code&gt;partials&lt;/code&gt; directory. And write whatever processing you want to do. And include it in you main layout using &lt;code&gt;{{ partial "your-file.html" . }}&lt;/code&gt;. You can pass any data along with your partial definition. In the above partial definition, we are passing the current context to our partial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make CSS styling a partial
&lt;/h2&gt;

&lt;p&gt;Let’s create a new partial by creating a file called &lt;code&gt;css.html&lt;/code&gt;. This will contain the logic to process and import our tailwind libraray.&lt;/p&gt;

&lt;p&gt;The contents of &lt;code&gt;css.html&lt;/code&gt; will be as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;{{ with resources.Get "css/main.css" }}
{{ with . | css.TailwindCSS }}
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"{{ .RelPermalink }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
{{ end }}
{{ end }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And our head block will include this as a partial as follows. The below code goes inside &lt;code&gt;baseof.html&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  {{ partial "css.html" . }}
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Modify the pages to use baseof
&lt;/h2&gt;

&lt;p&gt;You will need to define the &lt;code&gt;main&lt;/code&gt; block in all your layouts. This will help Hugo to link the contents of layouts page and combine it with baseof to generate the rendered page.&lt;/p&gt;

&lt;p&gt;Below is how, I modified my &lt;code&gt;index.html&lt;/code&gt; page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;{{ define "main" }}
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"text-red-500"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;This paragraph is red.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  {{ .Content }}
{{ end }}

curl localhost:1313 | grep "main.css"

: &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/css/main.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks as expected. We can see out main.css linked to our homepage.&lt;/p&gt;

&lt;p&gt;Now let’s modify the &lt;code&gt;list.html&lt;/code&gt; layout and define main here as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;{{ define "main" }}
  &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
    {{ range .Pages }}
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;{{ $.Site.Title }} - {{ $.Title }} - {{ .Title }}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    {{ end }}
  &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
{{ end }}

curl localhost:1313/posts/ | grep "main.css"

: &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/css/main.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome, we can now use tailwind from anywhere in our website.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this post we explored partials which is basically a way of reusing the code in Hugo. We learned about &lt;code&gt;baseof.html&lt;/code&gt; which is inherited by all the layouts. We combined our tailwind learning with partials and created a &lt;code&gt;css.html&lt;/code&gt; partial which can now be used from anywhere.&lt;/p&gt;

</description>
      <category>hugo</category>
      <category>partials</category>
    </item>
    <item>
      <title>Tailwind support in Hugo</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sun, 23 Feb 2025 16:57:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/tailwind-support-in-hugo-2pe2</link>
      <guid>https://forem.com/schwiftycold/tailwind-support-in-hugo-2pe2</guid>
      <description>&lt;h2&gt;
  
  
  Expectations from this post
&lt;/h2&gt;

&lt;p&gt;With the version &lt;code&gt;0.128.0&lt;/code&gt;, Hugo started supporting Tailwind internally. In this post, let’s configure Tailwind and display some text in red.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tailwind and other supporting apps
&lt;/h2&gt;

&lt;p&gt;We will install the below 4 packages.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;tailwindcss - This is the core Tailwind library and is required for using tailwind&lt;/li&gt;
&lt;li&gt;tailwind/cli - This is a CLI tool for Tailwind that helps in building tailwind styles&lt;/li&gt;
&lt;li&gt;postcss - This is a library to process CSS with JavaScript plugins.&lt;/li&gt;
&lt;li&gt;autoprefixer - This is a postcss library that helps in automatically generating the vendor prefixes like &lt;code&gt;-webkit-&lt;/code&gt; and &lt;code&gt;-moz-&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will install these packages as dev as they play no role after building the site.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; tailwindcss @tailwindcss/cli postcss autoprefixer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PostCSS config
&lt;/h2&gt;

&lt;p&gt;You need to specify the plugins for &lt;code&gt;postcss&lt;/code&gt;. This can be done by creating a file, &lt;code&gt;postcss.config.js&lt;/code&gt;. You can add the plugins you want to use here as follows.&lt;/p&gt;

&lt;p&gt;For now, we will add &lt;code&gt;tailwindcss&lt;/code&gt; and &lt;code&gt;autoprefixer&lt;/code&gt; as the plugins.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;  &lt;span class="err"&gt;plugins:&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;tailwindcss:&lt;/span&gt; &lt;span class="err"&gt;{},&lt;/span&gt;
    &lt;span class="err"&gt;autoprefixer:&lt;/span&gt; &lt;span class="err"&gt;{},&lt;/span&gt;
  &lt;span class="err"&gt;},&lt;/span&gt;
&lt;span class="err"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tailwind config
&lt;/h2&gt;

&lt;p&gt;We can create a more advanced config for Tailwind later.&lt;/p&gt;

&lt;p&gt;For now, let’s just create a file, &lt;code&gt;tailwind.config&lt;/code&gt; without any contents in the theme’s root directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Loading Tailwind on the index page
&lt;/h2&gt;

&lt;p&gt;Let’s first create a resource &lt;code&gt;main.css&lt;/code&gt; in the assets directory of the theme. Here we will import Tailwind and then we will load this resource in our index file using some Hugo magic.&lt;/p&gt;

&lt;h3&gt;
  
  
  main.css
&lt;/h3&gt;

&lt;p&gt;In the theme’s root directory, let’s create a directory &lt;code&gt;assets/css&lt;/code&gt; and add a file &lt;code&gt;main.css&lt;/code&gt; here.&lt;/p&gt;

&lt;p&gt;The content of the file will just load the Tailwind library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tailwindcss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  index.html
&lt;/h3&gt;

&lt;p&gt;Here, we just want to add &lt;code&gt;main.css&lt;/code&gt; as our stylesheet.&lt;/p&gt;

&lt;p&gt;This is done by using &lt;code&gt;link&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;First we want to load the resource then we want to process it using tailwind and add it to our page.&lt;/p&gt;

&lt;p&gt;We can use Hugo’s &lt;code&gt;with&lt;/code&gt; function to load the file and again using &lt;code&gt;with&lt;/code&gt;, we can process the obtained file using tailwind. The below snippet will do the trick.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{{ with resources.Get "css/main.css" }}&lt;/code&gt; will try to get the main.css file. Hugo will look into the assets direcetry for &lt;code&gt;css/main.css&lt;/code&gt;.&lt;code&gt;{{ with . }}&lt;/code&gt; will only be processed if the file was found. And if found we can pipe the contents to be processed by &lt;code&gt;css.TailwindCSS&lt;/code&gt; provided by Hugo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;{{ with resources.Get "css/main.css" }}
    {{ with . | css.TailwindCSS }}
        &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"{{ .RelPermalink }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ end }}
{{ end }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would be enough to start using Tailwind functionalities.&lt;/p&gt;

&lt;p&gt;To test if it is working of not, we can add a simple text and use some tailwind features to make it red.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;index.html&lt;/code&gt;, body let’s add the following. Adding class as &lt;code&gt;text-red-500&lt;/code&gt;, should turn the content of &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; to red.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"text-red-500"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;This paragraph is red.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Taking a peek
&lt;/h2&gt;

&lt;p&gt;Let’s first curl the homepage and verify of link was created successfully.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl localhost:1313

&amp;lt;html&amp;gt;
  &amp;lt;&lt;span class="nb"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &amp;lt;meta &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"generator"&lt;/span&gt; &lt;span class="nv"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Hugo 0.144.2"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;script &lt;span class="nv"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/livereload.js?mindelay=10&amp;amp;amp;v=2&amp;amp;amp;port=1313&amp;amp;amp;path=livereload"&lt;/span&gt; data-no-instant defer&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;title&amp;gt;
      Shubham&amp;amp;#39&lt;span class="p"&gt;;&lt;/span&gt;s corner
    &amp;lt;/title&amp;gt;
        &amp;lt;&lt;span class="nb"&gt;link &lt;/span&gt;&lt;span class="nv"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"stylesheet"&lt;/span&gt; &lt;span class="nv"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/css/main.css"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;p &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"text-red-500"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;This paragraph is red.&amp;lt;/p&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The file main.css here is the processed main.css and will look very different than what we have written.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is good, next let’s see the actual site. &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhct6b5hsytuid8ofila.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhct6b5hsytuid8ofila.png" width="316" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The rendered text is red which means we were finally able to load out Tailwind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this post, we saw how to enable the support of Tailwind CSS in out Hugo site. We added some text and turned it to red using Tailwind classes. Next, we will explore code re-usability in Hugo which is where partials come to play.&lt;/p&gt;

</description>
      <category>hugo</category>
      <category>tailwindcss</category>
    </item>
    <item>
      <title>Upgrading Hugo to latest version on Debian based systems</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sun, 23 Feb 2025 15:59:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/upgrading-hugo-to-latest-version-on-debian-based-systems-4mhi</link>
      <guid>https://forem.com/schwiftycold/upgrading-hugo-to-latest-version-on-debian-based-systems-4mhi</guid>
      <description>&lt;p&gt;There is a bad news for Ubuntu (or any Debian based OS) user if you have installed Hugo using &lt;code&gt;apt&lt;/code&gt;. The official &lt;code&gt;apt&lt;/code&gt; repository is not being updated continuously.&lt;/p&gt;

&lt;p&gt;Today, I wanted to experiment with the Tailwind support introduced in Hugo &lt;code&gt;0.128.0&lt;/code&gt;. But the apt package manager does not has this version. The latest version is &lt;code&gt;0.92.2&lt;/code&gt; as of now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;apt list &lt;span class="nt"&gt;-a&lt;/span&gt; hugo

: Listing...
: hugo/jammy-updates,jammy-security,now 0.92.2-1ubuntu0.1 amd64 &lt;span class="o"&gt;[&lt;/span&gt;installed]
: hugo/jammy 0.92.2-1 amd64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In such scenarios, you can install the &lt;code&gt;.deb&lt;/code&gt; file directly from the published releases. The disadvantage of this method is, you will lose the ability to upgrade the version automatically. In the future, you will be restricted to download the &lt;code&gt;deb&lt;/code&gt; file and install the updated version.&lt;/p&gt;

&lt;p&gt;Anyways, here are the steps to do so.&lt;/p&gt;

&lt;p&gt;First look for the latest (or the version you want) to install at the GoHugoIO release page - &lt;a href="https://github.com/gohugoio/hugo/releases" rel="noopener noreferrer"&gt;https://github.com/gohugoio/hugo/releases&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the time of writing this post, the latest version is &lt;code&gt;0.144.2&lt;/code&gt;. Any my current version is &lt;code&gt;0.92.2&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;hugo version

: hugo v0.92.2+extended linux/amd64 &lt;span class="nv"&gt;BuildDate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2023-01-31T11:11:57Z &lt;span class="nv"&gt;VendorInfo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ubuntu:0.92.2-1ubuntu0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the Downloads directory, I can run the below command to download the deb file that I want to install. You can use any directory to download this file. Generally people use &lt;code&gt;/tmp&lt;/code&gt;, but I like all my downloads in my &lt;code&gt;Downloads&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://github.com/gohugoio/hugo/releases/download/v0.144.2/hugo_0.144.2_linux-amd64.deb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next you can install the &lt;code&gt;deb&lt;/code&gt; file as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;hugo

: hugo_0.144.2_linux-amd64.deb

dpkg &lt;span class="nt"&gt;-i&lt;/span&gt; hugo_0.144.2_linux-amd64.deb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should upgrade your installation to latest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;hugo version

: hugo v0.144.2-098c68fd18f48031a7145bedab30cbaede48858f linux/amd64 &lt;span class="nv"&gt;BuildDate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2025-02-19T12:17:04Z &lt;span class="nv"&gt;VendorInfo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;gohugoio
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this will help you upgrading Hugo to latest version on Debian based systems.&lt;/p&gt;

</description>
      <category>hugo</category>
      <category>debian</category>
      <category>upgrade</category>
    </item>
    <item>
      <title>Object pool design pattern in Java</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sat, 07 Dec 2024 17:34:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/object-pool-design-pattern-in-java-220o</link>
      <guid>https://forem.com/schwiftycold/object-pool-design-pattern-in-java-220o</guid>
      <description>&lt;h2&gt;
  
  
  What is it?
&lt;/h2&gt;

&lt;p&gt;The object pool design pattern exposes a manager to manage a pool of reusable objects. The idea is to keep a know number of reusable objects (with a hard limit to initialize some more lazily). Whenever someone need the object from the pool, it will ask the pool manager. If there are free objects, the manager will engage one for your. If there aren’t any free objects but the hard limit is not breached, then the manager will initialize a new object and provide you. Else if the hard limit is breached then you will return empty handed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is this?
&lt;/h2&gt;

&lt;p&gt;This is used when we have an resource object which takes some time to initialize. And once initialized, it can be reused over and over again without a performance hit.&lt;/p&gt;

&lt;p&gt;Basically, creation is expensive and hence we want to reuse already created instances. Generally, we put a soft limit on the number of resources initially initialized. And we want to create more resources lazily if required. To prevent a huge number of resources from being created, we also put a hard limit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;At GreyOrange, we use something called as IDC files. These are huge binary files (sometimes 100-200 GBs). They provide the time it takes to travel b/w 2 coordinates. We created an IDC Manager to parse these files and provide us the required information. The initialization takes a huge amount of time (sometimes 10s). Once initialized, it takes less than 1ms to provide the information.&lt;/p&gt;

&lt;p&gt;Right now, we are good with just once instance of this manager, so it is a singleton class with one buffer linked to an IDC file. But if the demands for parallel calls increases, we might want to implement the manager as a Object Pool.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to implement this?
&lt;/h2&gt;

&lt;p&gt;There are 3 requirements.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An Object Pool Manager&lt;/li&gt;
&lt;li&gt;The initial number of objects - m&lt;/li&gt;
&lt;li&gt;The maximum number of objects - n&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Object Pool Manager will be a singleton class. We cannot allow multiple Object Pool Manager objects as they will create max, n objects each.&lt;/p&gt;

&lt;p&gt;We will create 2 lists, &lt;code&gt;availableResources&lt;/code&gt; and &lt;code&gt;enagaedResources&lt;/code&gt;. Initially, we will populate the &lt;code&gt;availableResources&lt;/code&gt; with m new resource objects.&lt;/p&gt;

&lt;p&gt;Each getter call will check the &lt;code&gt;availableResources&lt;/code&gt; list for available objects. If the objects are available then it will move the last object to engagedObjects. If the objects are not available then there are 2 choices. Check the hard limit, if not reached then create more objects and add to &lt;code&gt;availableResources&lt;/code&gt;. Else return null.&lt;/p&gt;

&lt;p&gt;A pseudo code for the manager is as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PoolManager&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;PoolManager&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;availableResources&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;engagedResources&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;initialLimit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;hardLimit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;PoolManager&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Get these properties from already defined config&lt;/span&gt;
        &lt;span class="c1"&gt;// Assume this is defined as per standard or equivalent configurations&lt;/span&gt;
        &lt;span class="n"&gt;initialLimit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getIntegerValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"POOL_INITIAL_LIMIT"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;hardLimit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getIntegerValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"POOL_HARD_LIMIT"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;availableResources&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;engagedResources&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Initialize the initial number of resources in the pool&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;initialLimit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;availableResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResourceObject&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;PoolManager&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;synchronized&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PoolManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PoolManager&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;getObject&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;availableResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// A sync is required as 2 thread may want to get a free object at the same time&lt;/span&gt;
            &lt;span class="kd"&gt;synchronized&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;availableResources&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;freeObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;availableResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;availableResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;engagedResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;freeObject&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;freeObject&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engagedResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;hardLimit&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;freeObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResourceObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;availableResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;freeObject&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;getObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;releaseObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;engagedObject&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engagedObject&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;synchronized&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engagedResources&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;freeObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;engagedResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engagedObject&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="n"&gt;availableResources&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;freeObject&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;){}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>design</category>
      <category>code</category>
    </item>
    <item>
      <title>Downloading a single file from 2 independent apps</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sat, 31 Aug 2024 18:41:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/downloading-a-single-file-from-2-independent-apps-kke</link>
      <guid>https://forem.com/schwiftycold/downloading-a-single-file-from-2-independent-apps-kke</guid>
      <description>&lt;h2&gt;
  
  
  Understanding the problem
&lt;/h2&gt;

&lt;p&gt;Let’s say you have a very large log file. And you want to create an app that can analyze this file and generate insights. Also, let’s say you want to create an another app that can simulate the work by reading the logs one-by-one.&lt;/p&gt;

&lt;p&gt;Both these apps are dependent on the same log file. Now, there are 2 scenarios.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;App1 starts, downloads the file and then App2 starts.&lt;/li&gt;
&lt;li&gt;App1 starts, downloading the file and App2 starts while the download is incomlete.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first scenario is easy to deal with. We can check the &lt;code&gt;md5sum&lt;/code&gt; of the local file and the file on the server. If they match, nothing to worrry about. If they don’t then we can have a complex logic to determine the life of the old log file and decide accordingly.&lt;/p&gt;

&lt;p&gt;The second scenario is conflicting one and this we can solve in code. The second scenario can also happen when the same app is ran twice simultaneously. Both the instances will start downloading the same file and this will create a havoc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;The idea is to have an identifier that an app has already started the download and is still downloading the resouce. If the first app has started the download, then wait for the first app to complete the download and then only start the application.&lt;/p&gt;

&lt;p&gt;For accomplishing this, we generally use file locking mechanism.&lt;/p&gt;

&lt;h3&gt;
  
  
  Download with file locking
&lt;/h3&gt;

&lt;p&gt;The process is modified to first create a lock file with extension &lt;code&gt;.lock&lt;/code&gt;. This lock file signifies that a download is already in progress. If this lock file exists then wait for the download to complete by the second app. The lock file will have &lt;code&gt;processid_threadid&lt;/code&gt; as identifier. This is useful in checking the race condition that can happen while writing the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;downloadFileWithLock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="n"&gt;lockFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;".lock"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Check if the file is being downloaded by another app&lt;/span&gt;
    &lt;span class="c1"&gt;// If it is being downloaded by an another app then wait for the download to finish&lt;/span&gt;
    &lt;span class="c1"&gt;// Else proceed with the download&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;waitForDownloadToFinish&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;processID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;ProcessHandle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;current&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"_"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;processID&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;identifier&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;writeToFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;// May be due to race condition, the file is already downloaded by another app&lt;/span&gt;
      &lt;span class="c1"&gt;// Check if this process started the download&lt;/span&gt;
      &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;savedIdentifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;readFromFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;identifier&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedIdentifier&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Download the file&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" - Downloading file..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="n"&gt;downloadFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="nc"&gt;RandomAccessFile&lt;/span&gt; &lt;span class="n"&gt;randomAccessFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RandomAccessFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;downloadFile&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"rw"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
          &lt;span class="n"&gt;randomAccessFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Very important works"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
          &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
        &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" - File downloaded successflly."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;waitForDownloadToFinish&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Utilities method - &lt;code&gt;waitFoDownloadToFinish&lt;/code&gt;, &lt;code&gt;readFromFile&lt;/code&gt; and &lt;code&gt;writeToFile&lt;/code&gt; are as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;writeToFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nc"&gt;Files&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toPath&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;readFromFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Files&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readAllBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toPath&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;waitForDownloadToFinish&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" - File is already being downloaded by another app. Wait for it to finish."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lockFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" - File download completed."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can create 2 new apps that will call this method and we will run the apps simultaneously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App1&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"downloaded_file.txt"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;FileDownloadUtil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;downloadFileWithLock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" - App1 starting operation..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App2&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"downloaded_file.txt"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;FileDownloadUtil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;downloadFileWithLock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" - App2 starting operation..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Outputs
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# For App1&lt;/span&gt;
main - Downloading file...
main - File downloaded successfully.
main - App1 starting operation...

&lt;span class="c"&gt;# For App2&lt;/span&gt;
main - File is already being downloaded by another app. Wait &lt;span class="k"&gt;for &lt;/span&gt;it to finish.
main - File download completed.
main - App2 starting operation...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;App1 started downloading the file and thus App2 waited for the download to complete. After the download completes, both the apps resumed its operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion and improvements
&lt;/h2&gt;

&lt;p&gt;This is just a basic code that lays the foundation of file locking mechanism for downloading a file simultaneously by multiple apps. This code is not a production ready code. A more complete solution should handle scenarios like downloads in chunks, resume functionality with unexpected shutdowns and other edge cases.&lt;/p&gt;

</description>
      <category>java</category>
      <category>design</category>
    </item>
    <item>
      <title>Reflection API in Java</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Fri, 21 Jun 2024 18:33:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/reflection-api-in-java-jn9</link>
      <guid>https://forem.com/schwiftycold/reflection-api-in-java-jn9</guid>
      <description>&lt;h2&gt;
  
  
  Where is this used?
&lt;/h2&gt;

&lt;p&gt;This is used to analyze/modify the behaviour of a class at runtime. Using this, you can view or change the private/public fields at wish (without exposing any getter/setter). Personally, I have used this in one of our projects at GreyOrange to write unit test cases. Using this in main code is a big no-no as it exposed you critical fields to the world.&lt;/p&gt;

&lt;h2&gt;
  
  
  Main Class
&lt;/h2&gt;

&lt;p&gt;Let’s create a main class for which we will write some test cases. But we want to test some private fields for which we don’t have a direct getter. The idea is to use reflection api to access such fields and fetch their current value or modify them if required.&lt;/p&gt;

&lt;p&gt;Here is a Duck class which has 3 fields of which 1 is static. Each time a duck class is created count which is the static field is increased by one. Each duck has an associated name and age.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;canCreateMoreDucks&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;canDrinkAlcohol&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Test Class – uses reflection API
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Change the value of a private field inside a class
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Field&lt;/code&gt; and &lt;code&gt;getDeclaredField&lt;/code&gt; are used to access a variable. Using &lt;code&gt;setAccessible&lt;/code&gt; as true will expose any private fields which can be manipulated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testDuckCanDrinkAlcohol&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Donald"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Donald"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;assertFalse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;canDrinkAlcohol&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// change age and check if duck can drink alcohol&lt;/span&gt;
    &lt;span class="c1"&gt;// But I don't want to create a setter for this&lt;/span&gt;
    &lt;span class="c1"&gt;// Use reflection API to change the age&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;duckClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Field&lt;/span&gt; &lt;span class="n"&gt;ageField&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;duckClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDeclaredField&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;ageField&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAccessible&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;ageField&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;duck&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;assertTrue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;canDrinkAlcohol&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Get the value of a static private variable in a class
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;static&lt;/code&gt; field can be accessed in the similar way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testDuckCanCreateMoreDucks&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Instead of creating more ducks&lt;/span&gt;
    &lt;span class="c1"&gt;// I will use reflection API to change the count&lt;/span&gt;
    &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Donald"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;assertTrue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;canCreateMoreDucks&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// Also assert count was 1&lt;/span&gt;
    &lt;span class="c1"&gt;// But I don't want to create a getter for this&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;duckClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Field&lt;/span&gt; &lt;span class="n"&gt;countField&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;duckClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDeclaredField&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"count"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;countField&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAccessible&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Don't need to pass an instance as count is static&lt;/span&gt;
        &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;countObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;countField&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;countObject&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Change the value of a static private variable in a class
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;setInt&lt;/code&gt; to change the value of the &lt;code&gt;Field&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testDuckCannotCreateMoreDucks&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Instead of creating more ducks&lt;/span&gt;
    &lt;span class="c1"&gt;// I will use reflection API to change the count&lt;/span&gt;
    &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Donald"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// change count to 10&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;duckClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Field&lt;/span&gt; &lt;span class="n"&gt;countField&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;duckClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDeclaredField&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"count"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;countField&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAccessible&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;countField&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;assertFalse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;canCreateMoreDucks&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>reflection</category>
    </item>
    <item>
      <title>Using Jmespath in Emacs</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Wed, 07 Feb 2024 03:30:13 +0000</pubDate>
      <link>https://forem.com/schwiftycold/using-jmespath-in-emacs-ofp</link>
      <guid>https://forem.com/schwiftycold/using-jmespath-in-emacs-ofp</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When you have a small JSON file, it is quite easy to look for what you want. But querying a large JSON data is very troublesome.&lt;/p&gt;

&lt;p&gt;This is where people use tools like Jmespath to filter and transform data into their liking. This post is about a small wrapper over &lt;code&gt;jp&lt;/code&gt; CLI utility that you can use while working on JSON files in Emacs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing JP
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Linux
&lt;/h3&gt;

&lt;p&gt;On Linux, you can install the utility by first downloding the binary and then installing it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; sudo wget https://github.com/jmespath/jp/releases/latest/download/jp-linux-amd64 \
&amp;gt; -O /usr/local/bin/jp &amp;amp;&amp;amp; sudo chmod +x /usr/local/bin/jp

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Mac
&lt;/h3&gt;

&lt;p&gt;On Mac, you can install Jmespath CLI using &lt;code&gt;brew&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; brew install jmespath/jmespath/jp

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding Jmespath recipe in Emacs
&lt;/h2&gt;

&lt;p&gt;This is my first recipe that I published on &lt;a href="https://melpa.org/" rel="noopener noreferrer"&gt;MELPA&lt;/a&gt;.Here are the steps to install it in &lt;code&gt;Doom&lt;/code&gt; using &lt;code&gt;straight&lt;/code&gt;. You can use any package manager to install it using &lt;code&gt;MELPA&lt;/code&gt; repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  Doom Emacs
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;On Doom, you can just mention the below recipe in your &lt;code&gt;package.el&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; (package! jmespath)

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Also, add the below line in your &lt;code&gt;config.el&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; (use-package jmespath)

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using Jmespath
&lt;/h2&gt;

&lt;p&gt;There is an interactive function, &lt;code&gt;jmespath-query-and-show&lt;/code&gt; that you can use to query the currently opened buffer or a file. To use it with current buffer, you can simply call this function and enter your query. The output will be shown on a new buffer with name &lt;strong&gt;JMESPath Result&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To use this with a different file, you can set the &lt;code&gt;Universal Argument&lt;/code&gt; using &lt;code&gt;C-u&lt;/code&gt; or &lt;code&gt;SPC-u&lt;/code&gt; (using evil). Then you can enter the file to execute the query on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read more
&lt;/h2&gt;

&lt;p&gt;To learn more about JMESPath, visit &lt;a href="https://jmespath.org/" rel="noopener noreferrer"&gt;Jamespath page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>emacs</category>
      <category>doom</category>
      <category>jmespath</category>
    </item>
    <item>
      <title>Running PMML models in Erlang using NIF and CPP</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sun, 15 Oct 2023 05:10:32 +0000</pubDate>
      <link>https://forem.com/schwiftycold/running-pmml-models-in-erlang-using-nif-and-cpp-3ojc</link>
      <guid>https://forem.com/schwiftycold/running-pmml-models-in-erlang-using-nif-and-cpp-3ojc</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Erlang is a great language for building concurrent systems that are fault-tolerant and scalable. But it lacks some of the libraries that are available in other languages. One such example is using &lt;code&gt;PMML&lt;/code&gt; files for machine learning models. At the time of writing, Erlang doesn't have a library for parsing &lt;code&gt;PMML&lt;/code&gt; files. This is a problem for people who want to use Erlang for building machine learning systems. Here I'll show how to use &lt;code&gt;C++&lt;/code&gt; to build a &lt;code&gt;NIF&lt;/code&gt; that can be used in Erlang to parse &lt;code&gt;PMML&lt;/code&gt; files. More specifically, I'll use the &lt;a href="https://github.com/AmadeusITGroup/cPMML" rel="noopener noreferrer"&gt;cPMML&lt;/a&gt; library to build a &lt;code&gt;NIF&lt;/code&gt; that can be used in Erlang.&lt;/p&gt;

&lt;h2&gt;
  
  
  Erlang NIF
&lt;/h2&gt;

&lt;p&gt;Erlang NIF provides you a way to define your functions in &lt;code&gt;C/C++&lt;/code&gt; and call them in &lt;code&gt;Erlang&lt;/code&gt; code natively. The &lt;code&gt;C/C++&lt;/code&gt; program is compiled to generate a library file that can be used in Erlang. This library is dynamically linked to the Erlang VM and is the fastest way of calling the &lt;code&gt;C/C++&lt;/code&gt; code from &lt;code&gt;Erlang&lt;/code&gt;. The disadvantage of the approach is that if the &lt;code&gt;C/C++&lt;/code&gt; code crashes, it will crash the Erlang VM. And you will need to maintain the &lt;code&gt;C/C++&lt;/code&gt; code along with the &lt;code&gt;Erlang&lt;/code&gt; code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;cPMML&lt;/code&gt; library requires a version of &lt;code&gt;C++&lt;/code&gt; that supports &lt;code&gt;C++11&lt;/code&gt; standard. Also, make sure you have the required header files on the &lt;code&gt;Erlang&lt;/code&gt; side.&lt;/p&gt;

&lt;p&gt;You can find them on Mac OS or Linux using the find function. If the header files are located in multiple locations, use the &lt;code&gt;Celler&lt;/code&gt; one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;find / -name erl_nif.h | grep erl_nif.h

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hello Nif program
&lt;/h2&gt;

&lt;h3&gt;
  
  
  C/C++ code
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Header file
&lt;/h4&gt;

&lt;p&gt;You need to include the below header file to use the &lt;code&gt;Erlang&lt;/code&gt; functionalities. On a lower level, it defines the data structures and environment that &lt;code&gt;Erlang&lt;/code&gt; provides to the &lt;code&gt;C/C++&lt;/code&gt; code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;erl_nif.h&amp;gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Function Definition
&lt;/h4&gt;

&lt;p&gt;These functions are called from &lt;code&gt;Erlang&lt;/code&gt; code. They must follow a specific structure and return a specific type of value. Here, we will define a function that will return a "Hello, World!" string.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ERL_NIF_TERM&lt;/code&gt; is the return type of the function. It is an interface for various return types like &lt;code&gt;binary&lt;/code&gt;, &lt;code&gt;tuple&lt;/code&gt;, &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;atom&lt;/code&gt;, etc. Meaning, that you can return any of these types from the function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ErlNifEnv&lt;/code&gt; is the pointer to the Erlang environment. It provides you access to various &lt;code&gt;Erlang&lt;/code&gt; functionalities like memory management, exception handling and &lt;code&gt;Erlang&lt;/code&gt; term creation. For us, it will help in creating Erlang Terms like &lt;code&gt;string&lt;/code&gt;.&lt;code&gt;enif_make_string&lt;/code&gt; is the function that will create a string from the &lt;code&gt;C/C++&lt;/code&gt; code.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;argc&lt;/code&gt; and &lt;code&gt;argv&lt;/code&gt; provide the number of arguments and the arguments passed to the function from &lt;code&gt;Erlang&lt;/code&gt; code.&lt;/p&gt;

&lt;p&gt;Below is the function definition for &lt;code&gt;hello world&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static ERL_NIF_TERM hello_world(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
    return enif_make_string(env, "Hello, World!", ERL_NIF_LATIN1);
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Export functions
&lt;/h4&gt;

&lt;p&gt;You need to specify the functions that you want to export to the &lt;code&gt;Erlang&lt;/code&gt; code. The structure is a list of &lt;code&gt;ErlNifFunc&lt;/code&gt; objects. Each object has the name of the function that Erlang sees, the number of arguments and the function pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static ErlNifFunc nif_funcs[] = {
    {"hello_world", 0, hello_world}
};

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

&lt;/div&gt;



&lt;p&gt;To initialize the &lt;code&gt;NIF&lt;/code&gt; library, you need to call the &lt;code&gt;ERL_NIF_INIT&lt;/code&gt; function. This takes in the name of your &lt;code&gt;Erlang&lt;/code&gt; module and the exported functions.&lt;/p&gt;

&lt;p&gt;Let's call our &lt;code&gt;Erlang&lt;/code&gt; module &lt;code&gt;hello_nif&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERL_NIF_INIT(hello_nif, nif_funcs, NULL, NULL, NULL, NULL)

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Final code
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extern "C" {
  #include &amp;lt;erl_nif.h&amp;gt;
  static ERL_NIF_TERM hello_world(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
      return enif_make_string(env, "Hello, World!", ERL_NIF_LATIN1);
  }
  static ErlNifFunc nif_funcs[] = {
      {"hello_world", 0, hello_world}
  };
}

ERL_NIF_INIT(hello_nif, nif_funcs, NULL, NULL, NULL, NULL);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Erlang code
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Erlang module
&lt;/h4&gt;

&lt;p&gt;We will name our &lt;code&gt;Erlang&lt;/code&gt; module &lt;code&gt;hello_nif.erl&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-module(hello_nif).

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Define your NIF functions
&lt;/h4&gt;

&lt;p&gt;This specifies the functions that are exported from the &lt;code&gt;C/C++&lt;/code&gt; code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-export([hello_world/0]).
-nifs([hello_world/0]).

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Load the library on module load
&lt;/h4&gt;

&lt;p&gt;If you compiled your &lt;code&gt;C/C++&lt;/code&gt; code to a library named &lt;code&gt;hello_nif.so&lt;/code&gt;, you can load it using the &lt;code&gt;load_nif&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-on_load(init/0).

init() -&amp;gt;
    ok = erlang:load_nif("./hello_nif", 0).

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Fallback function
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;C/C++&lt;/code&gt; code can crash and you need to handle that in the &lt;code&gt;Erlang&lt;/code&gt; code. These functions will run when the &lt;code&gt;C/C++&lt;/code&gt; code crashes. You should name each of these functions with the same name in the &lt;code&gt;C/C++&lt;/code&gt; code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello_world() -&amp;gt;
    exit(nif_library_not_loaded).

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Final code
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-module(hello_nif).
-nifs([hello_world/0]).
-on_load(init/0).

init() -&amp;gt;
    ok = erlang:load_nif("./hello_nif", 0).

hello_world() -&amp;gt;
    exit(nif_library_not_loaded).

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Compiling and Running
&lt;/h3&gt;

&lt;p&gt;To compile the &lt;code&gt;C/C++&lt;/code&gt; code you can use &lt;code&gt;gcc&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Mac OS
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc -o hello_nif.so hello.c -I /usr/local/lib/erlang/erts-13.2.2.2/include/ -bundle -bundle_loader /usr/local/lib/erlang/erts-13.2.2.2/bin/beam.smp

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Linux
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc -o hello_nif.so hello.c -I /usr/lib/erlang/erts-13.2.2.2/include -shared -fpic

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

&lt;/div&gt;



&lt;p&gt;On Erlng, you can call the &lt;code&gt;hello_world&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; c(hello_nif).
&amp;gt; hello_nif:hello_world().
"Hello, World!"

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  cPMML NIF
&lt;/h2&gt;

&lt;p&gt;We saw how to create a simple &lt;code&gt;NIF&lt;/code&gt; that returns a string. Now, let's create a &lt;code&gt;NIF&lt;/code&gt; that can take input and run prediction using a &lt;code&gt;PMML&lt;/code&gt; model file. For this, we will use the same model file we created in the &lt;a href="https://dev.to/schwiftycold/running-aiml-predictions-in-cpp-using-cpmml-library-28oh"&gt;previous post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We have a PMML model for linear regression, &lt;code&gt;y = 2x + 1&lt;/code&gt; and we want to predict the value of &lt;code&gt;y&lt;/code&gt; for a given value of &lt;code&gt;x&lt;/code&gt;. Keeping it short, we will expose a function called, &lt;code&gt;predict&lt;/code&gt; that will take the input and return the output.&lt;/p&gt;

&lt;h3&gt;
  
  
  C/C++ code
&lt;/h3&gt;

&lt;p&gt;Assuming you can compile &lt;code&gt;cPMML&lt;/code&gt; files as described in the &lt;a href="https://dev.to/schwiftycold/running-aiml-predictions-in-cpp-using-cpmml-library-28oh"&gt;previous post&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Header files
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;string&amp;gt;
#include &amp;lt;map&amp;gt;
#include "cPMML.h"

using namespace std;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  PMML Parser class
&lt;/h4&gt;

&lt;p&gt;We want to load the model once and use it for multiple predictions. For this, let's create a class that will load the model and call predictions on it. We will maintain only one instance of this class and use it for all the predictions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PmmlModelParser {
private:
  cpmml::Model model;

public:
    PmmlModelParser(const string&amp;amp; modelname) {
        model = cpmml::Model(modelname);
    }

    string predict(const unordered_map&amp;lt;string, string&amp;gt;&amp;amp; x_input) {
        return model.predict(x_input);
    }
};

// Global variable
PmmlModelParser *pmmlModelParser = nullptr;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  NIF implementation
&lt;/h4&gt;

&lt;p&gt;We will create two functions, &lt;code&gt;init&lt;/code&gt; and &lt;code&gt;predict&lt;/code&gt;. The &lt;code&gt;init&lt;/code&gt; function will take the &lt;code&gt;PMML&lt;/code&gt; file as input and load it. The &lt;code&gt;predict&lt;/code&gt; function will take the input and return the output.&lt;/p&gt;

&lt;p&gt;The below code describes the structure of the &lt;code&gt;NIF&lt;/code&gt; functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extern "C" {
    #include &amp;lt;erl_nif.h&amp;gt;

    static ERL_NIF_TERM init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {}

    static ERL_NIF_TERM predict(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {}

    static ErlNifFunc nif_funcs[] = {
        {"init", 1, init},
        {"evaluate", 1, predict}
    };
}

ERL_NIF_INIT(lr_model, nif_funcs, NULL, NULL, NULL, NULL)

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  init function
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;init&lt;/code&gt; function will take the &lt;code&gt;PMML&lt;/code&gt; file as input and load it. We will use the &lt;code&gt;enif_inspect_binary&lt;/code&gt; function to get the &lt;code&gt;PMML&lt;/code&gt; file as a binary. Then we will convert it to a string and pass it to the &lt;code&gt;PmmlModelParser&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static ERL_NIF_TERM init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
    ErlNifBinary input_bin;
    if (!enif_inspect_binary(env, argv[0], &amp;amp;input_bin)) {
        return enif_make_badarg(env);
    }
    string input(reinterpret_cast&amp;lt;char*&amp;gt;(input_bin.data), input_bin.size);
    pmmlModelParser = new PmmlModelParser(input);
    return enif_make_atom(env, "ok");
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  predict function
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;predict&lt;/code&gt; function will take the input and return the output. The input will be a string and the output will be a string on the Erlang side.&lt;/p&gt;

&lt;p&gt;But on the &lt;code&gt;C/C++&lt;/code&gt; side, we will convert the input to a map and pass it to the &lt;code&gt;PmmlModelParser&lt;/code&gt; class because &lt;code&gt;cPMML&lt;/code&gt; library expects a map as input. Don't get intimidated by the code below, it's just converting the &lt;code&gt;Erlang&lt;/code&gt; map to a &lt;code&gt;C++&lt;/code&gt; map by iterating over all the keys and value pairs.&lt;/p&gt;

&lt;p&gt;For prediction, we will need a map with the key as &lt;code&gt;X&lt;/code&gt; and value as the input.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I could have just used a binary input as X and converted it to a string on the &lt;code&gt;C/C++&lt;/code&gt; side. But, to make this more general, I am converting the &lt;code&gt;Erlang&lt;/code&gt; map to a &lt;code&gt;C++&lt;/code&gt; map. Now this can be used for any PMML file and not just a specific one.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static ERL_NIF_TERM predict(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  unordered_map&amp;lt;std::string, std::string&amp;gt; cpp_map;

  if (!enif_is_map(env, argv[0]))
  {
    return enif_make_badarg(env);
  }

  ErlNifMapIterator iter;
  if (enif_map_iterator_create(env, argv[0], &amp;amp;iter, ERL_NIF_MAP_ITERATOR_FIRST))
  {
    do
    {
      ERL_NIF_TERM key, value;
      if (enif_map_iterator_get_pair(env, &amp;amp;iter, &amp;amp;key, &amp;amp;value))
      {
        ErlNifBinary key_bin, value_bin;
        if (enif_map_iterator_get_pair(env, &amp;amp;iter, &amp;amp;key, &amp;amp;value))
        {
          char key_str[64], value_str[64];
          if (enif_get_string(env, key, key_str, sizeof(key_str), ERL_NIF_LATIN1) &amp;amp;&amp;amp;
              enif_get_string(env, value, value_str, sizeof(value_str), ERL_NIF_LATIN1))
          {
            cpp_map[key_str] = value_str;
          }
        }
      }
    } while (enif_map_iterator_next(env, &amp;amp;iter));
    enif_map_iterator_destroy(env, &amp;amp;iter);
  }

  string ret = pmmlModelParser-&amp;gt;predict(cpp_map);
  return enif_make_string(env, ret.c_str(), ERL_NIF_LATIN1);
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Erlang code
&lt;/h3&gt;

&lt;p&gt;We will name our &lt;code&gt;Erlang&lt;/code&gt; module &lt;code&gt;lr_model.erl&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-module(lr_model).
-export([init/1, evaluate/1]).
-nifs([init/1, evaluate/1]).
-on_load(init/0).

init() -&amp;gt;
    ok = erlang:load_nif("./lr_model", 0).

init(PmmlFile) -&amp;gt;
    exit(problem_loading_nif).

evaluate(Input) -&amp;gt;
    exit(problem_loading_nif).

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Compiling and Running
&lt;/h3&gt;

&lt;p&gt;Compiling the &lt;code&gt;C/C++&lt;/code&gt; code will now take an extra parameter to include &lt;code&gt;cPMML&lt;/code&gt; library. As the model is predicting the values for &lt;code&gt;y = 2x + 1&lt;/code&gt;, we should get &lt;code&gt;~3&lt;/code&gt; for &lt;code&gt;x = 1&lt;/code&gt; and &lt;code&gt;~1&lt;/code&gt; for &lt;code&gt;x = 0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;g++ -std=c++11 library.cpp \
-o lr_model.so \
-lcPMML \
-I /usr/local/lib/erlang/erts-13.2.2.2/include/ \
-bundle \
-bundle_loader /usr/local/lib/erlang/erts-13.2.2.2/bin/beam.smp


&amp;gt; c(lr_model).
&amp;gt; lr_model:init(&amp;lt;&amp;lt;"./lr_model.pmml"&amp;gt;&amp;gt;).
&amp;gt; lr_model:evaluate(#{"X"=&amp;gt;"0"}).
"0.967265"
&amp;gt; lr_model:evaluate(#{"X"=&amp;gt;"1"}).
"3.007469"

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog, we saw how to use &lt;code&gt;C/C++&lt;/code&gt; libraries in &lt;code&gt;Erlang&lt;/code&gt; code. We used a PMML file of a linear regression model to predict the values of &lt;code&gt;y = 2x + 1&lt;/code&gt; for a given value of &lt;code&gt;x&lt;/code&gt; in &lt;code&gt;Erlang&lt;/code&gt; where the code was implemented in C++. We saw how to use the &lt;code&gt;cPMML&lt;/code&gt; library to parse the &lt;code&gt;PMML&lt;/code&gt; file and use it for predictions.&lt;/p&gt;

</description>
      <category>erlang</category>
      <category>cpp</category>
      <category>nif</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Running AI/ML predictions in CPP using cPMML library</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sun, 01 Oct 2023 23:39:47 +0000</pubDate>
      <link>https://forem.com/schwiftycold/running-aiml-predictions-in-cpp-using-cpmml-library-28oh</link>
      <guid>https://forem.com/schwiftycold/running-aiml-predictions-in-cpp-using-cpmml-library-28oh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;PMML&lt;/code&gt; is a markup language to save your AI/ML model files so that you can use them for predictions later on (maybe during production).&lt;code&gt;cPMML&lt;/code&gt; is a library created by the &lt;a href="https://github.com/AmadeusITGroup/cPMML" rel="noopener noreferrer"&gt;AmadeusITGroup&lt;/a&gt; to parse and run predictions in C++. In this blog, we will train a linear regression model in&lt;code&gt;python&lt;/code&gt; and generate a &lt;code&gt;pmml&lt;/code&gt; file and then we will run our predictions in &lt;code&gt;C++&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a model file
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Dependencies
&lt;/h3&gt;

&lt;p&gt;We will need &lt;code&gt;pandas&lt;/code&gt;, &lt;code&gt;numpy&lt;/code&gt;, &lt;code&gt;scikit-learn&lt;/code&gt; and &lt;code&gt;sklearn2pmml&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pandas numpy scikit-learn sklearn2pmml

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Imports
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn2pmml import sklearn2pmml
from sklearn2pmml.pipeline import PMMLPipeline
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  The model
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Dataset
&lt;/h4&gt;

&lt;p&gt;For keeping things simple, let's train a linear regression model to match the equation, &lt;code&gt;y = 2x + 1&lt;/code&gt;. We can generate a random dataset for this equation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;X = np.random.rand(100, 1)
Y = 2 * X + 1 + 0.1 * np.random.randn(100, 1)

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Test/Train data
&lt;/h4&gt;

&lt;p&gt;Next, we'll divide the data into test and train datasets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;df = pd.DataFrame({'X': X.flatten(), 'Y': Y.flatten()})
train_df, test_df = train_test_split(df, test_size=0.2, random_state=42)
X_train = train_df[['X']]
y_train = train_df['Y']
X_test = test_df[['X']]
y_test = test_df['Y']

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Training the model
&lt;/h4&gt;

&lt;p&gt;For training the model, we can get the model from scikit learn library and use the dataset we generated above. We can also check the &lt;code&gt;mse&lt;/code&gt; to get an idea of the model's accuracy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipeline = PMMLPipeline([
    ("regressor", LinearRegression())
])

pipeline.fit(X_train, y_train)

y_pred = pipeline.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Saving the pmml file
&lt;/h4&gt;

&lt;p&gt;If you are satisfied by the performance of your model, you can export the model as a pmml file. We will save the model with the name, &lt;code&gt;lr_model.pmml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sklearn2pmml(pipeline, "lr_model.pmml", with_repr = True)

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the model file
&lt;/h2&gt;

&lt;p&gt;The main step of focus in this blog is using the model in C++ program. For this, you will need to isntall the &lt;code&gt;cPMML&lt;/code&gt; library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing cPMML
&lt;/h3&gt;

&lt;p&gt;To install the libray in your system, you just need to run the below command. This will run &lt;code&gt;cmake&lt;/code&gt;, so you should have &lt;code&gt;cmake&lt;/code&gt; installed in your system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/AmadeusITGroup/cPMML.git &amp;amp;&amp;amp; cd cPMML &amp;amp;&amp;amp; ./install.sh

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  For Mac M1
&lt;/h4&gt;

&lt;p&gt;I ran into some problems while installing this on Mac M1. Here are the steps to install this effortlessly.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ensure you have the latest version of &lt;code&gt;cmake&lt;/code&gt; installed in your system.&lt;/li&gt;
&lt;li&gt;You can edit the &lt;code&gt;install.sh&lt;/code&gt; script to remove &lt;code&gt;-j 4&lt;/code&gt; flag from the &lt;code&gt;cmake -j 4 ..&lt;/code&gt; command. This will turn off the multi processing.&lt;/li&gt;
&lt;li&gt;The last line of the &lt;code&gt;install.sh&lt;/code&gt; script is &lt;code&gt;sudo ldconfig&lt;/code&gt;. Change this to &lt;code&gt;sudo update_dyld_shared_cache&lt;/code&gt;. This installs the &lt;code&gt;.dylib&lt;/code&gt; or &lt;code&gt;.so&lt;/code&gt; library files to proper destination.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Running the predictions
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Include the library
&lt;/h4&gt;

&lt;p&gt;The first thing is to import the library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include "cPMML.h"
#include &amp;lt;iostream&amp;gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Load the model
&lt;/h4&gt;

&lt;p&gt;Then you can load the model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int main() {
  cpmml::Model model("lr_model.pmml");
  return 0;
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Start predictions
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;cPMML&lt;/code&gt; library takes input as an unordered_map of strings. For us, there is only one input which is &lt;code&gt;X&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int main() {
  cpmml::Model model("lr_model.pmml");

  // This shoule yield a value close to 1
  std::unordered_map&amp;lt;std::string, std::string&amp;gt; input1 = {
    {"X", "0"}
  };

  // This should yield a value close to 21
  std::unordered_map&amp;lt;std::string, std::string&amp;gt; input2 = {
    {"X", "10"}
  };

  std::cout&amp;lt;&amp;lt;"X = 0 Y = "&amp;lt;&amp;lt;model.predict(input1)&amp;lt;&amp;lt;'\n';
  std::cout&amp;lt;&amp;lt;"X = 10 Y = "&amp;lt;&amp;lt;model.predict(input2)&amp;lt;&amp;lt;'\n';

  return 0;
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Compilation
&lt;/h4&gt;

&lt;p&gt;You can compile the code by including the &lt;code&gt;cPMML&lt;/code&gt; library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; g++ -std=c++11 predict.cpp -o predict.o -lcPMML
&amp;gt; ./predict.o
X = 0 Y = 0.967265
X = 10 Y = 21.369305

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog, we saw how to store your model as a &lt;code&gt;PMML&lt;/code&gt; file and load it in &lt;code&gt;C++&lt;/code&gt; using &lt;code&gt;cPMML&lt;/code&gt; library. You can view the code for the above &lt;a href="https://github.com/UnresolvedCold/prediction-pmml-using-cpmml-in-cpp.git" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>pmml</category>
      <category>python</category>
      <category>cpp</category>
      <category>cpmml</category>
    </item>
    <item>
      <title>Embedded Jetty server with handlers for legacy Java applications</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Thu, 07 Sep 2023 00:00:00 +0000</pubDate>
      <link>https://forem.com/schwiftycold/embedded-jetty-server-with-handlers-for-legacy-java-applications-i5l</link>
      <guid>https://forem.com/schwiftycold/embedded-jetty-server-with-handlers-for-legacy-java-applications-i5l</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://eclipse.dev/jetty/" rel="noopener noreferrer"&gt;Jetty&lt;/a&gt; is a very powerful yet lightweight &lt;code&gt;Java&lt;/code&gt; library since ages that helps you create servers and clients for HTTP (literally all the versions), Web Sockets, OSGI, JMX, JAAS and much more.&lt;/p&gt;

&lt;p&gt;This post will deal with embedding a Jetty server in a Java application and adding handlers for different requests. I'm assuming you have a basic understanding of Java and Maven and have familiarity with different HTTP methods like GET, POST, PUT, DELETE, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Jetty Server Architecture
&lt;/h2&gt;

&lt;p&gt;An incoming request is handled using 4 components - Threadpool, Connectors, Handlers and Server.&lt;code&gt;Server&lt;/code&gt; is the core of the system that handles the management of servers and the entire lifecycle of the server.&lt;code&gt;Connectors&lt;/code&gt; help in accepting various requests over different protocols like HTTP, HTTPS, etc.&lt;code&gt;Handlers&lt;/code&gt; are the components that process the incoming request.&lt;code&gt;Threadpools&lt;/code&gt; are like tiny workers that make the system multi-threaded and help in handling multiple requests at the same time.&lt;/p&gt;

&lt;p&gt;An incoming request is first accepted at the &lt;code&gt;Connector&lt;/code&gt;. Then it is sent to the &lt;code&gt;Server&lt;/code&gt; which connects it to the &lt;code&gt;Handler&lt;/code&gt; where the main response is generated. And the response is generated using a particular thread as per &lt;code&gt;Threadpool&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Jetty in a Java application
&lt;/h2&gt;

&lt;h3&gt;
  
  
  New Java App
&lt;/h3&gt;

&lt;p&gt;First, let's create a new &lt;code&gt;Java&lt;/code&gt; app using &lt;code&gt;Maven&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; mvn archetype:generate \
-DgroupId = com.schwiftycold.poc \
-DartifactId = poc_jetty_server \
-DarchetypeArtifactId = maven-archetype-quickstart \
-DinteractiveMode = false

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

&lt;/div&gt;



&lt;p&gt;Add the following properties to your &lt;code&gt;POM&lt;/code&gt; file. This will set the &lt;code&gt;Java&lt;/code&gt; version and language encoding for the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;properties&amp;gt;
    &amp;lt;project.build.sourceEncoding&amp;gt;UTF-8&amp;lt;/project.build.sourceEncoding&amp;gt;
    &amp;lt;maven.compiler.source&amp;gt;20&amp;lt;/maven.compiler.source&amp;gt;
    &amp;lt;maven.compiler.target&amp;gt;20&amp;lt;/maven.compiler.target&amp;gt;
  &amp;lt;/properties&amp;gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add Jetty dependency
&lt;/h3&gt;

&lt;p&gt;Add the following dependency in your &lt;code&gt;pom.xml&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependencies&amp;gt;
  &amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.eclipse.jetty&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;jetty-server&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;11.0.3&amp;lt;/version&amp;gt;
  &amp;lt;/dependency&amp;gt;
  ...
&amp;lt;/dependencies&amp;gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Main server
&lt;/h3&gt;

&lt;p&gt;The main component of the system is the server class. This class will create a server context and register connectors and handlers. Let's call this class &lt;code&gt;MainServer&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Threadpool
&lt;/h4&gt;

&lt;p&gt;There are different kinds of Threadpool offered by Jetty like &lt;code&gt;QueuedThreadPool&lt;/code&gt;, &lt;code&gt;ExecutorThreadPool&lt;/code&gt;, &lt;code&gt;ScheduledThreadPool&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;QueuedThreadPool&lt;/code&gt; maintains a fixed number of threads and a queue to manage incoming requests. When a request comes and the thread is busy then it is added to the queue and will be picked when a thread is available. And, due to its nature, this is widely used for handling &lt;code&gt;http&lt;/code&gt; requests.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ScheduledThreadPool&lt;/code&gt; extends &lt;code&gt;QueuedThreadPool&lt;/code&gt; to handle scheduled tasks. It provides us with a scheduler to execute tasks at a certain interval. It is generally used for scheduling background tasks.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ExecutorThreadPool&lt;/code&gt; enables you to use custom &lt;code&gt;Executor&lt;/code&gt; as the Threadpool for Jetty.&lt;/p&gt;

&lt;p&gt;Here, we will be using &lt;code&gt;QueuedThreadPool&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ThreadPool threadPool = new QueuedThreadPool();

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Server
&lt;/h4&gt;

&lt;p&gt;The server is the essential component that manages the connectors and handlers. It takes the Threadpool to create a new server instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server server = new Server(threadPool);

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Connector
&lt;/h4&gt;

&lt;p&gt;A connector allows us to accept a variety of different protocols like &lt;code&gt;HTTP&lt;/code&gt;, &lt;code&gt;HTTPS&lt;/code&gt;, &lt;code&gt;Unix domain socket&lt;/code&gt;, etc.&lt;/p&gt;

&lt;h5&gt;
  
  
  HTTP
&lt;/h5&gt;

&lt;p&gt;A simple &lt;code&gt;HTTP&lt;/code&gt; connector can be initialized using the &lt;code&gt;ServerConnector&lt;/code&gt; class. And you can change the port to listen to using, &lt;code&gt;setPort&lt;/code&gt; function. By default, the port is &lt;code&gt;8080&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ServerConnector connector = new ServerConnector(server);
connector.setPort(9120);
server.setConnectors(new Connector[]{connector});

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

&lt;/div&gt;



&lt;h5&gt;
  
  
  HTTPS
&lt;/h5&gt;

&lt;p&gt;For using &lt;code&gt;HTTPS&lt;/code&gt; you'll need to register the &lt;code&gt;SSL/TLS&lt;/code&gt; certificate. We won't be using this here but a quick look into this will give us a glance of the infinite possibilities.&lt;/p&gt;

&lt;p&gt;First, you will need the sslContextFactory which defines your &lt;code&gt;SSL/TLS&lt;/code&gt; configurations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath("/path/to/keystore.jks");
sslContextFactory.setKeyStorePassword("keystore-password");
sslContextFactory.setKeyManagerPassword("key-password");


ServerConnector httpsConnector = new ServerConnector(
    server,
    new SslConnectionFactory(sslContextFactory, "http/1.1")
);
httpsConnector.setPort(8443);
server.setConnectors(new Connector[] {httpsConnector});

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Handlers
&lt;/h4&gt;

&lt;p&gt;Handlers are the ones that will be creating or response. For this, let's create a new singleton class. Let's set this to &lt;code&gt;null&lt;/code&gt; for now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.setHandler(null);

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Final code
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MainServer {
  public static void startServer() {
    try {
      ThreadPool threadPool = new QueuedThreadPool();

      Server server = new Server(threadPool);

      ServerConnector connector = new ServerConnector(server);
      connector.setPort(9120);

      server.setConnectors(new Connector[]{connector});
      server.setHandler(null);

      server.start();
      server.join();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Handler class
&lt;/h3&gt;

&lt;p&gt;Handler class is where we will write our logic to process the request. So this requires a special attention.&lt;/p&gt;

&lt;p&gt;Let's create a new class that extends &lt;code&gt;AbstractHandler&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Extend AbstractHandler
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;AbstractHandler&lt;/code&gt; required us to implement a &lt;code&gt;handle&lt;/code&gt; function. This function is triggered on every new request.&lt;/p&gt;

&lt;p&gt;The handle method uses four parameters. The &lt;code&gt;target&lt;/code&gt; parameter denotes the endpoint that was triggered. If we trigger the URL, &lt;code&gt;http://localhost:9120/hi&lt;/code&gt; then the target will take the value as &lt;code&gt;/hi&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;baseRequest&lt;/code&gt; and &lt;code&gt;request&lt;/code&gt; denote the same thing but in a different context.&lt;code&gt;baseRequest&lt;/code&gt; is Jetty specific whereas &lt;code&gt;request&lt;/code&gt; donates the servlet-specific APIs. Here, we won't be using the servlet APIs.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;response&lt;/code&gt; is the processed result generated by the server which will be sent to the client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MainHandler extends AbstractHandler{

  @Override
  public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
        System.out.println("target: " + target);
        System.out.println("baseRequest: " + baseRequest);
        System.out.println("request: " + request);
        System.out.println("response: " + response);     
  }
}

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

&lt;/div&gt;



&lt;p&gt;The output of the above code would be something as follows on triggering with &lt;code&gt;curl&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; curl -X POST "http://localhost:9120/hi" -d "p=value"

target: /hi
baseRequest: Request(POST http://localhost:9120/hi)@57ffc3ca
request: Request(POST http://localhost:9120/hi)@57ffc3ca
response: HTTP/1.1 200

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

&lt;/div&gt;



&lt;p&gt;Here, you can see the target as &lt;code&gt;/hi&lt;/code&gt;, the request denotes the method call and reference to the request object and the response is just 200 which denotes a success. We can manipulate this information to create responses based on different inputs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Manage different methods
&lt;/h4&gt;

&lt;p&gt;We can get the request method using the response object by calling the function, &lt;code&gt;getMethod&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void handle(...) {
    if ("POST".equalsIgnoreCase(baseRequest.getMethod())) {
      System.out.println("POST request received");
    }
    else if ("GET".equalsIgnoreCase(baseRequest.getMethod())) {
      System.out.println("GET request received");
    }
}

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

&lt;/div&gt;



&lt;p&gt;Now, calling curl as above will print &lt;code&gt;POST request received&lt;/code&gt; on the server side.&lt;/p&gt;

&lt;h4&gt;
  
  
  Extract the query parameters
&lt;/h4&gt;

&lt;p&gt;The query parameter and its values can be extracted from the base request object by using the &lt;code&gt;getParameterNames&lt;/code&gt; method. Here, I'm parsing the values and storing them in a &lt;code&gt;Map&lt;/code&gt; for accessing them later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if ("POST".equalsIgnoreCase(baseRequest.getMethod())) {
  System.out.println("POST request received");
  Map&amp;lt;String, String&amp;gt; queryParams = new HashMap&amp;lt;&amp;gt;();

  for (Enumeration&amp;lt;String&amp;gt; e = request.getParameterNames(); e.hasMoreElements();) {
    String name = e.nextElement();
    String[] values = request.getParameterValues(name);
    for (String value : values) {
      queryParams.put(name, value);
    }
  }

  System.out.println("Query params: " + queryParams);
  ...

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

&lt;/div&gt;



&lt;p&gt;Now, you can see the below response on the server side.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST request received
Query params: {p=value}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Generating the result
&lt;/h4&gt;

&lt;p&gt;The last part is generating the result for specific triggers. This is done by using &lt;code&gt;getWriter&lt;/code&gt; function to write to the response. You can also set the status of the response using &lt;code&gt;setStatus&lt;/code&gt; method. You can create more &lt;code&gt;if&lt;/code&gt; statements for each endpoint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try {
  if (target.startsWith("/hi")) {
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().println("Hello World");
    baseRequest.setHandled(true);
  }
} catch (Exception e) {
  e.printStackTrace();
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Final handle method
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    if ("POST".equalsIgnoreCase(baseRequest.getMethod())) {
      System.out.println("POST request received");
      Map&amp;lt;String, String&amp;gt; queryParams = new HashMap&amp;lt;&amp;gt;();

      for (Enumeration&amp;lt;String&amp;gt; e = request.getParameterNames(); e.hasMoreElements();) {
        String name = e.nextElement();
        String[] values = request.getParameterValues(name);
        for (String value : values) {
          queryParams.put(name, value);
        }
      }

      System.out.println("Query params: " + queryParams);

      System.out.println("Target: " +target.startsWith("/hi"));

      try {
        if (target.startsWith("/hi")) {
          System.out.println("Target: " +target);
          response.setStatus(HttpServletResponse.SC_OK);
          response.getWriter().println("Hello World");
          baseRequest.setHandled(true);
        }
      }
      catch (Exception e) {
        e.printStackTrace();
      }

    } else if ("GET".equalsIgnoreCase(baseRequest.getMethod())) {
      System.out.println("GET request received");
    }
  }

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Connect Handler and MainServer
&lt;/h4&gt;

&lt;p&gt;Before testing your new server, you'll also need to add the handler which can be done using the server's &lt;code&gt;setHandler&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.setHandler(new MainHandler());

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

&lt;/div&gt;



&lt;p&gt;Now calling the &lt;code&gt;hi&lt;/code&gt; endpoint will return &lt;code&gt;Hello World&lt;/code&gt; as a response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; curl -X POST "http://localhost:9120/hi" -d "p=value"

Hello World

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we saw how to configure &lt;code&gt;Jetty&lt;/code&gt; server and set handlers to generate responses for a particular endpoint trigger. This looks like a long post but it revolves around just creating a 'Hello World' server using &lt;code&gt;Jetty&lt;/code&gt;. You can find the code for the above &lt;a href="https://github.com/UnresolvedCold/POC-Jetty-Server-Handlers" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>jetty</category>
      <category>java</category>
      <category>maven</category>
      <category>embedded</category>
    </item>
  </channel>
</rss>
