Monday, July 13, 2015


Jenkins is an opensource and most popular continuous integration tool written in Java. There are many plugins are available to make it more easier. It supports almost all version control systems. You can configure builts with various means like, triggered by a commit in a version control system, scheduling by cron-jobs, built after a specific built completes etc.. ect
In this article I will show you how to install and configure Jenkins on CentOS 6.4.
Step – 1
To install and configure Jenkins on CentOS we have to first install JAVA on the machine. Below are the steps to install JAVA on CentOS.
yum install java-1.6.0-openjdk
Verify the java by issuing the below command
java -version
Now we will add Jenkins Repository to our machine.
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
Now we will Install Jenkins
yum install jenkins
Add Jenkins to system boot
chkconfig jenkins on
Start Jenkins
service jenkins start
By default Jenkins will start on Port 8080. You can verify that with the below command.
netstat -tnlp | grep 8080
Go to your browser and type http://localhost:8080/. Now you should able to see the Jenkins dashboard like below image.


Step – 2
Now we will setup Authentication for Jenkins. By default Jenkins allows “Anyone can do anything.”. Here we will setup Matrix based security authentication. For this follow the below steps:
Click on Manage Jenkins > Configure Global Security > Jenkins Own User Database > Allow Users to Signup > Save



Step – 3
After saving the above step then follow the below step to create an user:
Click on Signup button (Top right corner of the dashboard) > Fill all the required information > click on SignUp (Below the Form)


Step – 3
Now we will restrict User Signup and add “Matrix based security authentication”. Please follow the below steps:
Click on Manage Jenkins > Configure Global Security > Jenkins Own User Database > Unchek “Allow Users to Signup” > Matrix-based security > Put the user Name @ “User/group to add:” text box > click on add > Check all the Check boxes > Save


Now onwards User “Tapas Mishra” can administrate Jenkins after login. Also you can add more user to the db by following the below
Step – 4
Click on Manage Jenkins > Manage Users > Create U
ser
s
After
thi
s follow the Step – 3 again to add previliage to the new user.

Add nodes and configure Jobs to Start a Built Process -- Part II

I have described in my previous article about Jenkins and how to install and configure user account on it. In this article I will describe about how to configure nodes on Jenkins. Nodes are the servers where we will deploy our applications. You can configure n number of Nodes in your Jenkins applications depending on the server capacity.
Before configuring a node we have to add there credential first. Like user name and password for the node if the node is passwordbased authentication enabled. Else you can add the user name and private key if the node is passwordless authentication enabled.
How to add credentials
Log in to your Jenkins account. Click on the credential tab as shown in the below image.


Now Click on “Add domain” as shown in the below image.


Provide a user friendly name and some description to it and press “OK”


Now Click on “Add Credentials” tab then provide your information as shown in the below image.


After completing the Credential entry in the Jenknins now we will add the Node.
How to add Node on Jenkins
Go to the main dashboard of Jenkins Click on “Manage Jenkins” > Click on “Manage Nodes” from the List.
You will find a Node named “Master” in the node list which is the Jenkins server itself.
To add a new node click on “New Node”. Enter the information and click on as shown in the below image.


Now enter all the information about your host as shown in the below Image. Click on the advance and modify your port number if you are using any different port for your SSH.



It will take some time to Lunch the new node and then you will able to see the information about the node in your Jenkins Dashboard. See the below image.


Jenkins - Part III
In my previous articles (Jenkins Part I and Jenkins Part II) I have already described how to install Jenkins, how to Setup User account on Jenkins, how to Configure Nodes on Jenkins. In this article we will see how to configure a job on Jenkins. Basically the job will deploy an application from Github on a configured node. We will configure a simple Job in this part. We will just clone a github repository in a node using Jenkins. But in our later articles we will discuss about critical deployments.
We will complete this in three steps.
  • Install required Plugin
  • Configure Job
  • Build the Job and verify

Step 1 (Install required Plugin)

If we are using GIT as our Sour Code Management then we have to install plugin for it. The git plugin will help use to deploy the codes on a node. It’s very simple to configure. To install a plugin follow the steps as described below
Click on Manage Jenkins > Click on Manage Plugins > Click on Available Tab > Filter the Git Plugins > Chose the Git Plugin > Click on install without restart. > Chose Restart Jenkins when installation is complete option.

Step 2 (Configure Job)

Now we will create a Job for deploying an application on our previously configured server from github. To Create a new job Click on New Job > Give a Name to the Job > Choose “Build a free-style software project” option > Click on “Ok”
After getting the new window Check the option “Restrict where this project can be run“. You will get a text box to enter a node name where the build will be taken place (Setup a node following the steps in my previous article). Write the node name in my case it’s “www.linuxfunda.com“. If you will not check this option then Jenkins will clone your repository in Master itself.


http://i2.wp.com/www.linuxfunda.com/wp-content/uploads/2014/07/Jenkins-Job-2.png


Chose the Git Radio button from the “Source Code Management” section and write the Github Repository URL. In my case it’s “ https://github.com/tapasprivate/tapasprivate.git “.  I have described how to add Credentials in my previous article. Add the appropriate credential for the github account. Specify the branch name if you want any specific branch to clone else  it will clone “master” branch of the repository. There are also other advance options are available like git submodules which we will discuss in later articles.



Step 3 (Build the Job and Verify)

Now we have done with the configuration of the job. Save the Configuration and build the job. SSH to your node where you took the build. Go to the Workspace path and verify that your files are there from github. In my next article we will configure a job with shell script.


:)

Wednesday, July 8, 2015

Abstract Class And Methods Rules

There are few things we need to keep in mind when working with abstract methods and abstract classes.

The rules for abstract methods and abstract classes are:
  • A class can be marked as abstract with out containing any abstract method. But if a class has even one abstract method, then the class has to be an abstract class.
abstract class A
{
    // Valid, even with out any abstract methods
}

class B // Invalid, class B should be abstract, since it has abstract method.
{
    abstract void method1();
}
  • An abstract class can have one or more abstract methods.
abstract class C
{
    abstract void method1();

    abstract double method2(int x, int y);

    abstract boolean method3(char z);
}
  • An abstract class can have both abstract and non abstract (or concrete) method.
abstract class D
{
    void method1()
    {
        System.out.println("I am a concrete method");
    }

    abstract double method2(int x, int y);

    int method3(double z)
    {
        System.out.println("I am also a concrete method");
    }

    abstract boolean method4(char z);
}
  • The abstract method should not have method body. Even empty flower braces { } are not allowed.
abstract class A
{
    abstract void method1(); // Valid

    abstract void method2() {} // Invalid - since it has method body

}
  • Any sub-class extending from an abstract class should either implement all the abstract methods of the super-class or the sub-class itself should be marked as abstract.
abstract class A
{
    abstract void method1();

    abstract void method2();

}

class B extends A
{
    // Invalid since B does not implement the abstract methods
}

abstract class C extends A
{
    // Valid since C is marked as abstract, even though the abstract methods are not implemented, 
}

class D extends A
{
    void method1()
    {
        System.out.println("Method1 implemented here.");
    }

    // Invalid, class D should be marked as abstract, since method2 is not implemented.
}

abstract class E extends A
{
    void method1()
    {
        System.out.println("Method1 implemented here.");
    }

    // Even though method2 is not implemented, class D is marked as abstract, so it is Valid.
}


class F extends A
{
    // Valid since both methods are implemented here.
    void method1()
    {
        System.out.println("Method1 implemented here.");
    }

    void method2()
    {
        System.out.println("Method2 implemented here.");
    }
}
  • If an abstract class contains multiple methods, it is not necessary that all the methods of the abstract class are implemented in the immediate sub-class. Few of them can be implemented in sub-sub-classes or any where else in the sub-class hierarchy. But for a class to be concrete, all the abstract methods in its super-class must be implemented.
abstract class X
{
    abstract void method1();
    abstract void method2();
}

abstract class Y extends X
{
    void method1()
    {
        System.out.println("Method1 implemented here.");
    }
}

class Z extends Y
{
    void method2()
    {
        System.out.println("Method2 implemented here.");
    }
}
  • It is not necessary to add the abstract methods only in the super most class, we can add more abstract methods in the sub-classes.
abstract class X
{
    abstract void method1();
}

abstract class Y extends X
{
    abstract void method2();
}

class Z extends Y
{
    void method1()
    {
        System.out.println("Method1 from class X implemented here.");
    }

    void method2()
    {
        System.out.println("Method1 from class Y implemented here.");
    }
}

What is ConcurrentHashMap-identityHashMap-WeakHashMap ?

Q) What is ConcurrentHashMap? 
Ans) A concurrentHashMap is thread-safe implementation of Map interface. In this class put and remove method are synchronized but not get method. This class is different from Hashtable in terms of locking; it means that hashtable use object level lock but this class uses bucket level lock thus having better performance. 

Q) What is identityHashMap?
Ans) The IdentityHashMap uses == for equality checking instead of equals(). This can be used for both performance reasons, if you know that two different elements will never be equals and for preventing spoofing, where an object tries to imitate another.
Q) What is WeakHashMap?
Ans) A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.

Soft references act like a data cache. When system memory is low, the garbage collector can arbitrarily free an object whose only reference is a soft reference. In other words, if there are no strong references to an object, that object is a candidate for release. The garbage collector is required to release any soft references before throwing an OutOfMemoryException.

Weak references are weaker than soft references. If the only references to an object are weak references, the garbage collector can reclaim the memory used by an object at any time. There is no requirement for a low memory situation. Typically, memory used by the object is reclaimed in the next pass of the garbage collector

Monday, July 6, 2015



Continuous Integration Environment for Java with Jenkins, Maven, SVN, and IntelliJ IDEA


Continuous Integration is a practice that does not necessarily require specialized CI server software. You know for sure some magicians, who are able to write amazing shell scripts in less than a few hours, that calls the build, makes the tests, deploys status emails with links to nicely formatted reports and run it periodically at fixed times as Cron job. But not everybody is such a shell script wizard. For the rest of us, there is CI server software like Jenkins.
Regardless what way you take, you need a build process, that can be initiated by command line. The same applies to your tests. There are several build tools; the usual suspects in the Java world being Ant, Maven and meanwhile Gradle. Sometimes there are heated debates, which is the better tool. This is certainly not the case here, although I personally have a slight tendency towards Maven for several reasons.
The next important component is a version control system, in this example Subversion is used.
The screencast is divided into the following sections:
– Some basic configuration of Jenkins
– Quick setup of SVN
– Configure quickly a very simple Maven project on the command line
– Import the Maven project into IntelliJ IDEA and link it to the SVN repository
– Create a Jenkins Job, that polls every 5 minutes the SVN repository for changes
– Let a JUnit test fail and show what happens
– Add a dependency on log4j to Maven’s pom.xml and show the interaction between Maven, IntellliJ and Subversion
The actual project is nothing fancy, just a dummy Java Swing application.
But the same principles apply to more complicated scenarios, e.g. when working with a JEE stack. IMHO these are scenarios in which Maven can play its strengths.
Good Video!!!