<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Java on Subhadip's Blog</title><link>https://www.subhadig.net/tags/java/</link><description>Recent content in Java on Subhadip's Blog</description><generator>Hugo</generator><language>en-us</language><copyright>Subhadip Ghosh</copyright><lastBuildDate>Wed, 29 Apr 2020 00:00:00 +0000</lastBuildDate><atom:link href="https://www.subhadig.net/tags/java/index.xml" rel="self" type="application/rss+xml"/><item><title>How to call Spring MongoRepository from an ExecutorService</title><link>https://www.subhadig.net/posts/how-to-call-spring-mongorepository-from-an-executorservice/</link><pubDate>Wed, 29 Apr 2020 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/how-to-call-spring-mongorepository-from-an-executorservice/</guid><description>&lt;h3 id="the-problem"&gt;The problem&lt;/h3&gt;
&lt;p&gt;Recently I encountered an issue in code when I tried to call the
&lt;a href="https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/repository/MongoRepository.html"&gt;Spring MongoRepository&lt;/a&gt;
from a non-Spring-managed thread in a Java &lt;em&gt;Spring Boot&lt;/em&gt; application. Here is a
brief description of the issue.&lt;/p&gt;
&lt;h4 id="the-repository-interface"&gt;The Repository interface&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ResponseRepository&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;extends&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;MongoRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It&amp;rsquo;s a standard &lt;em&gt;Spring Repository&lt;/em&gt; interface without any custom methods.&lt;/p&gt;
&lt;h4 id="the-calling-class"&gt;The calling class&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;//A Spring Component&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AnImaginaryComponent&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nd"&gt;@Autowired&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;private&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ResponseRepository&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;responseRepo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;saveResponseInANewThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;//Create an Executor service&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ExecutorService&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Executors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newSingleThreadExecutor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;//Submit a Runnable instance&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;responseRepo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When &lt;code&gt;anImaginaryComponent.saveResponseInANewThread(r)&lt;/code&gt; is invoked, it
never saves the &lt;em&gt;r&lt;/em&gt; &lt;code&gt;Response&lt;/code&gt; into the database. And the worst part is that
it doesn&amp;rsquo;t throw any exceptions or print any log messages for the failure.&lt;/p&gt;</description></item><item><title>AMS: Data Provider Service - Generating performance metrics with Spring Boot Actuator</title><link>https://www.subhadig.net/posts/ams-data-provider-service-generating-performance-metrics-with-spring-boot-actuator/</link><pubDate>Sun, 15 Mar 2020 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/ams-data-provider-service-generating-performance-metrics-with-spring-boot-actuator/</guid><description>&lt;h3 id="introduction"&gt;Introduction&lt;/h3&gt;
&lt;p&gt;This is the first article in a series of upcoming articles on designing and
creating an &lt;a href="https://www.subhadig.net/tags/application-monitoring-service/"&gt;Application Monitoring System&lt;/a&gt;.
An Application Monitoring System (AMS) is a system that can monitor
performance of other running applications. I am building this application
component by component from scratch and I will publish it here on this blog.
In this post, I talk about how to create and run services that provide
application performance metrics using Java, Spring Boot and Docker.&lt;/p&gt;</description></item><item><title>How to fix Eclipse IDE flickering issue on Debian</title><link>https://www.subhadig.net/posts/how-to-fix-eclipse-ide-flickering-issue-on-debian/</link><pubDate>Fri, 25 Jan 2019 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/how-to-fix-eclipse-ide-flickering-issue-on-debian/</guid><description>&lt;p&gt;I am using Eclipse IDE (version 2018-12 at the time of writing) on Debian 9 Xfce and the issue with it is that the Eclipse editor windows would flicker around the edges. Sometimes so much so that it's impossible to type inside it. Here's how I fixed it:&lt;/p&gt;
&lt;p&gt;First check the value of GTK_IM_MODULE in your environment by executing&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$GTK_IM_MODULE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In my case the output was "xim". But Eclipse expect it to be "ibus". So enter the following command in a terminal session to set it to the value.&lt;/p&gt;</description></item><item><title>Higher Order Functions in Java?</title><link>https://www.subhadig.net/posts/higher-order-functions-in-java/</link><pubDate>Thu, 18 Jan 2018 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/higher-order-functions-in-java/</guid><description>&lt;p&gt;Higher order functions are those functions that can return functions as their results. The returned function can then be invoked in the same way as you would invoke a normal function. In programming languages like Python, where functional programming is treated as a first class citizen, you can easily define higher order function like this.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;add_with_5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;final_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add_with_5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Java on the other hand introduced functional programming in version 1.8. The support is still very primitive and it does not allow one to create higher order functions that return functions.&lt;/p&gt;</description></item><item><title>Correct Way of Using EntityManager in Singleton EJB</title><link>https://www.subhadig.net/posts/correct-way-of-using-entitymanager-in-singleton-ejb/</link><pubDate>Sat, 16 Sep 2017 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/correct-way-of-using-entitymanager-in-singleton-ejb/</guid><description>&lt;p&gt;This week and last week I spent a lot of time writing code to implement the persistence feature for our module with EJB 3 at work. This is the first time I was working with EJB so there was quite a bit of learnings involved. During this time, I noticed that even some of the most seasoned developers don't have a very clear idea about how the whole Persistence thing in JPA works under the hood. They would take some of the things for granted like if they use an EntityManager in their managed objects, something will auto-magically manage everything for them and in the process, did some rookie mistakes.&lt;/p&gt;</description></item><item><title>EntityManager with try-with-resource</title><link>https://www.subhadig.net/posts/entitymanager-with-try-with-resource/</link><pubDate>Mon, 11 Sep 2017 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/entitymanager-with-try-with-resource/</guid><description>&lt;p&gt;try-with-resource is the new feature introduced in Java 1.7 to automatically close a resource after using. It basically helps you to avoid a finally block at the end of the try-catch block to close any resource that needs closing like the BufferedReader. But there's a catch. You can not use any type of resource with a try-with-resource block, only those classes that extend java.lang.AutoCloseable can be used, an interface that has a single method:&lt;/p&gt;</description></item><item><title>Automated Package Installation and Configuration Script for Ubuntu</title><link>https://www.subhadig.net/posts/automated-package-installation-and-configuration-script-for-ubuntu/</link><pubDate>Thu, 29 Dec 2016 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/automated-package-installation-and-configuration-script-for-ubuntu/</guid><description>&lt;p&gt;In my last post, I discussed about how I created my first Amazon EC2 instance and ran our Java web server on it. One of the most important things that I have realized over the years is that if you work with a small group of developers, you ought to think about automating certain jobs like testing, deployment, setting up server etc. So that you can concentrate on the real work rather than spending valuable time in doing repetitive tasks. The benefit against spending extra time in writing scripts may not be prominent at first, but over time you will realize the gain in terms of person hours and headaches saved because of the automated scripts.&lt;/p&gt;</description></item><item><title>Running Java app server on AWS EC2 Ubuntu instance</title><link>https://www.subhadig.net/posts/running-java-app-server-on-aws-ec2-ubuntu-instance/</link><pubDate>Sat, 26 Nov 2016 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/running-java-app-server-on-aws-ec2-ubuntu-instance/</guid><description>&lt;p&gt;Today I will be writing about setting up an Ubuntu instance on the Amazon AWS cloud and running a Java web application on it. We are working on a hobby project that has its back-end written in Java. The app back-end was first hosted on &lt;a href="https://www.heroku.com/" target="_blank"&gt;Heroku&lt;/a&gt; but due to some issues, we decided to move it to Amazon AWS. I was given the responsibility to set up our server. This was the first time I had an opportunity to work with Amazon AWS. But it turned out to be quite fun.&lt;/p&gt;</description></item><item><title>[SOLVED] SVN/JavaHL version problem in Ubuntu 13.04 with Eclipse Indigo</title><link>https://www.subhadig.net/posts/solved-svnjavahl-version-problem-in-ubuntu-13-04/</link><pubDate>Sun, 13 Oct 2013 00:00:00 +0000</pubDate><guid>https://www.subhadig.net/posts/solved-svnjavahl-version-problem-in-ubuntu-13-04/</guid><description>&lt;p&gt;I upgraded my Ubuntu from 12.04. Eclipse 3.7 with Subclipse 1.6 was installed in 12.04. Now after upgrading to 13.04, after I start Eclipse and synchronize my project with the repository, it throws the error message: "Incompatible JavaHL library loaded. 1.6.x or later required."&lt;/p&gt;

&lt;p&gt;Possible reason: libsvn-java is needed to be installed in Ubuntu in order to make SVN work in Eclipse. The installed version in Ubuntu 12.04 was 1.6 as far as I remember. But after upgrading to 13.04, the libsvn-java version was also upgraded to 1.7. But the version of Subclipse that is compatible with libsvn-java 1.7 is Subclipse 1.8.&lt;/p&gt;</description></item></channel></rss>