Friday, 2 October 2015

Finding missing queue in Rabbitmq

Following is the code to compare two queues of rabbitmq . By using following code you can find out missing queue in other queue .

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;

import org.apache.commons.codec.binary.Base64;
import org.eclipse.jetty.util.ajax.JSON;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.testng.annotations.Test;


public class GetQueuInfo {

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String url1="http://queue1:55672/api/definitions";
                       //Following queue is compared against above queue.
String url2="http://queue1:55672/api/definition";


Vector<String> q1=new Vector<String>();
Vector<String> q2=new Vector<String>();


GetQueuInfo qInfo=new GetQueuInfo();
String response=qInfo.getResponse(url1);


JSONParser parser=new JSONParser();
Object obj=parser.parse(response);

JSONObject json=(JSONObject)obj;
JSONArray jArr=(JSONArray)json.get("queues");

for(int i=0;i<jArr.size();i++)
{
JSONObject jObj=(JSONObject)jArr.get(i);
String name=jObj.get("name").toString();
q1.add(name);
// System.out.println(name);
}

///
response=qInfo.getResponse(url2);
parser=new JSONParser();
obj=parser.parse(response);

json=(JSONObject)obj;
jArr=(JSONArray)json.get("queues");

for(int i=0;i<jArr.size();i++)
{
JSONObject jObj=(JSONObject)jArr.get(i);
String name=jObj.get("name").toString();
q2.add(name);
//System.out.println(name);
}

System.out.println("*****************Missing queues************************");
for(String str:q1)
{
if(!q2.contains(str))
{
System.out.println(str);
}
}



} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

Wednesday, 9 July 2014

Find out all combinations of an array.

This is the java code to find out combinations of an array.

class Combinations
{
String DIM_ARR[]={"a","b","c"};
  public static void main (String args[])
        {
            Combinations combobj= new Combinations();
            combobj.combine(0);
        }
       
       
        private void combine(int start){
            for( int i = start;i< DIM_ARR.length; i++ ){
                tcVect.add(DIM_ARR[i]+",");
                    System.out.println(tcVect.toString());
                if ( i < DIM_ARR.length )
                combine( i + 1);
                tcVect.remove(tcVect.size()-1);
            }
        }
}

Tuesday, 8 July 2014

HashMap Sort by Value

Following is the sample code to sort hashmap by its value using custom comparator.


package code_snippet;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class SortByValueHashMap {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        HashMap<String, Integer> hMap=new HashMap<String, Integer>();
        hMap.put("a", 10);
        hMap.put("b", 1);
        hMap.put("c", 100);
        System.out.println("Without Sorting="+hMap);
       

Tuesday, 27 May 2014

Example of deadlock

Following is the simplest example of how deadlock occurs?. Following code will run forever since they are waiting for each other to release the lock.

public static void main(String[] args) {
        // TODO Auto-generated method stub
        final LinkedList<String> ll_1=new LinkedList<String>();
        final LinkedList<String> ll_2=new LinkedList<String>();
       

Usage of wait,notify and notifyAll.

Following code demonstrates the the wait,notifyAll working.I have created one linkedlist object.
  Working of code is as below:
  1.First thread is created and set priority to 5 and called wait method.It will be in waiting state until it receives notify .
  2.Created second thread and set priority to 6 and called wait method.It will be in waiting state until it receives notify .
  3.Create third thread and added one element to list and called notifyAll method. In this ,notifyAll method will notify to second thread because priority is higher than first thread.It executes second thread then first thread. Always wait,notify,notifyAll methods get called from synchronized blocks or methods.
=======================code starts==================================
public static void main(String[] args) {
        // TODO Auto-generated method stub
        final LinkedList<String> ll=new LinkedList<String>();
        new Thread(new Runnable() {

Friday, 7 February 2014

Keyword driven automation framework tool

Hello guys, i have developed one tool for automation using java swing in front end and on top of webdriver.

Please download and use it, and let me know if you face any issue.

ReadMe
Executable

Monday, 7 October 2013

Static method and variables

This is one of the very important topics of core Java. In this post i am going to explain you about static in Java. First and very important point is you can access static methods and variables without creating an object of class. You can access static variables and methods by using classname.static_member.

Ex.
  class StatEx
  {
    static int i;
    void fun()
   {
      System.out.println(StatEx.i);
   }
  }

In above example you can notice that i have accessed i variable by using className (StatEx.i). And same you can do with the static member function.

Some of the important features of static ;
  • Static members can be accessed without creating an object of class.
  • Static function can access only static methods/variables.
  • You can initialize static members in static block .It gets executed only once when class loads.
  • Static variables also called as class variable.
  • Outer class or main class can not be static but you can declare inner class as static.
  • Static method can not be overridden.
  • Binding happens at compile time.
  • In java we have one public static void main method which is also static, an entry point for execution.