Advertisement

Sri Lanka's First and Only Platform for Luxury Houses and Apartment for Sale, Rent

Monday, May 25, 2026

Writing better Singleton Pattern with Java 26 LazyConstant


Singleton is a widely used Design Pattern which guarantees a single instance of a given class is created within an application process. This is a very good design pattern for creating instances which have a very high cost for creation in terms of memory and cpu.

A typical Singleton class for a UserService will look like following;

Tuesday, June 15, 2021

Java 16 Features


Java is nearing its next Long Term Support (LTS) Release which is Java 17. Thus far it has released Java 16 General Availability (GA) Release which can be used in Production. There are some new features available in Java 16 which we are going to look at right now.

Record 

Record is simplified way to declare a class which is used only to represent data. This type of classes are defined as Data Transfer Objects (DTO) or Entities in ORM frameworks to represent Tables. The current way to declare this type of classes is to create a public class with properties and all argument constructor and define all getters/setters along with equals, hashCode and toString. This requires a lot of additional code which can be reduced using code generation libraries and plugins like Lombok yet it is still an hassle. 

Java 16 introduces record type to ease creation of such classes. 

 import java.util.*;  
 public class RecordTest {  
      public static void main(String... args) {  
           Student s1 = new Student(1, "Shazin", 1);  
           Student s2 = new Student(2, "Shahim", 2);  
           System.out.println("Student 1 : "+s1);  
           System.out.println("Student 2 : "+s2);  
           System.out.println("s1 == s2 : "+(s1 == s2));  
           System.out.println("s1.equals(s1) : "+(s1.equals(s1)));  
           System.out.println("s1.equals(s2) : "+(s1.equals(s2)));  
           record GraduateStudent(Student student, List<String> qualifications) {};  
           GraduateStudent gs1 = new GraduateStudent(s1, Arrays.asList("A/S", "BSc"));  
           System.out.println("GraduateStudent 1 : " + gs1);  
      }  
      private static record Student(Integer id, String name, Integer grade) {  
           public Student {  // All args constructor 
                if (grade < 0 || grade > 13) {  
                     throw new IllegalArgumentException("Grade must be between 1 and 13");  
                }  
           }  
      }  
 }  
The above example shows the use a Record in Java 16. The record Student is defined below as a static variable with attributes id, name and grade. The getters/setters with equals, hashCode and toString will be generated by default for this.

Once instantiated the a record object acts as a regular object. The only difference is that generated accessors/modifiers don't follow Java Beans convention (getter for name is name() not getName()) thus not a drop in replacement for Java Beans. Due to this reason record types are not yet supported by JSON Serializers and ORM frameworks but its a work in progress.

New methods in Stream interface

There are several new methods introduced in Stream interface which can be used to increase performance and reduce boilerplate code. One is the Stream.toList() method.

           List<Integer> evenNos = nos.stream().filter(i -> i % 2 == 0).toList(); //.collect(Collectors.toList())  
           System.out.println("Even numbers : "+evenNos);  
This method reduces the need to call collect method when there is need to aggregate a Stream values to a List.

Also there is a new method introduced which is Stream.mapMulti() which is a more imperative implementation of Stream.flatMap(). The practical use of this is a bit complex but this article by Nicolai Parlog explains it well. I suggest you read it.

Pattern Matching

Pattern Matching is a very useful addition to Java 16 which eliminates the need of casting after the use of instanceof operator.

 import java.util.*;  
 public class PatternMatchingTest {  
      public static void main(String... args) {  
           String name = "Shazin";  
           List<Integer> nos = Arrays.asList(1, 2, 3, 4, 5);  
           Map map = Collections.singletonMap("Key", "Value");  
           Long bigInt = 1l;  
           System.out.println("Name Length : " + length(name));  
           System.out.println("Nos Length : " + length(nos));  
           System.out.println("Map Length : " + length(map));  
           System.out.println("bigInt Length : " + length(bigInt));  
      }  
      private static int length(Object o) {  
           if (o instanceof String s) { // s variable of type String  
                return s.length();  
           } else if (o instanceof Collection c) { // c variable of type Collection  
                return c.size();  
           } else if (o instanceof Map m) { // m variable of type Map  
                return m.size();  
           } else { // o variable of type Object  
                return 0;  
           }  
      }  
 }  
This feature allows to use a local scoped variable name right after the use of instanceof operator which matches the type used and can be used without casting. This eliminates a lot of boilerplate code.


Sunday, April 18, 2021

Writing Cloud Native Spring Applications with Docker, GraalVM and Spring Native

Cloud Computing has taken the IT world by storm because of its versatility, low initial capital required to get started and low operational costs. Many new programming languages/platforms such as Go, Node JS, etc. have been tailormade to work on Cloud nodes 

But many Enterprise level Software applications still run on Java and have failed to harness full capabilities of the Cloud due to its high memory footprint and high latency due to its Just in Time (JIT) compilation. These contribute to slower start up times and cold starts.

In order to tackle problems Oracle Labs Team introduced a VM called GraalVM which enabled language agnostic method to produce Cloud Native applications using Java (not only Java) along with capabilities to integrate Spring as well.

In order to harness the capabilities of GraalVM and easy development Spring introduced a developer tool named Spring Native which enables to convert most of the Spring Applications to Docker Container Images with GraalVM image as base which converts the Spring Application to a Native application runs it.

This enables faster startup time, low latency and low memory usage. GraalVM achieves this by using its Ahead of Time (AOT) compilation and building capabilities. 

You can use this developer tool directly from Spring Initializr as below;


Using this tool will add a new plugin and a Spring Native dependency to the maven pom.xml of the Spring Application. This plugin is available for Gradle as well if you fancy that.

If you have a Simple Spring Application like below;
@SpringBootApplication
@RestController
@RequestMapping("/native")
public class SpringNativeDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringNativeDemoApplication.class, args);
}

@PostConstruct
public void init() {
System.out.println("Initialized...");
}

@GetMapping("/greet")
public String greet() {
return "Hello";
}


}
Which has an endpoint /native/greet you will see the dependency in the pom.xml following;

<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>${spring-native.version}</version>
</dependency>

And plugin under plugins;
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder:tiny</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>
You can build the Docker container using the following command;

./mvnw spring-boot:build-image

This will generate the native image with ahead of time compilation. The generated executable will have all the necessary classes and statically linked native code from the SDK. Substrate VM which is a docker image provided by GraalVM is used here as the base of the image.

 
 After a long build process which is one of the downsides of this approach for the time being. Following command be used to list down the newly built image in Docker.

./docker images | grep spring-native-demo

Then the image can be run using the following command to be listening at port 8088.

./docker run -p 8088:8080 spring-native-demo:0.0.1-SNAPSHOT

This will start up the Spring Application very quickly due to the Ahead of Time compilation capability of GraalVM. 

You can hit the endpoint using following command or by visiting the url in a browser;

curl localhost:8088/native/greet --silent

As of now not all Spring Eco System modules are supported in Spring Native. But it is a work in progress.

There are numerous samples provided by Spring Team on how to use Spring Native in with other Spring Modules. 

More details can be found at Spring Native documentation. You can find the Source code at Github.



 

 

Friday, November 20, 2020

Spring Data Elasticsearch GeoPoint Field in Index

Recently I was working on a Project which involved Spring Data Elasticsearch and needed to use geo_point type field in the Elasticsearch index for speedier location based searches. Spring Data Elasticsearch provides a data type by the name GeoPoint to achieve this and an annotation named @GeoPointField to mark such fields.

@Document(indexName = "properties", replicas = 0, refreshInterval = "-1")
public class Property implements Serializable {

...
@GeoPointField
private GeoPoint location;
...

 

But sadly for me creating a repository out for the above @Document and saving objects was keep on converting the GeoPoint field to a plain map of latitude and longitude but not as a geo_point field. 

    "location": {
      "properties": {
        "latitude": {
          "type": "string"
         },
        "longitude": {
          "type": "string"
        }
      }
    } 

This results in following error while querying using Geo Distance Query. 

QueryParsingException failed to find geo_point field

After some research I finally found out that in such cases we would need to manually create the index before calling Repository.save.

In a service or any other initializer class you need to do the following;

@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;

@PostConstruct
public void init() {
IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Property.class);
indexOperations.putMapping(indexOperations.createMapping());
indexOperations.refresh();
}

 This fixes the issue and creates the location field with type geo_point as expected.

Sunday, March 22, 2020

Corona Meter

Corona Meter



Corona (Covid19) has taken over the world and it also has made its way to my home country Sri Lanka. We are in a curfew till Tuesday (24/03/2020) and it is expected to extended. Our security forces, medical staff are doing there best but as of writing this the no of cases reported hasn't stopped completely and also it hasn't blown out of control either. So we can say we are safe for now as long as we maintain social distancing and good personal hygiene.



In a time like this, it is particularly important to keep track of the Corona virus outbreak situation. So to ease thing I made a small DIY project which uses an API provided by the official Sri Lankan Health Promotion Bureau to show vital statistics. This is a need of the hour as it makes easy for myself and my wife to keep track of things easily. This is a self contained IOT project which only requires a power source or battery which can input 5v. It will connect to WiFi router and pull data. It can be easily modified to pull and display any country's data with minor modifications so it will be useful for many.

More information on how this was developed can be found at https://github.com/shazin/corona-meter

Tuesday, October 30, 2018

Spring Security Authentication

Spring Security Authentication

Security is one of the most vital concerns for any organization. In this article, you will learn about authentication and how to integrate them easily with the Spring MVC application.

Authentication

One of the fundamental ways to secure a resource is to make sure that the caller is who they claim to be. This process of checking credentials and making sure that they are genuine is called authentication.
This article will delve into the technical capabilities of Spring Security, specifically authentication. To find the complete code for this article, go to https://github.com/PacktPublishing/Hands-On-Spring-Security-5-for-Reactive-Applications/tree/master/Chapter02.
The following diagram shows the fundamental process Spring Security uses to address this core security requirement. The figure is generic and can be used to explain all the various authentication methods that the framework supports:


Authentication architecture

Spring Security has a series of servlet filters (a filter chain). When a request reaches the server, it is intercepted by this series of filters (Step 1 in the preceding diagram).In the reactive world (with the new Spring WebFlux web application framework), filters are written quite differently fromtraditional filters (such as those used in the Spring MVC web application framework). Having said that, the fundamental mechanism remains the same for both.
The Servlet filter code execution in the filter chain keeps skipping until the right filter is reached. Once it reaches the right authentication filter based on the authentication mechanism used, it extracts the supplied credentials (most commonly a username and password) from the caller.
Using the supplied values (here, you have a username and password), the filter(UsernamePasswordAuthenticationFilter) creates an Authentication object (in the preceding diagram, UsernamePasswordAuthenticationToken is created withthe username and password supplied in Step 2). The Authentication object created in Step 2 is then used to call the authenticate method in the AuthenticationManager interface:
public interface AuthenticationManager {

    Authentication authenticate(Authentication authentication)

    throwsAuthenticationException;

}
The actual implementation is provided by ProviderManager, which has a list of configured AuthenticationProvider.
public interface AuthenticationProvider {

    Authentication authenticate(Authentication authentication)

    throwsAuthenticationException;

    boolean supports(Class authentication);

}
The request passes through various providers and, in due course, tries to authenticate the request. There are a number of AuthenticationProvider interfaces as part of Spring Security.
In the diagram above, AuthenticationProvider requires user details (some providers require this, but some don't), which are provided in UserDetailsService:
public interface UserDetailsService {

    UserDetailsloadUserByUsername(String username) throws UsernameNotFoundException;

}
UserDetailsService retrieves UserDetails (and implements the User interface) using the supplied username.
If all goes well, Spring Security creates a fully populated Authentication object (authenticate: true, granted authority list, and username), which will contain various necessary details. The Authentication object is stored in the SecurityContext object by the filter for future use.
The authenticate method in AuthenticationManager can return the following:
  • An Authentication object with authenticated=true, if Spring Security can validate the supplied usercredentials
  • An AuthenticationException, if Spring Security finds that the supplied user credentials are invalid
  • null, if Spring Security cannot decide whether it is true or false (confused state)

Setting up AuthenticationManager

There are a number of built-in AuthenticationManager in Spring Security that can be easily used in your application. Spring Security also has a number of helper classes, using which you can set up AuthenticationManager. One helper class is AuthenticationManagerBuilder.
Using this class, it’s quite easy to set up UserDetailsService against a database, in memory, in LDAP, and so on. If the need arises, you could also have your own custom UserDetailsService (maybe a custom single sign-on solution is already there in your organization).
You can make an AuthenticationManager global, so it will be accessible by your entire application. It will be available for method security and other WebSecurityConfigurerAdapter instances.
WebSecurityConfigurerAdapter is a class that is extended by your Spring configuration file, making it quite easy to bring Spring Security into your Spring application. This is how you set up a global AuthenticationManager using the @Autowired annotation:
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void confGlobalAuthManager(AuthenticationManagerBuilderauth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("admin")
            .password("admin@password")
            .roles("ROLE_ADMIN");

    }

}
You can also create local AuthenticationManager, which is only available for this particular WebSecurityConfigurerAdapter, by overriding the configure method, as shown in the following code:
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilderauth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("admin")
            .password("admin@password")
            .roles("ROLE_ADMIN");

    }

}
Another option is to expose the AuthenticationManager bean by overriding the authenticationManagerBean method:
@Override
publicAuthenticationManagerauthenticationManagerBean() throws Exception {

    returnsuper.authenticationManagerBean();

}
You can also expose various AuthenticationManager, AuthenticationProvider, or UserDetailsService as beans, which will override the default ones.
The preceding code example has used AuthenticationManagerBuilder to configure in-memory authentication.

AuthenticationProvider

AuthenticationProvider provides a mechanism for getting the user details with which authentication can be performed. Spring Security provides a number of AuthenticationProvider implementations, as shown in the following diagram:


Spring Security built-in AuthenticationProvider

Custom AuthenticationProvider

You can also write a custom AuthenticationProvider by implementing the AuthenticationProvider interface. You have to implement two methods, namely authenticate (Authentication) and supports(ClassaClass):
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException 
    {

        String username = authentication.getName();
        String password = authentication.getCredentials().toString();

        if ("user".equals(username) && "password".equals(password)) {
            return new UsernamePasswordAuthenticationToken(username, password, Collections.emptyList());
        } else {
            throw new BadCredentialsException("Authentication failed");
        }

    }

    @Override
    publicboolean supports(ClassaClass) {
         returna Class.equals(UsernamePasswordAuthenticationToken.class);

    }

}

Your authenticate method is quite simple. Compare the username and password with a static value. You can write any logic here and authenticate the user. If there is an error, it throws an exception, AuthenticationException.
On the GitHub page, navigate to the jetty-in-memory-basic-custom-authentication project to see the full source code of this class.

Multiple AuthenticationProvider

Spring Security allows you to declare multiple AuthenticationProvider in your application. They are executedaccording to the order in which they are declared in the configuration.
The jetty-in-memory-basic-custom-authentication project is modified further, and you have used the newly created CustomAuthenticationProvider as an AuthenticationProvider (Order 1) and the existing inMemoryAuthentication as your second AuthenticationProvider (Order 2):
@EnableWebSecurity
@ComponentScan(basePackageClasses = CustomAuthenticationProvider.class)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    CustomAuthenticationProvider customAuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic()
            .and()
            .authorizeRequests()
            .antMatchers("/**")
            .authenticated(); // Use Basic authentication

    }

    @Override
    protected void configure(AuthenticationManagerBuilderauth) throws Exception {
        // Custom authentication provider - Order 1
        auth.authenticationProvider(customAuthenticationProvider);

        // Built-in authentication provider - Order 2
        auth.inMemoryAuthentication()
            .withUser("admin")
            .password("{noop}admin@password")

        //{noop} makes sure that the password encoder doesn't do anything
            .roles("ADMIN") // Role of the user
            .and()
            .withUser("user")
            .password("{noop}user@password")
            .credentialsExpired(true)
            .accountExpired(true)
            .accountLocked(true)
            .roles("USER");

    }

}
Whenever the authenticate method executes without error, the controls return, and, thereafter, the configured AuthenticationProvider doesn't get executed.

If you found this article interesting, you can explore Hands-On Spring Security 5 for Reactive Applications to secure your Java applications by integrating the Spring Security framework in your code. Hands-On Spring Security 5 for Reactive Applications
will guide you in integrating add-ons that will add value to any Spring Security module.

Sunday, February 18, 2018

Using ThingsBoard, MQTT, Arduino to Monitor Plant Soil Moisture, Temperature and Humidity - Part 1

Problem

Recently I got a rose plant from one of my friends who got married. I have been having plants of all sorts at my home but as any busy guy I forget to water the plants. This is a problem I thought I need to address using technology.

Solution

After doing some research I got to know about a Platform called ThingsBoard which allows Devices to connect to a central server using HTTP, MQTT, CoAP protocols and send telemetry to visualize everything in a Dashboard. I quickly downloaded it, used my Ethernet Shield, DHT11 sensor and YL-69 Soil Moisture sensor which I had lying around to check the possibility of integrating everything together.


Implementation

I just connected YL-69 Soil Moisture sensor to Arduno Analog Pin A1 to read the sensor data and Digital Pin 7 to power it if and when required. I connected Digital Pin 6 to read the sensor data from DHT11 sensor. Finally I followed the ThingsBoard Installation guide [1] and Getting Started guide [2] to setup ThingsBoard server in my laptop.

Finally wrote the following Arduino Sketch to use Ethernet Shield to connect to the network and ThingsBoard via MQTT protocol to send data.

Arduino sketch can be found below;


Data was sent in following format.
{"temperature":29.00, "humidity":30.00, "moisture":10, "active": false}

And was visualized in Dashboard Widgets like below;



You can see a Demo below where the Blue Color Soil Moisture sensor reading go up and down based on inserting and removal of the YL-69 sensor probe and Temperature and Humidity going up when I exhale hot air into the DHT11 sensor.





Future Work

Future work would be to add ESP8266 wireless connectivity to the entire thing and using ThingsBoard rule to fire and send an email when soil moisture level goes down below a threshold.

References


  1. https://thingsboard.io/docs/user-guide/install/windows/
  2. https://thingsboard.io/docs/getting-started-guides/helloworld/
  3. https://create.arduino.cc/projecthub/nekhbet/using-the-yl-39-yl-69-soil-humidity-sensor-with-arduino-968268
  4. https://playground.arduino.cc/Main/DHT11Lib
  5. https://www.techtalks.lk/blog/2018/2/thingsboard-mqtt-arduino-part-1

Tuesday, January 30, 2018

Git Commit Information in Spring MVC

At time there is a need for developers the need to know which Git Commit from which Git Branch was used to build a release so that they can keep track of the versions.
Spring Boot Actuator is tailor made for this if you are working on Spring Boot but if you are like me and not using Spring Boot for a specific project but only using Spring MVC then this guide will be helpful for you.
First of all you need to add the spring-boot-actuator
 <dependency>  
     <groupid>org.springframework.boot</groupid>  
     <artifactid>spring-boot-actuator</artifactid>  
     <version>Version</version>  
 </dependency>  
And then instead of relying on Spring Boot Autoconfiguration, in your Java Annotation configuration you can do the following;
@Configuration
@Import({
        EndpointWebMvcAutoConfiguration.class,
        ManagementServerPropertiesAutoConfiguration.class,
        EndpointAutoConfiguration.class
})
And in order to generate the git.properties file you can use the Maven git-commit-id plugin and use it as below;
 <build>  
     <pluginmanagement>  
       <plugins>  
         <plugin>  
           <groupid>pl.project13.maven</groupid>  
           <artifactid>git-commit-id-plugin</artifactid>  
           <version>2.2.4</version>  
           <executions>  
             <execution>  
               <id>get-the-git-infos</id>  
               <goals>  
                 <goal>revision</goal>  
               </goals>  
               <phase>package</phase>  
             </execution>  
             <execution>  
               <id>validate-the-git-infos</id>  
               <goals>  
                 <goal>validateRevision</goal>  
               </goals>  
             </execution>  
           </executions>  
           <configuration>  
             <dotgitdirectory>${project.basedir}/../.git</dotgitdirectory>  
             <dateformat>yyyy-MM-dd'T'HH:mm:ssZ</dateformat>  
             <dateformattimezone>${user.timezone}</dateformattimezone>  
             <generategitpropertiesfile>true</generategitpropertiesfile>  
             <generategitpropertiesfilename>${project.build.outputDirectory}/git.properties</generategitpropertiesfilename>  
             <format>properties</format>  
             <skippoms>true</skippoms>  
             <injectallreactorprojects>false</injectallreactorprojects>  
             <failonnogitdirectory>true</failonnogitdirectory>  
             <failonunabletoextractrepoinfo>true</failonunabletoextractrepoinfo>  
             <commitidgenerationmode>flat</commitidgenerationmode>  
             <gitdescribe>  
               <skip>false</skip>  
               <always>true</always>  
               <dirty>-dirty</dirty>  
               <match>*</match>  
               <tags>false</tags>  
               <forcelongformat>false</forcelongformat>  
             </gitdescribe>  
             <validationproperties>  
               <validationproperty>  
                 <name>validating project version</name>  
                 <value>${project.version}</value>  
                 <shouldmatchto>&lt;![CDATA[^.*(?&lt;!-SNAPSHOT)$]]&gt;</shouldmatchto>  
               </validationproperty>  
             </validationproperties>  
           </configuration>  
          </plugin>  
        </plugins>  
      </pluginmanagement>  
   </build>  
And when packaging you can use the following command
$ mvn clean package git-commit-id:revision 
Now when you access http://://info endpoint you will see the git commit id, branch name, commit time etc. and you can customize the pom.xml git-commit-id plugin configuration to have more information.

Tuesday, December 26, 2017

Java 9 Migration Tips


Java 9 was released this year and it is a much awaited release of Java with a lot of interesting and exciting features. But Upgrading is a concern for any CTO or Infrastructure Team as it may break the existing functionality or create unforeseen issues.

But in a recent presentation, Bernard Traversat who is the Vice President of Development for the Java SE Platform at Oracle provided some tips on successful migration to Java 9. Bernard manages the development team responsible for the Java Language, JVM, Core libraries, Java UI toolkits, and Java deployment stack for Oracle JDK product. So who else is fit enough provide advise on Java 9 Migration than him.

Why Upgrading is required;

  • Upgrades include Security fixes    
  • Upgrades include Regressions fixes    
  • New features with releases
  • More performance    
  • More robustness    
  • Because the top or bottom tier was upgraded (OS’s, Hardware)    
  • Reduce cost of operation via uniformity

Why Upgrading is delayed;

  • Fear of something is going to go wrong.
  • It will cost money if something goes wrong. According InformationWeek IT Downtime Costs $26.5 Billion in Revenue.
  • It requires extra work which costs time and money.
Out of Programming Languages Java, Golang, Python, C# .NET, As per Bernard Only Java is Backward compatible and upgradable.

There are binary, source and runtime compability explicit managed in the Java platform.
Most of the APIs (java.*, java.*) undergo millions of tests to ensure compatibility and have specifications managed under the JCP (Java Community Process).
In order to update a specification of an API, rigorous JCP process is required.

Migration

The following things remain the same from Java x to Java 9

  • The class path 
  • Class loading 
  • Not forced to migrate to modules
  • sun.misc.Unsafe works
  • Most existing code should work
  • The IDEs, Maven, etc. all have support for JDK 9 already

Things to look out for when migrating from Java x to Java 9

  • You will have to upgrade the libraries and tools that you use    
  • Build or deployment needs to be adjusted If you use any of the components that are shared between Java SE and Java EE 
  • A small number of supported APIs have been removed    
  • A number of non-API features and tools have been removed        
  • Libraries that you use may show some warnings

Incompatibility issues you will face during or after migration;

No 1 issue will be a Third Party library you use in your older Java code is not compatible with Java 9 anymore. This is addressed as many open source projects and vendors are already changing their code base to Java 9 but in case one hasn't done already until they do your migration will have issues.

Apart from that following can also affect you and you must be aware;

1. JEP-260 Encapsulate most internal APIs - This means Internal APIs will no longer be accessible to developers. If your code uses an internal API then it needs to change.

2. JEP-223 New Version String Scheme - JDK 1.9.0_25 will turn into JDK 9.1.3. If your code contains any version string checking then it has to change.

3. JEP-220 Modular Runtime Images - No more rt.jar and JDK directory structure will change. If your code has any JDK directory checking then it needs to change.

4. Most sun.misc.* and sun.reflect.* are removed - If your code uses any of these packages then it needs to change. Ex:- you can no longer use sun.misc.Base64 class.

5. JEP-214 Remove Java 8 Deprecated GC Combinations - Following GC Combinations will now produce errors. You need to change your Java start up scripts if you use any.

DefNew + CMS       : -XX:-UseParNewGC -XX:+UseConcMarkSweepGC
ParNew + SerialOld : -XX:+UseParNewGC
ParNew + iCMS      : -Xincgc
ParNew + iCMS      : -XX:+CMSIncrementalMode -XX:+UseConcMarkSweepGC
DefNew + iCMS      : -XX:+CMSIncrementalMode -XX:+UseConcMarkSweepGC -XX:-UseParNewGC
CMS foreground     : -XX:+UseCMSCompactAtFullCollection
CMS foreground     : -XX:+CMSFullGCsBeforeCompaction
CMS foreground     : -XX:+UseCMSCollectionPassing

6. Launching JVM with Unrecognized VM Options will result in failure to start

7. java.awt.peer and java.awt.dnd.peer packages will be hidden - If your code uses these it needs to be changed to use supporting APIs

8. Calling Thread.stop(Throwable) method will throw an Exception - This was deprecated for a long time now.

9. JEP-271 Unified GC Logging - Re-implemented GC Logging with the new JVM Logging framework JEP-158, If your Java start up scripts use any of the following they need to change.

-XX:+PrintGC will be changed to -Xlog:gc
-XX:+PrintGCDetails will be changed to -Xlog:gc*
-xloggc: will be changed to -Xlog:gc:

10. JEP-248 G1 is the new Garbage Collector - Serial GC is and will remain the default garbage collector for Windows 32 bit.

Conclusion

Migration to Java 9 is not as scary as it looks and will be easy with proper planning. Inorder to harness the full features it is advised to migrate
in small increments so that issues arising can also be mitigated easily.

References

https://www.techtalks.lk/blog/java-9-migration-tips

Friday, December 8, 2017

Writing Reactive Repositories for Spring Data with Mongodb

1. Overview

Reactive Programming has been alive for sometime now. Programming frameworks like Akka, Reactive Streams, Reactor, RxJava etc are good examples. In simple terms reactive programming is about writing non blocking software that are asynchronous and event driven.
Reactive Programming requires a small number of threads to scale vertically (Scale up inside a single JVM) instead of horizontally (Scale out to different nodes by means of clustering).
With Spring 5.0 there is out of the box support for Reactive Programming and now Spring Data project also has Reactive support. Now we will be looking at those latest features here in detail:

2. Setup

In order to use Spring Data Reactive Repositories we need to include spring-boot-starter-data-mongodb-reactive, de.flapdoodle.embed.mongo (for testing), rxjava and rxjava-reactive-streams. Plus reactive mongo-db driver is needed to make full use of the reactive capabilities. The maven dependencies will look like below:

Complete pom.xml file can be found at the Github repository listed at the conclusion section.

3. Enabling Spring Data Reactive Repositories for Mongodb

As the title suggests we will have a look the Spring Data Reactive Repositories with Mongodb. The new @EnableReactiveMongoRepositories is introduced to enable Reactive Repository support for Mongodb. The following configuration enables Spring Data Reactive Repositories for Mongodb:

4. Reactive Repositories

Spring Data project uses the repositories programming model which is the most high-level abstraction to deal with data. They’re comprised of a set of CRUD methods defined in a Spring Data provided interface and domain-specific query methods.
Mainly by using an interface named CrudRepository which exposes methods like findOne, delete, save. With Reactive Programming support Spring Data project has now introduced two more interfaces named ReactiveCrudRepository and RxJava2CrudRepository (for RxJava project support)
A typical Spring Data Reactive Repository would look like below:

And a RxJava2 version of the same Repository would look like below:


Note that Spring 5.0 Reactor Project specific Flux is returned in ReactiveTaxiRepository and RxJava project specific Flowable is returned in RxJava2TaxiRepository.
These repositories are really identical to standard Spring Data Repositories except for the fact that now they can return and/or accept as parameters, reactive elements such as Flux, Mono and Flowable.  By default, reactive repositories use Project Reactor types but other reactive libraries can also be used such as RxJava2 as shown above.

5. Using Spring Data Reactive Repositories for Mongodb

When using the new Spring Data Reactive Repositories, We can use the full features of Reactive Programming provided by the entities Flux, Mono or Flowable (RxJava2).
And now we look at Reactor version would like below:


And now we look at the RxJava2 version:

The above codes will find Taxis by Number CAL-4259 and collect that Flux stream or Flowable stream into a List and will block until the collection is finished.

6. Streaming Data with Tailable Cursor

Spring Data Reactive Repositories provide a way to Stream Data as it arrives into Mongodb with a @Tailable annotation, sort of like an Event Source system. Sticking to our example of Taxis, we can Subscribe to a Tailable Stream and while being subscribed, insert Taxi entities into Mongodb.
This will enable to see newly added Taxi entities coming into system in real time in a streaming manner until the subscription is disposed of. Simulating Taxis entering into a City in real time:


7. Advantages

Compared to a Standard Spring Data Repository, A Reactive Repository provides all the features of Reactive Programming to Data Retrieval. Just by using Reactive Repositories, we can easily filter, process, aggregate data returned declaratively and use asynchronous capabilities provided out of the box in Reactive Programming.

8. Conclusion

Reactive Programming provides a lot of features such Functional, Declarative style of Coding which is being rapidly adopted by developers and enables to write scale-able, easy to understand code.
Now with Spring Data Reactive Repositories these features can be easily incorporated into the existing features of Spring Data project. The complete Source code for the project can be found at GitHub .

Tuesday, October 3, 2017

Java + Spring Boot implementation of Blockchain

Blockchain is the buzz word these days as it is technology used by Cryptocurrencies which is taking over the world like crazy. There are so many articles written on the Theory of Blockchain like this one but there are very little implementations. But recently I stumbled upon an article by Daniel van Flymen who happens to have written a Python based implementation of the Blockchain.

According to him and I am pretty sure many Software Engineers would agree that the best way to learn something is to actually implement it so that you learn more than you get to learn while reading theory documentation.

So this inspired me to write Java + Spring Boot based Implementation of Daniel's work so that I can learn the Blockchain workings. For everyone who is interested following is a basic version of how a Blockchain will look like in the wild.


So Daniel talks a lot about the details of the Blockchain which you can read but in short following are the Steps required to write a Blockchain implementation.


  1. Building a Blockchain
  2. Building an API to access the Blockchain
  3. Interacting with the Blockchain 
  4. Consensus
You can see all above Steps implemented in Java + Spring Boot at https://github.com/shazin/block-chain


Monday, September 18, 2017

Server Sent Event Processing in Spring MVC 4.2

In a previous post I spoke about the features Spring MVC 4.2 has from Streaming Request Processing. I have discussed how we can StreamingResponseBody to send large data asynchronously in a streaming manner. 

In this post I am planning to talk about SseEmitter which is another way to send semi structured data clients in an asynchronous streaming manner.

If you want to learn more about the definition and structure of a Server Sent Event you can read the Mozilla Documentation. But in short a Server Sent Event is a push event from a server which will be taking place inside of a single TCP/IP Socket connection from Client to Server. This event being push means it eliminates the need for constant polling of the Server for data thus reduces unwanted load on Server and the Client can receive the data in real time.

In Spring MVC following controller code can be written to send server sent events easily and in the client end Javascript can be used to read these events and present in the web page. For demo purpose I am showing a live Cricket match score and commentary coming down to a Web page client in real time. The server code will look like below; 



And Javascript client client code will look like below;


And as you can see from the image below there is only one request being initiated to /score end point but multiple commentary data being received through that long lived connection.


Saturday, September 9, 2017

Spring Web Flux - The Non Blocking Asynchronous Functional Reactive Web Framework - 2

Spring Web Flux Framework can be used with its latest Release Candidate Release 5.0.0.RC3 if you want to try it out. You just need to add the Spring Milestone Repository in your gradle or maven file.


I have written the following service which emulates a delay of maximum 1000 milliseconds in a service to test how the conventional Spring Web MVC vs Spring Web Flux work.


And the following controller implementations one with a Blocking conventional Spring Web MVC Controller method which returns a list to get people and a Non Blocking Spring Web Flux Controller method which returns a Fluxwhich is a defferred result and processed differently.


And Tested sending 1000 concurrent requests to both methods using a Gatling test. The Application was running inside of a Tomcat Container.

Spring Web MVC results were


And Spring Web Flux results were


If you compare the 99th Percentile, Max and Mean response times of the both results you can see Spring Web Flux is considerably faster without any code changes to improve performance. This is a promising sign. The numbers can be improved even more if we use the Netty Reactive Servers instead of conventional Tomcat servers in my understanding.



Spring Web Flux - The Non Blocking Asynchronous Functional Reactive Web Framework - 1

Spring has a very diverse eco system of projects from Batch Jobs to Web Security. The best part of Spring project is that they are always ahead of time and do innovative work which sometimes are adopted by the Java Platform itself. For Example the @Autowired Annotation introduced by Spring was later standardized in JSR 330 as @Inject

Spring 5 is something they are working on right now (Have released 5.0.0 Release Candidate 3 Version as of writing this post) and has a lot of innovative features. Following are a list of those features;
  • Reactive programming: introducing our Spring WebFlux framework built on Reactor 3.1, with support for RxJava 1.3 & 2.1 and running on Tomcat, Jetty, Netty or Undertow.
  • Functional style with Java 8 & Kotlin: several API refinements and Kotlin extensions across the framework, in particular for bean registration and functional web endpoints.
  • Integration with Java EE 8 APIs: support for Servlet 4.0, Bean Validation 2.0, JPA 2.2, as well as the JSON Binding API (as an alternative to Jackson/Gson in Spring MVC).
  • Ready for JDK 9: fully aligned with JDK 9 at runtime, on the classpath as well as the module path (on the latter: as filename-based “automatic modules” for the time being).
One of most interesting is the introduction of Spring Web Flux - A Non Blocking Functional Reactive Web Framework. Reactive Programming has been alive for sometime in form of RxJava project and many more programming frameworks like Akka, NodeJS etc.

In simple terms reactive programming is about writing non blocking software that are asynchronous and event driven which require a small number of threads to scale vertically (Scale up inside a single JVM) instead of horizontally (Scale out to different nodes by means of clustering). 

If we take a look at the Spring Web MVC framework which was a Synchronous Blocking Framework based on Servlet (Prior to Servlet 3.x). Every Request Reaching a Spring Web MVC Controller would use the Request Thread that came a long with the Client Request to cater that request and will be blocking, Which meant that the scale-ability was bound to the maximum number of Request Threads a Web Container had. 

This was partially solved by the introduction of Servlet 3.x with asynchronous servlets where now a Request Thread would delegate the task of catering the request to a Separate Thread. Spring Web MVC framework allowed to return a DefferedResult from a Controller method which meant that the Request thread will not be blocking until the processing of the request by the Controller method.



Spring Web Flux uses a completely new approach of using Reactive Streams instead of the Servlet API. A key aspect of reactive is that it won't overwhelm the consumers when producers produce at a rate which is faster than the consumers can consume. This concept is called back pressure.


Reactive Programming will not let this happen (Image Courtesy : iStock)


References

  1. https://docs.spring.io/spring-framework/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/web-reactive.html
  2. https://spring.io/blog/2017/05/08/spring-framework-5-0-goes-rc1

Monday, August 28, 2017

Java 9 : The Future 3

Why is a module path better than a class path? 

Class path 

  • Class path can get in trouble when multiple classpath elements contain the same package 
  • Class path is searched (linearly) every time a new class is requested 

Module Path 

  • Modules form a partition of packages 
    • no package can be in more than one runtime module
  • Modules perform a directed search 
    • Once the runtime finds a module,it remembers its packages 
    • Never has to search for those packages again 
    • Class loading becomes O(1), not O(n)

So now instead Dependency List as with Classpath, we have a Module Dependency Graph which is Directed Graph on Dependencies. 

Following is the Dependency Graph for java.se module which is the Entire JVM Utilities available out of the box.


Breaking a single rt.jar into modules which can be defined in the graph shown above was the reason why Java 9 release took longer than other Java releases. It must have been very difficult task for the Engineering team and hats off to them.

So what about Backward Compatibility??

All Old Codes which are not modularized (available in Classpath) will be put into a one big module called the "Unnamed Module". That module will by default
  • Requires every other module
  • Exports / opens every package available in that module 
  • Maximum compatibility with classpath behavior 
This sort of behavior allows Java code to incrementally modularized when and if required. 



Tuesday, August 8, 2017

Java 9 : The Future 2

This is a continuation of Java 9 : Future 1 Post

Modules enforce Strong Encapsulation as we discussed in the previous post. Now a Package is not public by default to all other modules unless it is explicitly exported in the module-info.java Module Descriptor.

Can Class D from Module A access Class F from Module B?


With the Module Descriptors mentioned below, yes it is possible as Module A has mentioned as it requires Module B and Module B has exports package Z.

Strong Encapsulation

  • Module must require other Modules to be used in them.
  • Module exports packages to specific Modules or to all.
  • Module opens packages explicitly for reflection.
  • Accessibility is enforced by compiler, JVM and Reflection.

But why Modules again?

Remember packages starting with sun.* or com.* that were available in rt.jar (JVM Runtime) which were only specific for certain JVM Vendor Implementations and not all. Java programmers have been constantly warned not to use these packages as they will break Platform Independence. But up until Java 9 Modules there was no way to completely stop developers from using those packages.

Enter java.base Module

Following is the Module Descriptor for java.base module. With this as you can see only certain packages are exported and com.*/sun.* packages are only available within the module.

// module-info.java
module java.base { 
      exports java.lang;  
      exports java.io;  
      exports java.net;  
      exports java.util; 
}

Every Module in Java 9 implicitly will have require to java.base. Just like every class in Java will automatically import java.lang.*. 

So what does this means?

This means that now there can be a Java Application which is 100% modular with all Jar files being modules. Which means we can't use classpath in those scenarios. 

Enter Module Path

  • Path containing directories which contain modules
  • Modules can be exploded directories, or modular JARs (other forms too) 
  • The runtime searches the module path when looking for a module.
Plus a Java Application can be an hybrid of Modular as well as Backward Compatible Application in those cases there will be both a Module Path and Class Path.

To be continued..






Wednesday, August 2, 2017

Java 9 : The Future 1

There is much so talk and hype about Java 9 and its features. There are so many posts about Java 9 and specially on Java 9 Jigshaw or Modules. As a Java Developer for almost a decade now, I was really cautious and concerned about the new features of Java 9 and wanted to learn about it in depth. What better way than learning from the creators of Java 9 itself. Recently I got the chance to view an InfoQ video on a Presentation done by Karen Kinnear who is the Technical Lead for the Hotspot Java Virtual Machine Runtime team at Oracle. This lead me to realize Java 9 is far beyond Modules and the in depth nature of the Modules and the necessity for such time and resource consuming change in the Java Language Framework and Java Virtual Machine.

To start off if we look at all the features available in Java 9, we can list the followings;

102: Process API Updates
110: HTTP 2 Client
143: Improve Contended Locking
158: Unified JVM Logging
165: Compiler Control
193: Variable Handles
197: Segmented Code Cache
199: Smart Java Compilation, Phase Two
200: The Modular JDK
201: Modular Source Code
211: Elide Deprecation Warnings on Import Statements
212: Resolve Lint and Doclint Warnings
213: Milling Project Coin
214: Remove GC Combinations Deprecated in JDK 8
215: Tiered Attribution for javac
216: Process Import Statements Correctly
217: Annotations Pipeline 2.0
219: Datagram Transport Layer Security (DTLS)
220: Modular Run-Time Images
221: Simplified Doclet API
222: jshell: The Java Shell (Read-Eval-Print Loop)
223: New Version-String Scheme
224: HTML5 Javadoc
225: Javadoc Search
226: UTF-8 Property Files
227: Unicode 7.0
228: Add More Diagnostic Commands
229: Create PKCS12 Keystores by Default
231: Remove Launch-Time JRE Version Selection
232: Improve Secure Application Performance
233: Generate Run-Time Compiler Tests Automatically
235: Test Class-File Attributes Generated by javac
236: Parser API for Nashorn
237: Linux/AArch64 Port
238: Multi-Release JAR Files
240: Remove the JVM TI hprof Agent
241: Remove the jhat Tool
243: Java-Level JVM Compiler Interface
244: TLS Application-Layer Protocol Negotiation Extension
245: Validate JVM Command-Line Flag Arguments
246: Leverage CPU Instructions for GHASH and RSA
247: Compile for Older Platform Versions
248: Make G1 the Default Garbage Collector
249: OCSP Stapling for TLS
250: Store Interned Strings in CDS Archives
251: Multi-Resolution Images
252: Use CLDR Locale Data by Default
253: Prepare JavaFX UI Controls & CSS APIs for Modularization
254: Compact Strings
255: Merge Selected Xerces 2.11.0 Updates into JAXP
256: BeanInfo Annotations
257: Update JavaFX/Media to Newer Version of GStreamer
258: HarfBuzz Font-Layout Engine
259: Stack-Walking API
260: Encapsulate Most Internal APIs
261: Module System
262: TIFF Image I/O
263: HiDPI Graphics on Windows and Linux
264: Platform Logging API and Service
265: Marlin Graphics Renderer
266: More Concurrency Updates
267: Unicode 8.0
268: XML Catalogs
269: Convenience Factory Methods for Collections
270: Reserved Stack Areas for Critical Sections
271: Unified GC Logging
272: Platform-Specific Desktop Features
273: DRBG-Based SecureRandom Implementations
274: Enhanced Method Handles

Modular JDK feature is just a small part of it but is a significant one, in this part I am planning to talk about it in depth with some coding examples;

The Why of Modular JDK?

The main reason for a Modular JDK was to avoid Classpath Hell. People who have experience Deploying Java Applications in Production or Pre Production Environments would agree that creating and maintaining the Classpath for a Java Application with the focus to enable future maintenance is a Nightmare and can become a Hell very soon if not done properly. 

OSGi Framework tried to address this by creating an Engine in which Classloader Isolated Bundles can run with Strict Contracts to Export and Import Dependency. This was a good approach to solve Classpath hell but the problem was an OSGi Bundle is a Jar on Steroids (Had to have a META-INF/MANIFEST.MF Bundle Descriptor). So Normal Jar Files had to be converted to a OSGi Bundle before those can be used inside an OSGi Engine. Making OSGi Bundles of already existing millions of Jars and maintaining them through latest versions in its entirety could be classified as Hell. So OSGi didn't catch up to its fullest potential. 

Plus JVM Classpath scanning was Sequential where when a Class is requested by an Application running in JVM it will scan the Classpath Jars one after the other until it finds the requested Class. This is an O(n) problem which needed to be addressed to improve performance.

The Official Java Team had to come up with something off the shelf. This is the reason Why Modular JDK was created with all the difficulties.

The How of the Modular JDK?

Up until Java 8, there were only 2 concepts of code containers; 

1. Package (Public to all)
2. Class inside Package (default or package private, Public to all)

From Java 9 there are 3 concepts of code containers;

1. Module (A module is group of packages with a Module Descriptor)
2. Package inside Module (Public to All modules which can access the module contain the package)
3. Class inside Package (Public within module, Public to certain modules, Public to all modules)


How to create a Module in Java 9?

A module is a same old Java Jar file with a Module Descriptor. A Module Descriptor is a Java Source file with the name module-info.java residing in the root of the source directory. 

The Structure of the Module Descriptor


References