Wednesday, October 12, 2016

What is Volatile keyword in Java

The volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory because of this it's guaranteed that all reader thread will see updated value of the volatile variable once write operation completed.

So if you want to share any variable in which read and write operation is atomic by implementation e.g. read and write in an int or a boolean variable then  you can declare them as volatile variable.

The Java volatile keyword cannot be used with method or class and it can only be used with a variable


Note: If a variable is not shared between multiple threads, you don't need to use volatile keyword with that variable

Singleton Pattern

Singelton pattern restricts the instantiation of a class and ensures that only one instance of the class exists per JVM.

To implement Singleton pattern, we have to follow the following step.

1. Constructor should be private.
2. Public static method that returns the instance of the class.

Lazy Initialization:

When you call the getInstance method then the object of the singleton class is created.

public class SingletonPattern {

private static SingletonPattern instance;

private SingletonPattern() {
}

public static SingletonPattern getInstance() {
if (instance == null) {
instance = new SingletonPattern();
}
return instance;
}
}

Thread-Safe Singleton:

In the case of thread safe singleton, we use the synchronized block or synchronized method to create the thread safe singleton class.
Below example is also known as double check locking. In this approach ,the synchronized block is used inside the if condition with an additional check to ensure that only one instance of singleton class is created. 

public class ThreadSafeSingleton {

    private volatile ThreadSafeSingleton instance;
    
    private ThreadSafeSingleton(){}
    
    public static ThreadSafeSingleton getInstanceUsingDoubleLocking(){
        if(instance == null){
            synchronized (ThreadSafeSingleton.class) {
                if(instance == null){
                    instance = new ThreadSafeSingleton();
                }
            }
        }
        return instance;
    }
    
}


Bill Pugh Singleton Implementation:


In this approach when the singleton class is loaded, SingletonHelper class is not loaded into memory and only when someone calls the getInstance method, this class get loaded and create the singleton class instance.


public class Logger {

    private Logger(){}
    
    private static class SingletonHelper{
        private static final Logger INSTANCE = new Logger();
    }
    
    public static Logger getInstance(){
        return SingletonHelper.INSTANCE;
    }
}


Note: Above approach doesn't require Synchronization.

Enum Singleton:

When we use the reflection then above all approach break and we are able  to create more than one object of each Singleton class to overcome this challenge we are using the enum singleton class.

public enum EnumSingleton {

     INSTANCE;  
}
Note:Only drawback of above approach is that it is not supporting the lazy loading.


Difference between Singleton Pattern vs Static Class in Java

If your class don't maintain any state then use static class over singleton.

If your requirement needs to maintain state then Singleton pattern is the better choice because in concurrent environment static class could lead subtle race condition.

In the case of a heavy object, we can lazily load the singleton class but static class alway eagerly loaded.

Static class provide better  performance than Singleton pattern because static methods are bonded on compile time.

java.lang.Math is an example of static class.
java.lang.Runtime is an example of Singleton class.

Thursday, September 15, 2016

Java Static Import

The static import feature introduces in Java 5.
By using static import we access the static variable and static member function directly.


For e.g.
package com.ocjp.exam.basic;
public class BasicUtility {
    public static int x;
    public static void test(){
          System.out.println(“Inside Test”);
  }
}
***********************Static Import*****************************
package com.ocjp.exam.basic;
import static com.ocjp.exam.basic.BasicUtility.x;
import static com.ocjp.exam.basic.BasicUtility.test;
import static java.lang.System.out;
public class StaticImport {
  public static void main(String[] args) {
      test();
      out.print(x);
   }
}

Tuesday, August 23, 2016

POM Tags Description



The <project> element is the root of the descriptor. The following table lists all of the important child elements.

Element Type      Description
modelVersion String Declares to which version of project descriptor this POM conforms.
parent Parent The <parent> element contains the information required to locate the parent project from which this project will inherit from
groupId String A universally unique identifier for a project. It is normal to use a fully-qualified package name to distinguish it from other projects with a similar name (eg.org.apache.maven).
artifactId String The identifier for this artifact that is unique within the group given by the group ID. An artifact is something that is either produced or used by a project. Examples of artifacts produced by Maven for a project include: JARs, source and binary distributions, and WARs.
version String The current version of the artifact produced by this project.
packaging String The type of artifact this project produces, for example jar war ear pom. Plugins can create their own packaging, and therefore their own packaging types, so this list does not contain all possible types.
Default value is: jar.
name String The full name of the project.
url String The URL to the project's homepage.
Default value is: parent value [+ path adjustment] + artifactId
organization Organization This element describes various attributes of the organization to which the project belongs. These attributes are utilised when documentation is created (for copyright notices and links).
scm Scm(Software Configuration management) Specification for the SCM used by the project, such as CVS, Subversion, etc.
issueManagement IssueManagement The project's issue management system information.
ciManagement CiManagement The project's continuous integration information.
distributionManagement Distribution
Management
Distribution information for a project that enables deployment of the site and artifacts to remote web servers and repositories respectively.
properties/
key=value*
Properties (Many) Properties that can be used throughout the POM as a substitution, and are used as filters in resources if enabled. The format is <name>value</name>.
dependencyManagement Dependency
Management
Default dependency information for projects that inherit from this one. The dependencies in this section are not immediately resolved. Instead, when a POM derived from this one declares a dependency described by a matching groupId and artifactId, the version and other values from this section are used for that dependency if they were not already specified.
dependencies/
dependency*
List<Dependency> (Many) This element describes all of the dependencies associated with a project. These dependencies are used to construct a classpath for your project during the build process. They are automatically downloaded from the repositories defined in this project.
repositories/
repository*
List<Repository>  (Many) The lists of the remote repositories for     discovering dependencies and extensions.
pluginRepositories/
pluginRepository*
List<Repository> (Many) The lists of the remote repositories for discovering plugins for builds and reports.
build Build Information required to build the project.
reporting Reporting This element includes the specification of report plugins to use to generate the reports on the Maven-generated site. These reports will be run when a user executes mvn site. All of the reports will be included in the navigation bar for browsing.
profiles/profile* List<Profile> (Many) A listing of project-local build profiles which will modify the build process when activated.
  

Tuesday, August 9, 2016

Clear Dispatcher Cache Manually


There are many ways to clear the Dispatcher Cache few of them are as follow.

By Using Curl Command:

curl -u admin:admin -X POST -H  "CQ-Action: Activate"  -H  "CQ-Handle: pagepath " -H  "CQ-Path: pagepath "   -H "Content-Length: 0"  -H  "Content-Type: application/octetstream" 
http://DispatcherServerHost:DispatcherServerPort/dispatcher/invalidate.cache


By Using Rest Client:

http://DispatcherServerHost:DispatcherServerPort/dispatcher/invalidate.cache
CQ-Action: Activate
CQ-Handle: pagepath 
CQ-Path:    pagepath 
Content-Length: 0

Content-Type: application/octet-stream











Note: Suppose we have to delete  /content/geometrixx/en/products.html then
          pagepath = /content/geometrixx/en/products










Monday, August 8, 2016

Debug AEM with Eclipse

For debugging the AEM server with Eclipse we have to follow the following steps.

1.Start the AEM instance/server in debug mode.
2. Debug the configuration in Eclipse.


Start the AEM in debug mode:

Edit the start.bat file present inside crx-quickstart/bin folder and add the line
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=30303,suspend=n  after Djava.awt.headless=true
As shown in below image.






After changing the start.bat file save it and start the server by double clicking it.


Debug Configuration in Eclipse:

Open the Eclipse and go to the Run --> Debug Configuration and after that double click on the Remote Java Application give the Name choose the project on which we want to apply debugger and provide the host (localhost) and port (30303) as shown in below image.

















After providing all the details click on debug button.
Now just apply the breakpoint on the java file which you want to debug and enjoy the debugging.








Uninstall AEM Bundle Manually

Uninstall the Bundle manually using CURL command.

curl -u username:password -daction=uninstall http://hostname:4502/system/console/bundles/bundle-name


Runmode of AEM



Check the Runmode of AEM (cq5) 

Go to the URL  http://hostname:port/system/console/status-slingsettings and check the run modes of the AEM server.











In the above mention way, we verify the run mode directly through browser no need to access the server crx-quickstart folder directly.