Skip to main content

Posts

Showing posts from October, 2019

How to read a large file in Java

Problem with reading large file Large file can be any plain text or binary file which is huge in size and can not fit in JVM memory at once. For example if a java application allocated with 256 MB memory and it tries to load a file completely which is more or close to that memory in size then it may throw out of memory error. Points to be remembered Never read the whole file at once. Read file line by line or in chunks, like reading few lines from text file or reading few bytes from binary file. Do not store the whole data in memory, like reading all lines and keeping as string.  Java has many ways to read the file in chunks or line by line, like BufferedReader, Scanner, BufferedInputStream, NIO API. We will use NIO API to read the file and Java stream to process it. We will also see how to span the processing with multiple threads to make the processing faster. CSV file In this example I am going to read a CSV file which is around 500 MB in size.  Sample is as...

Spring boot application events

Spring boot provides an easy and quick way to implement the event listeners. Spring boot generates multiple events during it's application lifecycle. We will see the below events which are provided by spring application under "org.springframework.boot.context.event" Java package. Thanks to spring that we don't need to focus on writing the listeners but the business logics which we want to execute during a specific event and to do that we just need to annotate our business method with @EventListener. ApplicationContextInitializedEvent Event published when a SpringApplication is starting up and the ApplicationContext is prepared and ApplicationContextInitializers have been called but before any bean definitions are loaded. Implementation and code example ApplicationEnvironmentPreparedEvent Event published when a SpringApplication is starting up and the Environment is first available for inspection and modification. Implementation and code example ...

ApplicationStartedEvent and ApplicationReadyEvent code example

Both ApplicationStartedEvent and ApplicationReadyEvent executes once service is started but there is little difference that ApplicationReadyEvent executes when application is ready to serve the request. Will see the implementation for both in below code. Since both are starting after application is up, we can use annotation based listener in this case. We will see the implementation In two different ways. Using Annotation To use annotation based listener we can define below code in any spring managed bean, like Component, Configuration etc. @EventListener(ApplicationReadyEvent.class) public void applicationReadyEvent() { System.out.println("Executing ApplicationReadyEvent..."); } @EventListener(ApplicationStartedEvent.class) public void applicationStartedEvent() { System.out.println("Executing ApplicationStartedEvent..."); } Registering with SpringApplication Below code shows the event listener creation with SpringApplication. @SpringBootApplicat...

ApplicationFailedEvent code example

ApplicationFailedEvent executes when application is failed to start. For example if application port is already in use or any other error during start. We can use this event listener to email the error or executing any script upon failure. In this example we will try to run the application twice to generate the failed event. SpringBootTutorialApplication @SpringBootApplication public class SpringBootTutorialApplication{ public static void main(String[] args) { SpringApplication app = new SpringApplication(SpringBootTutorialApplication.class); //register ApplicationFailedEvent event app.addListeners((ApplicationFailedEvent event)->{ System.out.println("Executing ApplicationFailedEvent..."); }); //start the application app.run(args); } } Try to run the application twice on the same port so in second attempt it will generate the error during start as one instance is already running on that port. Output Failed event execution statement is highligh...

ApplicationStartingEvent and ApplicationEnvironmentPreparedEvent code example

Both ApplicationStartingEvent and ApplicationEnvironmentPreparedEvent are executed before the application start and may be useful to modify or read the application or environment at runtime such as profiles. SpringBootTutorialApplication.java @SpringBootApplication public class SpringBootTutorialApplication implements AsyncConfigurer{ public static void main(String[] args) { SpringApplication app = new SpringApplication(SpringBootTutorialApplication.class); //register ApplicationEnvironmentPreparedEvent app.addListeners((ApplicationEnvironmentPreparedEvent event)->{ System.out.println("Executing ApplicationEnvironmentPreparedEvent..."); }); //register ApplicationStartingEvent app.addListeners((ApplicationStartingEvent event)->{ System.out.println("Executing ApplicationStartingEvent..."); }); //start the application app.run(args); } } Output Below output shows that both the events are executed before application start. Exe...

ApplicationContextInitializedEvent Code Example

ApplicationContextInitializedEvent provides you access to ApplicationContext and SpringApplication using which you can do many things like adding listeners at runtime, invoking events etc. In below code we will see how to register this event and fail the application if it finds the value true for given property. SpringBootTutorialApplication.java public class SpringBootTutorialApplication{ public static void main(String[] args) { SpringApplication app = new SpringApplication(SpringBootTutorialApplication.class); //register ApplicationContextInitializedEvent event app.addListeners((ApplicationContextInitializedEvent event)->{ System.out.println("Executing ApplicationContextInitializedEvent..."); //get the property value String shouldFailed = event.getApplicationContext() .getEnvironment().getProperty("should.app.failed"); if("true".equals(shouldFailed)) { //invoke failed event event.getApplicationContext() .pu...

Entity to DTO conversion in Java using Jackson

It's very common to have the DTO class for a given entity in any application. When persisting data, we use entity objects and when we need to provide the data to end user/application we use DTO class. Due to this we may need to have similar properties on DTO class as we have in our Entity class and to share the data we populate DTO objects using entity objects. To do this we may need to call getter on entity and then setter on DTO for the same data which increases number of code line. Also if number of DTOs are high then we need to write lot of code to just get and set the values or vice-versa. To overcome this problem we are going to use Jackson API and will see how to do it with minimal code only. Maven dependency <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> Entity class Below is ...

File upload using Spring boot rest service with unit testing

We will create a REST service using spring boot which will have an endpoint to upload the file. Then we create JUnit test for upload service using mocking. Maven dependency <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> REST Service for file upload Below service reads the content of uploaded file and print on the console. Client application will upload the file using given endpoint ...

How to convert java object to Map or Properties

Sometimes, we may need to convert the java objet to key-value pairs, for example Map or Properties class. It can be doable in multiple ways like using reflection or jackson library. We are going to use jackson library to achieve it here. Maven dependency <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> Employee.java We will use this class to convert to map and properties class. class Employee{ private long id; private String name; private Integer age; public Employee(long id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } //getter methods //setter methods } Generic method to convert the object Jackson library provides ObjectMapper class which provides "convertValue" method to convert the java objects. Below is the co...

Asynchronous REST service implementation in Spring boot

In this tutorial we will see how to create an asynchronous REST service endpoint using Spring boot application. Asynchronous service works in a way that it will not block the client request and do the processing in separate thread. When work is complete the response returned to the client so our service will be able to handle more client requests at the same time, compare to synchronous processing model. Let's understand how it is working in synchronous mode. In such server/client application at server side it has a pool of threads which are serving the request. If a request received by a thread then it will be blocked until it send the response back to client. In this case if processing doesn't take much time it will be able to process it quickly and accept other client requests but there could be one situation when all threads are busy and not able to accept the new client requests. To overcome of such problems, asynchronous processing model introduced for REST service...

Asynchronous execution using CompletableFuture in java

CompletableFuture CompletableFuture was introduced in Java 8 to support the asynchronous execution and avoid blocking calls. It implements Future and CompletionStage interfaces. Future can be used to retrieve the value and status of current task while CompletionStage provides multiple methods to support the event based task execution which helps in creating a chain or pipeline for the actions to happen during specified events. Runnable Runnable interface has a void run() method where we can write the logic which we want to execute but it can not return any result. Runnable can be executed using Thread or ExecutorService. public abstract void run(); When we need to execute some tasks where we don't need to wait to get some result back then we can use Runnable. We just execute our task and do other work as we don't depend on the result of the task. Like if we want to write some logs asynchronously then we can use Runnable interface and execute without waiting for it...

Swagger2 implementation in Spring boot

What is Swagger Using swagger we can describe our REST APIs without much effort. We just need to do some minimal configuration and it provides a complete UI which describes all our endpoints along with interface to execute them using web browser without any additional plugin. Also it is customisable so we can add descriptions and other orchestration to our APIs. Why Swagger When we have a REST API and we want to share it with our users, we need to provide them all the documentation and specification for our APIs which requires some effort to prepare them. With the help of Swagger UI we can share the Swagger URL for our service where users not only see all the listed operations but they can play also with those APIs using web browser. Implementing Swagger in Spring boot application Now we will see how to use Swagger in our spring boot application. Maven dependencies Below dependencies are required for Swagger. "sringfor-swagger-ui" is required if you want Swagg...

SOAP request SSL signature validation in Spring boot cxf

What is signed SOAP request SOAP request can be signed using SSL certificates. It can be use to sign multiple parts in SOAP request message like, timestamp, WSA address, user name, message body etc. Then this signed message is verified by SOAP service at server where it verifies the parts of message using the available certificates. The certificate which is used by client to sign the request, must be available at server side as a trusted certificate otherwise SOAP service will not accept the request and throw the error like "Request signed by untrusted certificate". Callback Handler We need to create the implementation of callback handler where we configure the password details for the certificate keystore. Below is an implementation of the same. public CallbackHandler keystorePasswordCallback() { return (c)->{ WSPasswordCallback pc = (WSPasswordCallback) callbacks[0]; pc.setPassword("keystore_password"); }; } WSS4J Security Interceptor...

jaxb2-maven-plugin to generate java code from XSD schema

In this tutorial I will show how to generate the Java source code from XSD schema. I will use jaxb2-maven-plugin to generate the code using XSD file which will be declared in pom.xml to make it part of build, so when maven build is executed it will generate the java code using XSD. Class generation can be controlled in plugin configuration. Maven changes (pom.xml) Include below plugin in your pom.xml. Here we have done some configuration under configuration section as given below. schemaDirectory : This is the directory where I keep my schema (XSD file). outputDirectory : This is the java source location where I want to generate the Java files. If it is not given then by default it will be generate inside target folder. clearOutputDir : If this property is true then it will generate the classes on each build otherwise it will generate only if output directory is empty. <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</art...

Distributed configuration using spring cloud config

What is spring cloud config Spring cloud config helps application in externalising the configuration properties in a server/client model. Which means there is a config server where all client application connects and read the properties related to their application. Properties can be accessed basis on application name and configured profile. It supports resource based configuration using GIT repository or local file system over HTTP. Properties are managed in form of key-value pair. Though it easily integrate with spring boot application but can be used by any application in other languages also as properties can be accessed through REST URLs also. We are going to learn below things here. GIT repository for configuration management Cloud config server Config client GIT Repository Here we are going to use GIT for configuration management which will be configured in config server. Setting up a GIT repository is very easy. We have to create the properties file using some na...

JAX-rs client with connection and response timeout using jersey

In this tutorial we will learn how to use JAX-rs API with jersey client API to communicate with Restful service. Maven dependencies I have created this example using the Spring boot application, so I need only starter dependencies which are listed as below. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> If you are not using spring application then you need to add below dependencies in your pom. <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</dependency> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</dependency> ...

Why HashMap key should be immutable in java

HashMap is used to store the data in key, value pair where key is unique and value can be store or retrieve using the key. Any class can be a candidate for the map key if it follows below rules. 1. Overrides hashcode() and equals() method.   Map stores the data using hashcode() and equals() method from key. To store a value against a given key, map first calls key's hashcode() and then uses it to calculate the index position in backed array by applying some hashing function. For each index position it has a bucket which is a LinkedList and changed to Node from java 8. Then it will iterate through all the element and will check the equality with key by calling it's equals() method if a match is found, it will update the value with the new value otherwise it will add the new entry with given key and value. In the same way it check for the existing key when get() is called. If it finds a match for given key in the bucket with given hashcode(), it will return the value other...