Wednesday, July 8, 2015

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!!!

Tuesday, June 30, 2015

DispatchAction Functionality in Struts 2

In Struts 1 DispatchAction helps us in grouping a set of related functions into a single action. In Struts 2 all the Actions by default provide this functionality. To use this functionality we need to create different methods with the similar signature of the execute() method.

In our example the CalculatorAction class has all the methods related to a Calculator like add(), multiply(), division() and subtract() which are used for performing mathematical operation on two numbers and it displays the result in a single jsp based on the action button clicked in jsp.

 
Action Class

package com.simplecode.action;

import com.opensymphony.xwork2.Action;

public class CalculatorAction implements Action
{      
private float number1;
private float number2;
private float result;
private String methodName;

       
public String execute()
       
{
                number1
= 0;
                number2
= 0;
                result
= 0;
                setMethodName
("execute Method");
               
return SUCCESS;
       
}

       
public String add()
       
{
                result
=number1+number2;
                setMethodName
("add Method");
               
return "add";
       
}

       
public String subtract()
       
{
                result
= number1 - number2;
                setMethodName
("subtract Method");
               
return "subtract";
       
}

       
public String multiply()
       
{
                result
= number1 * number2;
                setMethodName
("multiply Method");
               
return "multiply";
       
}

       
public String divide()
       
{
               
if(number2!=0)
                        result
= number1/number2;
               
else if(number1!=0)
                        result
= number2/number1;
               
else
                        result
=0;
               
                setMethodName
("divide Method");
               
return "divide";
       
}

       
public float getNumber1() {
               
return number1;
       
}

       
public void setNumber1(float number1) {
               
this.number1 = number1;
       
}

       
public float getNumber2() {
               
return number2;
       
}

       
public void setNumber2(float number2) {
               
this.number2 = number2;
       
}

       
public float getResult() {
               
return result;
       
}

       
public void setResult(float result) {
               
this.result = result;
       
}

       
public String getMethodName() {
               
return methodName;
       
}

       
public void setMethodName(String methodName) {
               
this.methodName = methodName;
       
}
}

Struts.xml

Here in order to do a DispatchAction functionality we need to create separate action mapping for each method in the action class, which is done in the struts.xml as shown below.
 
 
<!DOCTYPE struts PUBLIC
 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
 
 
<package name="default" extends="struts-default">
 
 
 
        <action name="Number" class="com.simplecode.action.CalculatorAction">
 
                <result name="success">/curd.jsp
 
        </action>
 
        <action name="addNumber" method="add"
 
                class="com.simplecode.action.CalculatorAction">
 
                <result name="add">/curd.jsp
 
        </action>
 
        <action name="multiplytNumber" method="multiply"
 
                class="com.simplecode.action.CalculatorAction">
 
                <result name="multiply">/curd.jsp
 
        </action>
 
        <action name="subtractNumber" method="subtract"
 
                class="com.simplecode.action.CalculatorAction">
 
                <result name="subtract">/curd.jsp
 
        </action>
 
                <action name="divideNumber" method="divide"
 
                class="com.simplecode.action.CalculatorAction">
 
                <result name="divide">/curd.jsp
 
        </action>
 
 
 
</package>
 
</struts>



Here not
e that we use the same action class in all the action mappings but only the method name differs. So now When the request URL is “Number” the execute() method in the CalculatorAction class will be executed. When the request URL is “addNumber” the add() method in the CalculatorAction class will be invoked, this is specified using the method attribute of the action tag in struts xml. Similarly for subtract, multiply and divide request the subtract() ,multiply() and divide() methods will be invoked respectively.

This way configuring a separate action mapping for each method of same action class can be avoided by using a feature called Dynamic Method Invocation. We will learn about this in our upcoming tutorial.
 
Jsp

In the curd.jsp page we create five different buttons to invoke the different methods in the CalculatorAction class.
 
<%@taglib uri="/struts-tags" prefix="s"%>





Dispatch Action










On running the example the following page will be displayed in the browser. Now when the user click a button the appropriate method in the CalculatorAction class gets invoked.
 


For example When the user clicks the add button the add() method in the CalculatorAction class gets executed and the following page is displayed.
DispatchAction
 


 
Note: Dispatch action functionality is used when you have multiple submit buttons in a single form.

 Good One.