[Solved] java.lang.IllegalArgumentException: “ABC”

java.lang.IllegalArgumentException is runtime unchecked exception. IllegalArgumentException throw as a preconditions check to indicate that a method has been passed an illegal or inappropriate argument.

IllegalArgumentException occurred generally in two cases:

  • IllegalArgumentException on preconditions check
  • IllegalArgumentException on chained exception

Constructors

  • IllegalArgumentException() :Constructs an IllegalArgumentException with no detail message.
  • IllegalArgumentException(String s) :Constructs an IllegalArgumentException with the specified detail message.
  • IllegalArgumentException(String message, Throwable cause) :Constructs a new exception with the specified detail message and cause.
  • IllegalArgumentException(Throwable cause) :Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).

Example: IllegalArgumentException preconditions check

In below example validating percentage and email based on basic criteria and throwing generic message as “Bad Percentage” and “Invalid Email Address”.

public class IllegalArgumentExceptionExample {
public static void main(String[] args) {
      try
      {
    	  validatePercentage(50); //valid percentage
    	  validatePercentage(-20);//invalida percentage
      }
      catch(Exception ex)
      {
    	  ex.printStackTrace();
      }
      try
      {
    	  validateEmail("facingissuesonit@gmail.com"); //valid email
    	  validateEmail("facingissuesonit-gmail.com");//invalid email
      }
      catch(Exception ex)
      {
    	  ex.printStackTrace();
      }

	}
	public static void validatePercentage(int pct) {
	    if( pct < 0 || pct > 100) {
	         throw new IllegalArgumentException("Bad Percent");
	     }
	}
	public static void validateEmail(String email)
	{
	  if (!email.contains("@")) {
	      throw new IllegalArgumentException("Invalid Email Address");
	  }
	}
}

Output

java.lang.IllegalArgumentException: Bad Percent
	at com.exceptions.IllegalArgumentExceptionExample.validatePercentage(IllegalArgumentExceptionExample.java:44)
	at com.exceptions.IllegalArgumentExceptionExample.main(IllegalArgumentExceptionExample.java:23)
java.lang.IllegalArgumentException: Invalid Email Address
	at com.exceptions.IllegalArgumentExceptionExample.validateEmail(IllegalArgumentExceptionExample.java:51)
	at com.exceptions.IllegalArgumentExceptionExample.main(IllegalArgumentExceptionExample.java:33)

Example: IllegalArgumentException chained exception

In below example, consider a situation in which a method throws an IllegalArgumentException with message “passing Argument is not valid” but the actual cause of exception was an ArithmeticException because of an attempt to divide by zero The method will throw only IllegalArgumentException to the caller. So the caller would not come to know about the actual cause of exception and will see only generic message.

public class ChainedExceptionExample {

	public static void main(String[] args) {
		try {
			int totalAge = 500;
			int numberOfPerson = 0;

			int averageAge = averageAge(totalAge, numberOfPerson);

			System.out.println("Average Age :" + averageAge);
		} catch (Exception ex) {
			System.out.println(ex);
			ex.printStackTrace();
		}
	}

	public static int averageAge(int totalAge, int numberOfPerson) {
		int avarageAge;
		try {
			/**
			 * ArithmaticException can happen here because of value value
			 * NumberOfPerson as 0
			 */
			avarageAge = totalAge / numberOfPerson;
		} catch (Exception ex) {
			System.out.println(ex); // Actual Exception
			/**
			 * Exception Chaining here by relating this ArithmaticException to
			 * IllegalArgumentException
			 */
			throw new IllegalArgumentException("Passing argument is not valid", ex.getCause());
		}
	return avarageAge;
	}
}

Output

java.lang.ArithmeticException: / by zero
java.lang.IllegalArgumentException: Passing argument is not valid
java.lang.IllegalArgumentException: Passing argument is not valid
	at com.customexceptions.ChainedExceptionExample.averageAge(ChainedExceptionExample.java:33)
	at com.customexceptions.ChainedExceptionExample.main(ChainedExceptionExample.java:10)

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s

Chained Exception

Prerequisite

Chained Exception allows to relate one exception with another exception, i.e one exception describes cause of another exception. Chained Exception is used in such type of situations.

In below example, consider a situation in which a method throws an IllegalArgumentException with message “passing Argument is not valid” but the actual cause of exception was an ArithmeticException because of an attempt to divide by zero The method will throw only IllegalArgumentException to the caller. So the caller would not come to know about the actual cause of exception and will see only generic message.

Constructors Of Throwable class

  • Throwable(Throwable cause) : Where cause is the exception that causes the current exception.
  • Throwable(String message, Throwable cause) : Where message is the exception message and cause is the exception that causes the current exception.

Methods Of Throwable class

  • getCause() method:This method returns actual cause of an exception.
  • initCause(Throwable cause) method:This method sets the cause for the calling exception.

Example of using Chained Exception

public class ChainedExceptionExample {
	public static void main(String[] args) {
		try {
			int totalAge = 500;
			int numberOfPerson = 0;

			int averageAge = averageAge(totalAge, numberOfPerson);

			System.out.println("Average Age :" + averageAge);
		} catch (Exception ex) {
			System.out.println(ex);
			ex.printStackTrace();
		}
	}
	public static int averageAge(int totalAge, int numberOfPerson) {
		int avarageAge;
		try {
			/**
			 * ArithmaticException can happen here because of value value
			 * NumberOfPerson as 0
			 */
			avarageAge = totalAge / numberOfPerson;
		} catch (Exception ex) {
			System.out.println(ex); // Actual Exception
			/**
			 * Exception Chaining here by relating this ArithmaticException to
			 * IllegalArgumentException
			 */
			throw new IllegalArgumentException("Passing argument is not valid", ex.getCause());
		}
		return avarageAge;
	}
}

Output

java.lang.ArithmeticException: / by zero
java.lang.IllegalArgumentException: Passing argument is not valid
java.lang.IllegalArgumentException: Passing argument is not valid
	at com.customexceptions.ChainedExceptionExample.testMethod1(ChainedExceptionExample.java:23)
	at com.customexceptions.ChainedExceptionExample.main(ChainedExceptionExample.java:9)

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s

 

Java Exception Propagation

Prerequisite

Exception Propagation :  When an exception happens, propagation is the process where exception is dropped from top to bottom of stack until not caught by catch block or if no any catch block to handle it throw to JVM.

Ways to handle Exception Propagation in checked and unchecked exception are different.

  1. Exception Propagation in Unchecked Exceptions
  2. Exception Propagation in Checked Exceptions (throws)

Exception Propagation in Unchecked Exceptions

When a unchecked exception happens, Exception being dropped from to the top to the
bottom of the stack. If not caught once, the exception again drops down to the previous method and so on until it gets caught or until it reach the very bottom of the call stack. This is called exception propagation.

Note : By default, Unchecked Exceptions are forwarded in calling chain (propagated).

In the example below, exception occurs in method3() where it is not handled, so it is propagated to previous method2() where it is not handled, again it is propagated to method1() where exception is handled.
Exception can be handled in any method in call stack either in main() method, method1(), method2() or method3() .

public class UncheckedExceptionPropagation {
	public static void main(String[] args) {
		method1();
		System.out.println("Flow Completed..");
	}
	public static void method1()
	{
		try
		{
			method2();//Exception propagation to method1
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			System.out.println("Exception Handled Here..");
		}
	}
	public static void method2()
	{
		method3();//Exception propagation to method2
	}
	public static void method3()
	{
		int x=100/0;//ArithmeticException happen here
	}
}

Output

java.lang.ArithmeticException: / by zero
Exception Handled Here..	at com.customexceptions.UncheckedExceptionPropagation.method3(UncheckedExceptionPropagation.java:29)
	at com.customexceptions.UncheckedExceptionPropagation.method2(UncheckedExceptionPropagation.java:25)
	at com.customexceptions.UncheckedExceptionPropagation.method1(UncheckedExceptionPropagation.java:15)
	at com.customexceptions.UncheckedExceptionPropagation.main(UncheckedExceptionPropagation.java:7)

Flow Completed..

Exception Propagation in Checked Exceptions

When a checked exception happens, the propagation of exception does not happen and its mandatory to use throws keyword here. Only unchecked exceptions are propagated. Checked exceptions throw compilation error.

In example below, If we omit the throws keyword from the method3() and method2() functions, the compiler will generate compile time error. Because unlike in the case of unchecked exceptions, the checked exceptions cannot propagate without using throws keyword.

Note : By default, Checked Exceptions are not forwarded in calling chain (propagated).

import java.io.IOException;

public class CheckedExceptionPropagation {
	public static void main(String[] args) {

		method1();
		System.out.println("Flow Completed..");
	}
	public static void method1()
	{
		try
		{
			method2();//Exception propagation to method1
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			System.out.println("Exception Handled Here..");
		}
	}
	public static void method2() throws IOException
	{
		method3();//Exception propagation to method2
	}
	public static void method3() throws IOException
	{
		throw new IOException("Propagation Exception test");
	}
}

Output

java.io.IOException: Propagation Exception test
	at com.customexceptions.CheckedExceptionPropagation.method3(CheckedExceptionPropagation.java:31)
	at com.customexceptions.CheckedExceptionPropagation.method2(CheckedExceptionPropagation.java:27)
	at com.customexceptions.CheckedExceptionPropagation.method1(CheckedExceptionPropagation.java:17)
	at com.customexceptions.CheckedExceptionPropagation.main(CheckedExceptionPropagation.java:9)
Exception Handled Here..
Flow Completed..

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s

Exception handling with method overriding

Before discussing about 4 rules to handle exception in overriding method. You should understand concepts of

Exception handling in override method these are 4 rules where you have to handle exceptions for child class according to given parent class scenarios:

4 Rules for Exception Handling in Overring Method
4 Rules for Exception Handling in Overring Method

Note : When you face any Java interview and test at least one or two questions surely will ask based on these cases to check you Exception Handling concepts and practical knowledge.

Case 1 : If parent-class method doesn’t declare any exception

  1. Child class overriding method can declare no exception in overriding method.

    Overriding Method No Exception throws
    Overriding Method No Exception throws
  2. Child class overriding method can declare unchecked exception in overriding method.

    Overriding Method Throws Unchecked Exception
    Overriding Method Throws Unchecked Exception
  3. Child class overriding method can declare strictly no checked exception in overriding method.

    Override method throws no checked exception
    Override method throws no checked exception

Case 2 : If parent-class method declares unchecked exception

  1. Child class overriding method can declare no exception in overriding method.

    Parent Uncheck child no excepton
    Parent Class Unchecked Exception Child Class No Exception
  2. Child class overriding method can declare any number of unchecked exception in overriding method.

    Parent Unchecked exception Child any Number Unchecked exception
    Parent Class method UncheckedException and Child Class overriding method unlimited Unchecked Exception
  3. Child class overriding method can declare strictly no checked exception in overriding method.

    Parent Class Unchecked Exception and Child class Checked Exception
    Parent Class Unchecked Exception and Child class overriding method Checked Exception not allow.

Case 3 : If parent-class method declares checked exception

  1. Child class overriding method can declare no exception in overriding method.

    Parent Class Method Checked Exception Child Class No Exception
    Parent Class Method Checked Exception Child Class Overriding Method No Exception
  2. Child class overriding method can declare same checked exception in overriding method.

    Parent Class Checked Exception Child Class same Checked Exception
    Parent Class method Checked Exception Child Class overriding method same Checked Exception
  3. Child class overriding method can declare sub-type of checked exception in overriding method.Parent class checked exception and child class sub class checked exception
  4. Child class overriding method can declare any number of unchecked exception in overriding method.Parent class checked exception child class any number unchecked exception

Case 4 : If parent-class method declares both checked & unchecked exceptions

  1. Child class overriding method can declare no exception in overriding method.

    Parent class checked and uncheked exception child no exception
    Parent class checked and un-cheked exception child class overriding method no exception
  2. Child class overriding method can declare same checked exception in overriding method.

    Parent class having checked and uncheck exception both and child class checked exception
    Parent class having checked and uncheck exception both and child class overriding method having same checked exception
  3. Child class overriding method can declare sub-type of checked exception in overriding method.

    Parent class having checked and uncheked exception child class having sub class of cheked exception
    Parent class having checked and uncheked exception child class overriding method having sub class of checked exception
  4. Child class overriding method can declare any number of unchecked exception in overriding method.

    Parent class having cheked and unchecked exception and child class having unlimited unchecked exception
    Parent class having chceked and unchecked exception and child class overriding method having unlimited unchecked exception

Conclusions

I have covered all scenarios for exception handling in overriding method:

  • If parent-class method doesn’t declare any exception, then child-class overriding-method can declare,
    • 1. No exception or
    • 2. Any number of unchecked exception
    • 3. but strictly no checked exception
  • If parent-class method  declare unchecked exception, then child-class overriding-method can declare,
    • 1. No exception or
    • 2. Any number of unchecked exception
    • 3. but strictly no checked exception
  • If parent-class method  declare unchecked exception, then child-class overriding-method can declare,
    • 1. No exception or
    • 2. Same checked exception or
    • 3. Sub-type of checked exception or
    • 4. any number of unchecked exception
  • All above conclusion hold true, even if combination of both checked & unchecked exception is declared in parent-class’ method

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s

 

 

[Solved] java.lang.StackOverflowError

java.lang.StackOverflowError is sub class of java.lang.VirtualMachineError. JVM throws a stack overflow when application stack exhausted, due to deep recursion.

What is StackOverFlowError?

When a method call is invoked by a Java application, a stack frame is allocated on the call stack. The stack frame contains the parameters of the invoked method, its local parameters, and the return address of the method. The return address denotes the execution point from which, the program execution shall continue after the invoked method returns. If there is no space for a new stack frame then, the java.lang.StackOverflowError is thrown by the Java Virtual Machine (JVM).

Reasons to occurs java.lang.StackOverFlowError

  • Recursion : A method invokes itself during it’s execution and method calling stack reach to max limit of JVMExample 1
  • Circular Dependency on method calling. Example 2

A method may not declare such errors in its throw clause, because these errors are abnormal conditions that shall never occur.

Constructors

  • StackOverflowError(): Constructs a StackOverflowError with no detail message.
  • StackOverflowError(String s): Constructs a StackOverflowError with the specified detail message.

Recusrsion StackOverFlowError Example

In this below recursion factorial of number example , I have commented out the recursion terminating condition to create StackOverFlowError.

package com.exceptions.errors;

public class StackOverFlowErrorExample {

	public static void main(String[] args) {
		RecursionExample recursion=new RecursionExample();
		System.out.println(recursion.fact(7));
	}
}

class RecursionExample
{
	public int fact(int n){
//Uncomment this condition to recursion terminating condition
//	    if( n == 0 ){
//	        return 1;
//	    }
	    return n * fact(n-1);
	}
}

Output

Exception in thread "main" java.lang.StackOverflowError
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at com.exceptions.errors.RecursionExample.fact(StackOverFlowErrorExample.java:18)
	at ......

Circular Dependency StackOverFlowError Example

In this example, we defined two classes, Company and Employee. The class Company contains one instance of the Employee class, while, the Employee class contains one instance of the Company class. Thus, we have a circular dependency between these two classes. Furthermore, each toString() method, invokes the corresponding toString() method of the other class, and so on, which results in a java.lang.StackOverflowError.

package com.exceptions.errors;

public class StackOverFlowExample2 {

	public static void main(String[] args) {
		Company company = new Company();
		System.out.println(company.toString());

	}
}

class Company {
	private int budget;
	private Employee employeeInstance = null;

	public Company() {
		budget = 0;
		employeeInstance = new Employee();
	}

	@Override
	public String toString() {
		return "FacingIssueOnIT : Company";
	}
}

class Employee {
	private int salary;
	private Company companyInstance = null;

	public Employee() {
		salary = 10;
		companyInstance = new Company();
	}

	@Override
	public String toString() {
		return "FacingIssueOnIT : Employee";
	}
}

Output

Exception in thread "main" java.lang.StackOverflowError
	at com.exceptions.errors.Employee.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:33)
	at com.exceptions.errors.Company.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:18)
	at com.exceptions.errors.Employee.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:33)
	at com.exceptions.errors.Company.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:18)
	at com.exceptions.errors.Employee.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:33)
	at com.exceptions.errors.Company.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:18)
	at com.exceptions.errors.Employee.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:33)
	at com.exceptions.errors.Company.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:18)
	at com.exceptions.errors.Employee.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:33)
	at com.exceptions.errors.Company.&amp;amp;lt;init&amp;amp;gt;(StackOverFlowExample2.java:18)
	at com.exceptions.errors.Employee.&amp;amp;lt;init&amp;amp;gt;
....

How to deal with the StackOverflowError

  • In case of recursion, always apply terminating condition and that should execute in some cases so that method call not go to continuous call.
  • To inspect the stack trace and detect the repeating pattern of line numbers. These line numbers indicate the code being recursively called. Once you detect these lines, you must carefully inspect your code and understand why the recursion never terminates.
  • In case recursion and terminating condition are in place correctly.  You can increase the stack’s size, in order to allow a larger number of invocations. Default thread stack configuration depend on JVM some may equals to either 512KB, or 1MB. You can increase the thread stack size using the -Xss flag. This flag can be specified either via the project’s configuration, or via the command line. The format of the -Xss argument is:
     -Xss[g|G|m|M|k|K]

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:

[Solved] java.lang.OutOfMemoryError: Java heap space/PermGen/memory leak

java.lang.OutOfMemoryError is sub class of java.lang.VirtualMachineError. It throw when the JVM cannot allocate an object because of out of memory, and no more memory could be made available by the garbage collector. OutOfMemoryError objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable.

Types of OutOfMemoryError in Java

Mainly two types of java.lang.OutOfMemoryError in Java:

    1.  The java.lang.OutOfMemoryError: Java heap space
    2. The java.lang.OutOfMemoryError: PermGen space

Though both of them occur because JVM ran out of memory they are quite different to each other and their solutions are independent of each other.

Constructors

  • OutOfMemoryError() :Constructs an OutOfMemoryError with no detail message.
  • OutOfMemoryError(String s):Constructs an OutOfMemoryError with the specified detail message.

OutOfMemoryError: Java heap space Example

In below example try to create java.lang.OutOfMemoryError by adding name “Saurabh Gupta” in infinite loop. It will add till the point as long as not throw java.lang.OutOfMemoryError.

package com.exceptions.errors;

public class OutOfMemoryErrorExample {

	public static void main(String[] args) {
		StringBuffer str=new StringBuffer("FacingIssuesOnIt");
	    int i=0;
		while(i==0)
		{
			str.append("Saurabh Gupta");
			System.out.println(i);
		}
	}
}

OutOfMemoryError StackTrace

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Unknown Source)
	at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
	at java.lang.AbstractStringBuilder.append(Unknown Source)
	at java.lang.StringBuffer.append(Unknown Source)
	at com.exceptions.errors.OutOfMemoryErrorExample.main(OutOfMemoryErrorExample.java:10)

In above example you see small program can also create OutOfMemoryError because just small wrong steps. as it’s having infinite loop and adding test continuously on same variable. You will get in depth knowledge of this in below paragraphs.

Reason to java.lang.OutOfMemoryError: PermGen space

PermGen can happen by two ways:

Reason 1:

In you are familiar with different generation of heap and garbage collection process, new , old and permanent generation of heap space. PermGen means Permanent Generation of the heap is used to store String pool and various Metadata required by JVM related classes, method and other java primitives.

In most of JVM default size of Perm Space is around “64MB”  which can reach easily by having too many classes and huge number of Strings in application.

Point to Remember: Setting heap size by -Xmx  no impact on OutOfMemory in perm space. To increase size of perm space specify size for permanent generation in JVM options as below.

“-XX: PermSize” and  “-XX: MaxPermSize”

export JVM_ARGS=”-Xmx1024m -XX:MaxPermSize=256m”

Reason 2:
Another reason of “java.lang.OutOfMemoryError: PermGen” is memory leak through Classloaders. Generally it’s happen in webserver and application servers like Glassfish, Weblogic, WebSphere or tomcat.

In application server used different class loaders are used to load different applications so that deploy and undeploy of one application without affecting of others application on same server. But during undeployment if container somehow keeps reference of any class loaded by application class loader then that class and all related class will not get garbage collected and quickly fill permGen space if you deploy and undeploy application many times.

Solutions to Resolve java.lang.OutOfMemoryError

Java.lang.OutOfMemoryError is a kind of error from JVM because of memory leak or objects are consuming memory but not releasing it. To identify root cause of problem required lots of investigation, like which object is taking memory, how much memory it is taking or finding dreaded memory leak.

Solve java.lang.OutOfMemoryError: Java heap space

  1. Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options. For increasing heap size in JVM better option to set  -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application.

                   export JVM_ARGS=”-Xms1024m -Xmx1024m”

  2. If  still getting OutOfMemoryError after applying above solution.In this case, You can use profile tool to investigate memory leak and heap dump. For example :
    • Eclipse Memory Analyzer(MAT) to examine your heap dump.
    • Profiler like Netbeans or JProbe.

This is tough solution and requires some time to analyze and find memory leaks.

Solve java.lang.OutOfMemoryError: PermGen space

  1. Easy way to solve OutOfMemoryError: PermSpace is to increase heap size of Perm space by using JVM option   “-XX: MaxPermSize“. You can also specify initial size of Perm space by using  “-XX: PermSize” and keeping both initial and maximum Perm Space you can prevent some full garbage collection which may occur when Perm Space gets re-sized. For Example

          export JVM_ARGS=”-XX:PermSize=64M -XX:MaxPermSize=256m”

  2. If still getting OutOfMemoryError  after applying above solution. In this case, You can use profile tool to investigate memory leak and heap dump. For example :
    • Eclipse Memory Analyzer(MAT) to examine your heap dump.
    • Profiler like Netbeans or JProbe.

This is tough solution and requires some time to analyze and find memory leaks.

Solve OutOfMemoryError in PermGen Space In Tomcat

Tomcat  6.0 onward  provides memory leak detection feature which can detect many common memory leaks on web-app perspective For Example:

  • ThreadLocal memory leaks
  • JDBC driver registration
  • RMI targes
  • LogFactory
  • Thread spawned by web-apps etc.

You can check complete details on http://wiki.apache.org/tomcat/MemoryLeakProtection

Below are couple of free tools available in java space used to analyze heap and culprits of OutOfMemoryError.

Tools to investigate java.lang.OutOfMemoryError

  1. Eclipse Memory Analyzer(MAT) :  It help to analyze classloader leaks and memory leaks  by analyzing java heap dump.  It also help to consumption of less memory and identify exact suspect of memory leak.
  2. Visualgc (Visual Garbage Collector Monitoring Tool) : Attach this tool to you instrument hot spot JVM . It visually display all key data graphically including garbage collection, class loader and JVM compiler performance.
  3. Jhat (Heap Analyzer Tool) : After JDK-6 it’s part of new version of JDK now. We can use that command line utility to analyze heap dump in heap dump file by using “jmap”. When you execute below command and point your browser to port 7000 then you can start analyzing objects present in heap dump.
    Command : jthat -J-Xmx256m heapdump

References

https://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:

[Solved] java.lang.NumberFormatException: For input string “AB.C”

NumberFormatException is runtime Unchecked Exception. It’s sub class of IllegalArgumentsException. Generally it throws to indicate that the application has attempted to convert a String to one of the numeric types, but that the string does not have the appropriate format.

Constructors

  • NumberFormatException() : Constructs a NumberFormatException with no detail message.
  • NumberFormatException(String s) : Constructs a NumberFormatException with the specified detail message.

Example 1

In below example asking to enter integer value from console and that value further use for other steps. This example will throw ArithmaticException when enter value as 0 or throw NumberFormatException for any value except integer.

import java.util.Scanner;

public class NumberFormatExceptionExample {

public static void main(String[] args) {
System.out.println("Please enter a integer value :");
Scanner scn = new Scanner(System.in);
try
{
int n = Integer.parseInt(scn.nextLine());
if (99%n == 0)
System.out.println(n + " is a factor of 99");
}
catch (ArithmeticException ex)
{
System.out.println("Arithmetic " + ex);
}
catch (NumberFormatException ex)
{
System.out.println("Number Format Exception " + ex);
}
}
}

Output

Please enter a integer value :
3.56
Number Format Exception java.lang.NumberFormatException: For input string: "3.56"

Example 2

Here issue is passing string as non numeric value while parsing for Integer will throw NumberFormatException.

try
{
int age=Integer.parseInt("Twenty");}
catch (NumberFormatException ex)
{
System.out.println("Number Format Exception " + ex);
}

Example 3

Here issue is passing string as numeric  value which out MAX range of integer that’s what throw NumberFormatException. In this case instead of using Integer.parseInt() use Long.parseLong(). 

try{
int phoneNumber=Integer.parseInt("1234567890");
}
catch (NumberFormatException ex)
{
System.out.println("Number Format Exception " + ex);
}

Solutions

Here are some common solutions for NumberFormatException:

  • Always check for numeric before using statement Integer.parseInt().
  • If expectation is big numeric value always use Long.parseLong().

References

https://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:

 

 

 

 

 

Java Custom Exception

We can create our own exception that is called as Custom Exception or User Defined Exception. Custom exceptions are used to customize the exceptions and messages according to user needs.

Steps to create Custom Exception

Below are steps to create custom exception:

  1. Create a class extends with one of the exceptions which are sub types of the java.lang.Exception class. Generally exception class always extends directly from the Exception class.
  2. Create a constructor with String parameter which is the detailed message of the exception.
  3. In this constructor, simply call the super constructor and pass the message.

Note : There is convection to use Custom Exception name ends with word ‘Exception’.

Steps Custom Exception or User Defined Exception
Custom Exception or User Defined Exception

In below example create a Custom Exception as EmployeeNotFoundException
which is extending class java.lang.Exception and create different constructors to handle exception with exception message and stack traces.

public class EmployeeNotFoundException extends Exception{
        //Constructor to throw exception with message
	public EmployeeNotFoundException(String message)
	{
		super(message);
	}
        //Constructor to throw exception with stack trace
	public EmployeeNotFoundException(Throwable cause)
	{
		super(cause);
	}
        //Constructor throw exception meth message and stack trace
	public EmployeeNotFoundException(String message,Throwable cause)
	{
		super(message,cause);
	}
}

Example to use Custom Exception

import java.util.Arrays;
import java.util.List;
public class CustomExceptionTest {
    public static void main(String[] args) throws EmployeeNotFoundException{
    String[] empArr ={"Saurabh", "Gaurav", "Shailesh", "Ankur", "Ranjith", "Ramesh"};
    List empList = Arrays.asList(empArr);
    String searchStr = "Rishabh";
	try {
	   if (!empList.contains(searchStr)) {
//Throw exception when emmployee name not match with existing list with customize message.
	throw new EmployeeNotFoundException("Employee not found in search");
	     }
	} catch (Exception ex) {
	 throw new EmployeeNotFoundException("Employee not found",ex);
	}
	}
}

Output

Exception in thread "main" com.customexceptions.EmployeeNotFoundException: Employee not found
    at com.customexceptions.CustomExceptionTest.main(CustomExceptionTest.java:16)
Caused by: com.customexceptions.EmployeeNotFoundException: Employee not found in search
    at com.customexceptions.CustomExceptionTest.main(CustomExceptionTest.java:13)

Conclusion

You have learn here :

  • What is Custom Exception or User-defined exception?
  • How to create Custom Exception or User-defined Exception?
  • and How to implement it?

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s

 

[Solved] org.mockito.exceptions.misusing.UnnecessaryStubbingException

UnnecessaryStubbingException is runtime and sub class of MockitoException. Mockito JUnit Runner triggers UnnecessaryStubbingException only when none of the test methods use the stubbings. This means that it is ok to put default stubbing in a ‘setup’ method or in test class constructor. That default stubbing needs to be used at least once by one of the test methods.

Constructors

  • UnnecessaryStubbingException(String message) : Will throw exception with message.

Example

//code test:
 String result = translator.translate("Saurabh");

//Mock:
// <- stubbing used during code execution
 when(translator.translate("Saurabh")).thenReturn("Gupta");
// <- stubbing never used
when(translator.translate("Gaurav")).thenReturn("Gupta");

Solutions

It is highly recommended to remove unused stubbings to keep the codebase clean. You can opt-out from detecting unused stubbing using MockitoJUnitRunner.Silent() or MockitoRule.silent() (when you are using Mockito JUnit rules.

References

https://static.javadoc.io/org.mockito/mockito-core/2.6.5/org/mockito/exceptions/misusing/UnnecessaryStubbingException.html

Know More

To know more about Junit, Mockito and exception solutions follow below links:

[Solved] org.mockito.exceptions.misusing.PotentialStubbingProblem

PotentialStubbingProblem is a RuntimeException and subclass of MockitoException. Mockito framework throws this exception when “stubbing arguments mismatch“.

Strict stubbing is a new opt-in feature for JUnit Rule and JUnit Runner to detect potential stubbing problems org.mockito.exceptions.misusing.PotentialStubbingProblem exception is thrown when mocked method is stubbed with some argument in test but then invoked with different argument in code.

This exception can occurred by many reasons:

  1. Mistake or typo in the test code, the argument(s) used when declaring stubbing’s is unintentionally different.
  2. Mistake or typo in the code under test, the argument(s) used in the code under test is unintentionally different.
  3. Intentional use of stubbed method with different argument, either in the test (more stubbing) or in code under test.

Constructors

  • PotentialStubbingProblem(String message) : Will throw exception with message.

Example

 //test method:
 given(mock.getSomething(200)).willReturn(something);

 //code under test:
// stubbing argument mismatch
 Something something = mock.getSomething(100);

Solutions

public class TestExample {
     @Rule
     public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

     @Test public void exampleTest() {
         //Change the strictness level only for this test method:
         mockito.strictness(Strictness.LENIENT);

         //remaining test code
         given(mock.getSomething(200)).willReturn(something);
         //Will work
         Something something = mock.getSomething(100);

     }
 }

References

https://static.javadoc.io/org.mockito/mockito-core/2.6.5/org/mockito/exceptions/misusing/PotentialStubbingProblem.html

Know More

To know more about Junit, Mockito and exception solutions follow below links:

[Solved] org.mockito.exceptions.misusing.RedundantListenerException

RedundantListenerException is RuntimeException and sub class of MockitoException. This occurred by Mockito framework when  instance of MockitoListener is being added to Mockito Framework and there is already a listener with this implementation type registered.

Note : It is ok to add multiple different implementations of the same listener interface type.

Constructors

  • RedundantListenerException(String message) : Will throw exception with message.

Example

MockitoFramework.addListener(MockitoListener);

Solutions

If you are trying to add the listener but a listener of the same type was already added (and not removed) this method will throw RedundantListenerException. This is a safeguard to ensure users actually remove the listeners via removeListener</strong>. We do not anticipate the use case where adding the same listener type multiple times is useful.

References

https://static.javadoc.io/org.mockito/mockito-core/2.6.5/org/mockito/exceptions/misusing/RedundantListenerException.html

Know More

To know more about Junit, Mockito and exception solutions follow below links:

[Solved]org.mockito.exceptions.base.MockitoException

MockitoException  is RuntimeException raised by Mockito framework to emit an error either due to Mockito, or due to the User. Mockito throws exception in form of stacktrace with suggested solutions.

StackTrace for Application calls:

StackTraceElement[] Throwable.getStackTrace()

StackTrace with Mockito and API calls:

StackTraceElement[] getUnifilteredStackTrace()

Constructors

  • MockitoException(String message) : Will throw exception with message.
  • MockitoException(String message, Throwable T) : Will throw exception with message and stacktrace.

Mockito Exception Subclasses

MockitoException Hierarchy
MockitoException Hierarchy

Example

In below example , using OngoingStubbing on mock object of Map interface where setting stub object for get() method which accept any input String and return response as String as set in thenReturn() method.

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.stubbing.OngoingStubbing;

public class MockitoApplicationTester {

	@Test
	public void second_stubbing_throws_InCorrectUseOfAPI() throws Exception {
		// Mock of Map interface
		Map<String, String> map = mock(Map.class);

		OngoingStubbing<String> mapOngoingStubbing = when(map.get(anyString()));
		try {
			mapOngoingStubbing.thenReturn("1st stubbing");
			//This method will throw exception
			mapOngoingStubbing.thenReturn("2nd stubbing");
			Assert.assertEquals(map.get("Test"), "1st stubbing");
		} catch (MockitoException e) {
			StackTraceElement[] stacks = e.getStackTrace();
			for (StackTraceElement stack : stacks) {
				System.out.println(stack.getMethodName());
			}

			StackTraceElement[] ustacks = e.getUnfilteredStackTrace();
			for (StackTraceElement stack : ustacks) {
				System.out.println(stack.getMethodName());
			}
			throw e;

		}
	}

Output

org.mockito.exceptions.base.MockitoException:
Incorrect use of API detected here:
-> at com.facingissuesonit.mockito.MockitoTestExamples.MockitoApplicationTester.second_stubbing_throws_IndexOutOfBoundsException(MockitoApplicationTester.java:48)

You probably stored a reference to OngoingStubbing returned by when() and called stubbing methods like thenReturn() on this reference more than once.
Examples of correct usage:
    when(mock.isOk()).thenReturn(true).thenReturn(false).thenThrow(exception);
    when(mock.isOk()).thenReturn(true, false).thenThrow(exception);

	at com.facingissuesonit.mockito.MockitoTestExamples.MockitoApplicationTester.second_stubbing_throws_IndexOutOfBoundsException(MockitoApplicationTester.java:48)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			>&#65279;</span>

Solutions

Above example is throwing MockitoException with message as “Incorrect use of API detected here”  because of thenReturn() method set two times value for single mock stub object. For solving above issue comment one of thenRetun() statement from try block.

References

https://static.javadoc.io/org.mockito/mockito-core/2.6.5/org/mockito/exceptions/base/MockitoException.html#getUnfilteredStackTrace()

Know More

To know more about Junit, Mockito and exception solutions follow below links:

[Solved] org.mockito.exceptions.misusing.WrongTypeOfReturnValue

WrongTypeOfReturnValue is RuntimeException and Subclass of MockitoException.  It throws when :

  1.  It might occurred because of wrongly written multi-threaded tests.
  2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies with doReturn|Throw() family of methods. 

Constructors

  • WrongTypeOfReturnValue(String message) : Will throw exception with message.

Example

Below case to show you on what case Mockito will throw WrongTypeOfReturnValue .

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import org.junit.Test;

public class NestedWhen {
	public class Bar {
	}

	public class Foo {
		Bar getBar() {
			return new Bar();
		}
	}

	public class Product {
		Bar bar;

		Product(Foo f) {
			bar = f.getBar();
		}
	}

	public class ProductService {
		Foo foo;

		ProductService(Foo f) {
			foo = f;
		}

		Product produce() {
			return new Product(foo);
		}
	}

	@Test
	public void nestedWhenTest() {
		Foo mfoo = mock(Foo.class);
		Product mpoo = mock(Product.class);
		ProductService productService = spy(new ProductService(mfoo));
		// throw WrongTypeOfReturnValue exception here!!!
		when(productService.produce()).thenReturn(mpoo);
	}
}

Output

org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
NestedWhen$Product$$EnhancerByMockitoWithCGLIB$$4418933b cannot be returned by getBar()
getBar() should return Bar
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

	at com.facingissuesonit.mockito.MockitoTestExamples.NestedWhen.nestedWhenTest(NestedWhen.java:44)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)

Solutions

Here above code is throwing this exception because spy execute the real code by nature, so when calling “productService.produce()” the expression is actually executed and return a real Product. The constructor of Product takes the constructor arg “foo” which is a mock and it executes “f.getBar()”. This invocation is recorded by mockito because this “foo” instance is a mock.

Then when you want to return “mpoo”, mockito raises the exception WrongTypeOfReturnValue saying that the recorded invocation “foo.getBar()” cannot return a Product.

If you want to mock a partial mock, which you should avoid if possible. You need to use the following style for spies, this way Mockito can tell the spy instance to only record the invocation.

doReturn(mpoo).when(productService).produce();

References

https://static.javadoc.io/org.mockito/mockito-core/2.6.5/org/mockito/exceptions/misusing/WrongTypeOfReturnValue.html

Know More

To know more about Junit, Mockito and exception solutions follow below links:

 

[Solved] org.mockito.exceptions.misusing.InvalidUseOfMatchersException

InvalidUseOfMatchersException is RuntimeException and subclass of MockitoException. It throws when trying to push behavior on object which is not mock.

Constructors

  • InvalidUseOfMatchersException() : Will throw exception.
  • InvalidUseOfMatchersException(String message) : Will throw exception with message.

Example

Here in below example mock Map interface and creating stub for get method to return fixed String for any type of String elements.


import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

public class MockitoApplicationTester {

@Test
public void second_stubbing_throws_InCorrectUseOfAPI() throws Exception {
// Mock of Map interface
Map<String, String> mapMock = mock(Map.class);
Map<String, String> mapReal = new HashMap<String, String>();

//Issue is here because performing argument matcher without mock object
when(mapReal.get(anyString())).thenReturn("1st stubbing");

//Correct statement
when(mapMock.get(anyString())).thenReturn("1st stubbing");

Assert.assertEquals(mapMock.get("Test"), "1st stubbing");

}

}

Output


org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:

-> at com.facingissuesonit.mockito.MockitoTestExamples.MockitoApplicationTester.second_stubbing_throws_InCorrectUseOfAPI(MockitoApplicationTester.java:22)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

at com.facingissuesonit.mockito.MockitoTestExamples.MockitoApplicationTester.second_stubbing_throws_InCorrectUseOfAPI(MockitoApplicationTester.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)

Solutions

Here in above code is throwing this exception because using real object to create stub that’s what throwing “InvalidUseOfArgumentMatchersException“. To fix above this comment out wrong statement and execute again.

This exception can occurred by others ways also:

  1. This error might show up because you use argument matchers with methods that cannot be mocked.
  2. Stubbed and verified method can be used on final/private/equals()/hashCode() methods.
  3. Mocking methods declared on non-public parent classes is not supported.

References

https://static.javadoc.io/org.mockito/mockito-core/2.6.5/org/mockito/exceptions/misusing/InvalidUseOfMatchersException.html

Know More

To know more about Junit, Mockito and exception solutions follow below links:

[Solved] org.mockito.exceptions.misusing.MissingMethodInvocationException

MissingMethodInvocationException is RuntimeException and subclass of MockitoException. It generally throws when:

  1. It might occurs when() requires an argument which has to be ‘a method call on a mock. For example:                when(mock.getArticles()).thenReturn(articles);
  2. You try to stubbed/verified either of: final/private/equals()/hashCode() methods.
  3. Mocking methods declared on non-public parent classes is not supported.
  4. Inside when() you don’t call method on real object always call from mock or spy.

Constructors

  • MissingMethodInvocationException(String message) : Will throw exception with message.

Example

To complete example follow link Mockito + JUnit Tutorial

import static org.mockito.Mockito.spy;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.mockito.Mockito.*;
//@RunWith attaches a runner with the test class to initialize the test data
@RunWith(org.mockito.runners.MockitoJUnitRunner.class)
public class MathApplicationTestrSpy {
private CalculatorService calcService;

@Before
public void setUp(){
Calculator calculator = new Calculator();
calcService = spy(calculator);
//Issue is here because using actual object
when(calculator.add(20.0,10.0)).thenReturn(30.0);
}

@Test
public void testAdd(){
Assert.assertEquals(calcService.add(20.0, 10.0),30.0,0);
}

}

Output

org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

	at com.facingissuesonit.mockito.MockitoTestExamples.MathApplicationTestrSpy.setUp(MathApplicationTestrSpy.java:29)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)

Solutions

Here in above example trying stub on real object instead of mock and spy object. To solve this issue update code as below. For complete example please refer Mockito + JUnit TutorialMockito + JUnit Tutorial.


@Before
public void setUp(){
Calculator calculator = new Calculator();
calcService = spy(calculator);
//Issue is here because using actual object
//when(calcService.add(20.0,10.0)).thenReturn(30.0);

//Used Spy object
when(calculator.add(20.0,10.0)).thenReturn(30.0);
}

References

https://static.javadoc.io/org.mockito/mockito-core/2.6.5/org/mockito/exceptions/misusing/MissingMethodInvocationException.html

Know More

To know more about Junit, Mockito and exception solutions follow below links:

Elasticsearch Ingest Node Vs Logstash Vs Filebeat

Elasticsearch Ingest Node

Ingest node use to pre-process documents before the actual document indexing
happens. The ingest node intercepts bulk and index requests, it applies transformations, and it then passes the documents back to the index or bulk APIs.

Logstash

Logstash is a server-side data processing pipeline that ingests data from multiple sources simultaneously, transforms it, and then sends it to different output sources like Elasticsearch, Kafka Queues, Databases etc.

Filebeat

Filebeat is lightweight log shipper which reads logs from thousands of logs files and forward those log lines to centralize system like Kafka topics to further processing on Logstash, directly to Logstash or Elasticsearch search.

There is overlap in functionality between Elasticsearch Ingest Node , Logstash and Filebeat.All have there weakness and strength based on architectures and area of uses. You cam also integrate all of these Filebeat, Logstash and Elasticsearch Ingest node by minor configuration to optimize performance and analyzing of data.

Below are some key points to compare Elasticsearch Ingest Node , Logstash and Filebeat.

Elasticsearch Ingest Node Vs Logstash Vs Filebeat

PointsElasticsearch Ingest NodeLogstashFilebeat
Data In and OutAs ingest node runs as pipeline within the indexing flow in Elasticsearch, data has to be pushed to it
through bulk or indexing requests and configure pipeline processors process documents before indexing of actively writing data
to Elasticsearch.
Logstash supports wide variety of input and output plugins. It can act as middle server to accept pushed data from clients over TCP, UDP and HTTP and filebeat, message queues and databases.
It parse and process data for variety of output sources e.g elasticseach, message queues like Kafka and RabbitMQ or long term data analysis on S3 or HDFS.
Filebeat specifically to shipped logs files data to Kafka, Logstash or Elasticsearch.
QueuingElasticsearch Ingest Node is not having any built in queuing mechanism in to pipeline processing.
If the data nodes are not bale to accept data, the ingest node will stop accepting data as well.
Logstash provide persistent queuing feature mechanism features by storing on disk.Filebeat provide queuing mechanism with out data loss.
Back-pressureClients pushing data to ingest node need to be able to handle back-pressure by queuing data In case elasticsearch is not reachable or able to accept data for extended period otherwise there would be data loss.Logstash provide at least once delivery guarantees and buffer data locally through ingestion spikes.Filebeat designed architecture like that with out losing single bit of log line if out put systems like kafka, Logstash or Elasticsearch not available
Data ProcessingIngest node comes around 20 different processors, covering the functionality of
the most commonly used Logstash plugins.

Ingest node have some limitation like pipeline can only work in the context of a single event. Processors are
also generally not able to call out to other systems or read data from disk. It's also not having filters as in beats and logstash.
Logstash has a larger selection of plugins to choose from. This includes
plugins to add or transform content based on lookups in configuration files,
Elasticsearch, Beats or relational databases.
Logstash support filtering out and dropping events based on
configurable criteria.
Beats support filtering out and dropping events based on
configurable criteria.
ConfigurationEach document can only be processed by a single pipeline when passing through the ingest node.

Logstash supports to define multiple logically separate pipelines by conditional control flow s to handle complex and multiple data formats.

Logstash is easier to measuring and optimizing performance of the pipeline to supports monitoring and resolve potential issues quickly by excellent pipeline viewer UI.
Minor configuration to read , shipping and filtering of data. But limitation with parsing.
SpecializationIngest Node pipeline processed data before doing indexing on elasticsearch.Its middle server to parse process and filter data from multiple input plugins and send processes data to output plugins.Specific to read and shipped logs from different servers to centralize location on Elasticsearch, Kafka and if require parsing processed through Logstash.
IntegrationLogstash supports sending data to an Ingest Pipeline.Ingest node can accept data from Filebeat and Logstash etc,Filebeat can send data to Logstash , Elasticsearch Ingest Node or Kafka.
PerformancePlease follow below link to check performance of each on different cases:Elasticsearch Ingest Node , Logstash and Filebeat Performance comparison.

Know More

To know more about Elasticsearch Ingest Node, Logstash or Filebeat follow below links:

Java: Duration and Period Calculation

Almost every developer handle Duration and Period issues while need to do any calculation based on Date and Time. In Java 8 introduces new classes to handle Duration and period.

  • Period : A date period is measured in days, months, and years. Period can be calculated by java.time.Period class.
  • Duration: A time duration is measured in hours, minutes, and seconds. Duration can be calculated by java.time.Duration class.

Period period= Period.between(localDate1, localDate2);

Duration duration=Duration.between(localTime1, localTime2);

Duration.toString() method return duration as P1Y10M6D. Here duration is 1 year, 10 month and 6 days.

Period.toString() method return period as PT3H10M40S. Here duration is 3 hour, 10 minute and 40 seconds.

Example

Below is example to calculate Period and Duration  between two dates and times.

package com.date;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Java8DurationAndPeriodExample {

public static void main(String[] args) {

LocalDate june9th2016 = LocalDate.of(2016, Month.JUNE, 9);
LocalDate April15th2017 = LocalDate.of(2017, Month.APRIL, 15);
DateTimeFormatter format=DateTimeFormatter.ofPattern("MM/dd/yy");
// Compute period:
Period period=Period.between(April15th2017, june9th2016);
System.out.format("Period between %s and %s is %d year, %d month, %d days \n" , April15th2017.format(format), june9th2016.format(format), period.getYears(), period.getMonths(), period.getDays());

// Add one month to August 10, 2018:
LocalDate April15th2017plusOneMonth=April15th2017.plus(1, ChronoUnit.MONTHS);
System.out.format("Adding one month to %s yields %s \n", April15th2017.format(format), April15th2017plusOneMonth.format(format));

// Subtract one month from August 10, 2018:
LocalDate April15th2017minusOneMonth=April15th2017.plus(-1, ChronoUnit.MONTHS);
System.out.format("Subtracting one month from %s yields %s \n", April15th2017.format(format), April15th2017minusOneMonth.format(format));

// Create two times for duration processing:
LocalTime twelveFifteenPM = LocalTime.of(12, 15, 9);
LocalTime twoTwentyFiveAM = LocalTime.of(2, 25, 0);
DateTimeFormatter timeFormat=DateTimeFormatter.ofPattern("HH:mm:ss a");
Duration duration=Duration.between(twoTwentyFiveAM, twelveFifteenPM);

System.out.format("Duration between %s and %s is %s \n", twoTwentyFiveAM.format(timeFormat), twelveFifteenPM.format(timeFormat), duration);
duration=Duration.between(twelveFifteenPM, twoTwentyFiveAM);
System.out.format("Duration between %s and %s is %s \n", twelveFifteenPM.format(timeFormat), twoTwentyFiveAM.format(timeFormat), duration);
	<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			>&#65279;</span>}
}

Output


Period between 04/15/17 and 06/09/16 is 0 year, -10 month, -6 days 
Adding one month to 04/15/17 yields 05/15/17 
Subtracting one month from 04/15/17 yields 03/15/17 
Duration between 02:25:00 AM and 12:15:09 PM is PT9H50M9S 
Duration between 12:15:09 PM and 02:25:00 AM is PT-9H-50M-9S 

[Solved] java.time.temporal. UnsupportedTemporalType Exception: Unsupported field: YearOfEra

UnsupportedTemporalTypeException is sub class of java.time.DateTimeException which is runtime checked exception. UnsupportedTemporalTypeException indicates that a ChronoField or ChronoUnit is not supported for a Temporal class.

Constructors

  • UnsupportedTemporalTypeException(String message): Constructs a new UnsupportedTemporalTypeException with the specified message.
  • UnsupportedTemporalTypeException(String message, Throwable cause): Constructs a new UnsupportedTemporalTypeException with the specified message and cause.

 

Exception Example

Below is example to convert Legacy date(java.util.Date) to java.time.Instant and vice versa.

package java.time.temporal;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class UnsupportedTemporalTypeExceptionExample {

public static void main(String[] args) {
SimpleDateFormat utilDateTimeFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//Exception on below line to resolve this issue uncomment at end of line.
DateTimeFormatter java8DateTimeFormat=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");//.withZone(ZoneId.systemDefault());;
//Date to Instant
Date date=new Date();
Instant inst = date.toInstant();
System.out.println("Initial Date :"+utilDateTimeFormat.format(date));
System.out.println("Converted Instant :"+java8DateTimeFormat.format(inst));
//Instant To Date
Date newDate = Date.from(inst);
System.out.println("Converted Date :"+utilDateTimeFormat.format(newDate));
	}
}

Exception StackTrace


Initial Date :2018/08/04 08:11:53
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
    at java.time.Instant.getLong(Unknown Source)
    at java.time.format.DateTimePrintContext.getValue(Unknown Source)
    at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(Unknown Source)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(Unknown Source)
    at java.time.format.DateTimeFormatter.formatTo(Unknown Source)
    at java.time.format.DateTimeFormatter.format(Unknown Source)
    at com.date.Legacy2Java8DateTimeInteroprability.main(Legacy2Java8DateTimeInteroprability.java:26)

Issue

The Instant class doesn’t contain Zone information, it only stores timestamp in milliseconds from UNIX epoch, i.e. 1 Jan 1070 from UTC. So, formatter can’t print a date because date always printed for concrete time zone. To format an Instant a time-zone is required. Without a time-zone, the formatter does not know how to convert the instant to human date-time fields, and therefore throws an exception.

Solutions

The time-zone can be added directly to the formatter using withZone().You should set time zone to formatter and all will be fine.

in above example you can uncomment commented section as below to solve this issue.


DateTimeFormatter java8DateTimeFormat=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());;

some more ways to format date


DateTimeFormatter formatter =
    DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
                     .withLocale( Locale.UK )
                     .withZone( ZoneId.systemDefault() );

or

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.UK).withZone(ZoneOffset.UTC);

Java: Legacy to Java 8 Date Time Interoperability

In Java 8 added some new methods with Legacy java.util Date and Time classes to convert object of java.time classes to get advantage new java 8 Date and Time classes with minimal changes.

Points To Remember:

  • java.util.Date and java.time.Instance classes are similar. The Date.from(Instant) and Date.toInstant() methods allow conversion between these classes. It represents instantaneous point of time in UTC which is independent of TimeZone  as epoch-seconds (since 1970-01-01T00:00:00Z) plus nanoseconds.
  • The java.time.ZonedDateTime class is the replacement for java.util.GregorianCalendar. The GregorianCalendar.from(ZonedDateTime) and GregorianCalendar.to(ZonedDateTime) methods faciliate conversions between these classes. It present time as follows:
    • LocalDate : year, month and day
    • LocalTime: hour, minute, second, nanoseconds
    • ZoneId: time zone
    • ZoneOffset: current offset from GMT
  • The ZoneOffset class specifies only an offset from Greenwich/UTC. The ZoneId class specifies a time zone identifier and has access to the rules used each time zone.
  • GeregorianCalendar with date as 1970-01-01 will use date component can be replaced with an instance of LocalDate.
  • GeregorianCalendar with time as 00:00 will use time component can be replaced with an instance of LocalTime.

java.util.Date to an java.time.Instant


Date date=new Date();
Instant instant=date.toInstant();

An java.time.Instant to java.util.Date


Date date=Date.from(instant);

java.util.Calendar to an java.time.Instant


Calendar cal=Calendar.getInstance();
Instant instant=cal.toInstant();

java.util.GregorianCalendar instance to an java.time.ZoneDateTime


Calendar gCal=GregorianCalendar.getInstance();
ZoneDateTime zoneDateTime=ZoneDateTime.ofInstant(gCal.toInstant(),ZoneId.systemDefault());

java.time.ZoneDateTime instance to java.util.GregorianCalendar


GregorianCalendar gCal=GregorianCalendar.from(zoneDateTime);

java.util.TimeZone object to java.time.ZoneId


Calendar gCal1=GregorianCalendar.getInstance();
TimeZone tz = cal.getTimeZone();
ZoneId zoneID=tz.toZoneId();
int tzoffset = cal.get(Calendar.ZONE_OFFSET);

Example

In below example considered all the cases of Legacy to Java 8 object classes.

package com.date;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Legacy2Java8DateTimeInteroprability {

public static void main(String[] args) {
SimpleDateFormat utilDateTimeFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateTimeFormatter java8DateTimeFormat=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());;

//Date to Instant
Date date=new Date();
Instant inst = date.toInstant();
System.out.println("Initial Date :"+utilDateTimeFormat.format(date));
System.out.println("Converted Instant :"+java8DateTimeFormat.format(inst));

//Instant To Date
Date newDate = Date.from(inst);
System.out.println("Converted Date :"+utilDateTimeFormat.format(date));

//Calendar to ZoneDateTime
Calendar cal = Calendar.getInstance();
ZonedDateTime zdt = ZonedDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault());
System.out.println("Calendar to ZoneDateTime :"+java8DateTimeFormat.format(zdt));

//Calendar to Instant
Instant inst1 = cal.toInstant();
System.out.println("Calendar to Instant :"+java8DateTimeFormat.format(inst1));

//Geregorian calendar from Zone Date Time
GregorianCalendar gcal = GregorianCalendar.from(zdt);
System.out.println("Gregorian Calendar Date Time:"+utilDateTimeFormat.format(gcal.getTime()));
ZonedDateTime zdt1 = gcal.toZonedDateTime();
System.out.println("Calendar to ZoneDateTime Date :"+java8DateTimeFormat.format(zdt1));

LocalDateTime ldt = zdt1.toLocalDateTime();
System.out.println("Local Date Time :"+java8DateTimeFormat.format(ldt));
LocalDate localDate = zdt1.toLocalDate();
System.out.println("Local Date :"+java8DateTimeFormat.format(ldt));
LocalTime localTime = zdt1.toLocalTime();
System.out.println("Local Time :"+java8DateTimeFormat.format(ldt));

//TimeZone to Zone Id
Calendar gCal1=GregorianCalendar.getInstance();
TimeZone tz = cal.getTimeZone();
System.out.println("Time Zone :"+tz.getDisplayName());
ZoneId zoneID=tz.toZoneId();
System.out.println("Zone ID:"+zoneID.getId());
int tzoffset = cal.get(Calendar.ZONE_OFFSET);
<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			>&#65279;</span>System.out.println("Zone Offset:"+tzoffset);
	}
}

Output


Initial Date :2018/08/04 07:48:45
Converted Instant :2018/08/04 07:48:45
Converted Date :2018/08/04 07:48:45
Calendar to ZoneDateTime :2018/08/04 07:48:45
Calendar to Instant :2018/08/04 07:48:45
Gregorian Calendar Date Time:2018/08/04 07:48:45
Calendar to ZoneDateTime Date :2018/08/04 07:48:45
Local Date Time :2018/08/04 07:48:45
Local Date :2018/08/04 07:48:45
Local Time :2018/08/04 07:48:45
Time Zone :Pacific Standard Time
Zone ID:America/Los_Angeles
Zone Offset:-28800000

Java : How to get current date time?

In below example you will learn ways to get current date and time from Legacy Java Date and Calendar APIs and also from Java 8 LocalDateTime, LocalDate and LocalTime..

java.util.Date

Date date=new Date();

java.util.Calendar and java.util.GregorianCalendar

Calendar calendar=Calendar.getInstance();
Calendar gCalnedar=GregorianCalendar.getInstance();

java.time.LocaleDateTime

LocalDateTime localDateTime=LocalDateTime.now();

java.time.LocaleDate

LocalDate localDate=LocalDate.now();

java.time.LocaleTime

LocalTime localTime=LocalTime.now();

 

Complete Example

package com.date;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class CurrentDateandTimeExample {

public static void main(String[] args) {

SimpleDateFormat dateTimeFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//java.util.Date
Date date=new Date();
System.out.println("java.util.date Current Date and Time :"+ dateTimeFormat.format(date));

//java.util.Calendar and java.util.GregorianCalendar
Calendar calendar=Calendar.getInstance();
System.out.println("java.util.Calendar Current Date and Time :"+ dateTimeFormat.format(calendar.getTime()));
Calendar gCalendar=GregorianCalendar.getInstance();
System.out.println("java.util.GregorianCalendar Current Date and Time :"+ dateTimeFormat.format(gCalendar.getTime()));

//Java 8
//java.time.LocalDateTime
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime lDateTime=LocalDateTime.now();
System.out.println("java.time.LocalDateTime Current Date and Time :"+ dateTimeFormatter.format(lDateTime));

//java.time.LocalDate
DateTimeFormatter dateFormatter=DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate lDate=LocalDate.now();
System.out.println("java.time.LocalDate Current :"+ dateFormatter.format(lDate));

//java.time.LocalTime
DateTimeFormatter timeFormatter=DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime lTime=LocalTime.now();
System.out.println("java.time.LocalTime Current Time :"+ timeFormatter.format(lTime));

	}

}

Output


java.util.date Current Date and Time :2018/08/03 12:12:26
java.util.Calendar Current Date and Time :2018/08/03 12:12:26
java.util.GregorianCalendar Current Date and Time :2018/08/03 12:12:26
java.time.LocalDateTime Current Date and Time :2018/08/03 12:12:26
java.time.LocalDate Current :2018/08/03
java.time.LocalTime Current Time :12:12:26

“Learn From Others Experience"

Advertisements
%d bloggers like this: