This post explains different ways in which we can remove entries in a cache using JCache api. The JCache api provides the below methods 1) boolean remove(K key) –> remove an entry with key matching the method parameter 2) boolean remove(K key, V oldValue) –> removes an entry with matching key and value 3) void…… Continue reading Different ways to remove cache entry
JCache Expiry Policy Example
This post explains about JCache’s Expiry policy with an example. Cache’s Expiry policy is used to inform the caching provider when to remove an entry. The below code uses JCache’s CreatedExpiryPolicy as an example. CreatedExpiryPolicy is used to inform the cache provider to remove the entry after a specified time since the entry’s addition to…… Continue reading JCache Expiry Policy Example
Mocking abstract classes
This post explain mocking abstract classes for testing purposes. Note 1) This feature is not available in pre 1.10.12 release 2) We can mock abstract class only if it has parameter less constructor Below is the example of how we can do it. ClassA (Abstract Class) package package2; public abstract class ClassA { public abstract…… Continue reading Mocking abstract classes
Reading json data in non-streaming way (object model)
We can read a json file and create an object model of it, using the ObjectMapper’s readTree api. ObjectMapper class is responsible for parsing the json file and create appropriate java objects. It uses Jackson’s parsing feature internally for reading the json file. They are many overloaded method of readTree available in ObjectMapper. In this…… Continue reading Reading json data in non-streaming way (object model)
Replacing cache entry’s value
This post explains two ways provided by JCache api through, which we can replace the values of existing cache entry. JCache provides two apis for replacing the cache entry 1) replace(key, newValue) –> This api replaces an entry with matching key given in the method argument with newValue. It returns true when replaced else false.…… Continue reading Replacing cache entry’s value
JCache simple example
This post will be a simple introduction to JCache api. For this post and other future post related to JCache, I will be using Ehcache (a tool that provides caching facility). For the below example we need to know about the below terms. 1) CachingProvider 2) CacheManager 3) Cache 4) Configuration CachingProvider –> Represents the…… Continue reading JCache simple example
Creating a file archiver in zip format
This post explains how to create file archiver in WinZip format. WinZip is a file archiver and compression tool. Whenever a zip file is created by default the file is compressed and archived together in a single file. We can change the default behaviour (i.e., compression) by setting the level property in ZipOutputStream. The below…… Continue reading Creating a file archiver in zip format
Creating a zip file of a folder with CRC32 checksum
This post explains how to create a zip file of a folder (also containing sub folders) with CRC32 checksum. The below code is similar to the previous post “Creating zip file of a folder” with minor change in main method. The main method code snippet from the previous post as shown below File folder =…… Continue reading Creating a zip file of a folder with CRC32 checksum
Printing json data in a formatted way
This post explains how to print json data in a properly formatted way, whether the destination is console, file etc. For example when the below code prints the json data to a file, the output will be as shown below Initial Code package package11; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; public…… Continue reading Printing json data in a formatted way
Handling json deserialization error
This post explains how to handle errors generated while deserializing the json file. This can be achieved by implementing an interface “DeserializationProblemHandler” provided by jackson framework as shown below UnMarshallingErrorHandler package package1; import java.io.IOException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; public class UnMarshallingErrorHandler extends DeserializationProblemHandler { @Override public boolean handleUnknownProperty(DeserializationContext ctxt,…… Continue reading Handling json deserialization error