Posts for: #Java

How to call Spring MongoRepository from an ExecutorService

The problem

Recently I encountered an issue in code when I tried to call the Spring MongoRepository from a non-Spring-managed thread in a Java Spring Boot application. Here is a brief description of the issue.

The Repository interface

public interface ResponseRepository extends MongoRepository<Response, String> {

}

It’s a standard Spring Repository interface without any custom methods.

The calling class

@Component //A Spring Component
public class AnImaginaryComponent {

    @Autowired
    private ResponseRepository responseRepo;

    public void saveResponseInANewThread(Response r) {
        //Create an Executor service
        ExecutorService executor = Executors.newSingleThreadExecutor();

        //Submit a Runnable instance
        executor.submit(() -> responseRepo.save(r));
    }
}

When anImaginaryComponent.saveResponseInANewThread(r) is invoked, it never saves the r Response into the database. And the worst part is that it doesn’t throw any exceptions or print any log messages for the failure.

[Read more]

AMS: Data Provider Service - Generating performance metrics with Spring Boot Actuator

Introduction

This is the first article in a series of upcoming articles on designing and creating an Application Monitoring System. 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.

[Read more]

How to fix Eclipse IDE flickering issue on Debian

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:

First check the value of GTK_IM_MODULE in your environment by executing

echo $GTK_IM_MODULE

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.

[Read more]

Higher Order Functions in Java?

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.

def adder(a):
 def add(b):
  return a + b
 return add
add_with_5 = adder(5)
final_value = add_with_5(1)

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.

[Read more]

Correct Way of Using EntityManager in Singleton EJB

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.

[Read more]