Wednesday, August 12, 2015

JMeter Web Service Setup

Example of calling Web Service using JMeter. The tool can be downloaded from this link - http://jmeter.apache.org/.

Run jmeter.bat and add SOAP Sampler.







Set up Thread Group.




Add SOAP Message.




SQL developer client configuration

Setting up SQL Developer connection to Oracle database. Click '+' to add new connection.







[Java] Reading Map collection

Sample code to read Map collection entries.


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

public class ReadMap {


public static void main(String[] args) {
   Map<String, String> m = new HashMap<String,String>();
   m.put("One","Number one");
   m.put("Two","Number two");
   m.put("Three","Number three");

   long st = System.currentTimeMillis();

   //#1
   for (Map.Entry<String, String> entry : m.entrySet()) 
   {
      System.out.println(">>Key = " + entry.getKey() 
          + ", Value = " + entry.getValue());
   }
   System.out.println(">>#1 - Run time ::: " +
     (System.currentTimeMillis() - st) + "\n" );

//#2
   st = System.currentTimeMillis();
   for (String key : m.keySet()) 
   {
      String value = m.get(key);
      System.out.println(">>Key = " + key 
         + ", Value = " + value);
   }
   System.out.println(">>#2 - Run time ::: " 
      + (System.currentTimeMillis() - st)  + "\n" );


//#3
  st = System.currentTimeMillis();
for (String key : m.keySet()) 
{
System.out.println(">>Key = " + key);
}
System.out.println(">>#3 - Run time ::: " 
     + (System.currentTimeMillis() - st)  + "\n" );

  //#4 st = System.currentTimeMillis();
for (String value : m.values()) 
{
System.out.println(">>Value = " + value);
}
System.out.println(">>#4 - Run time ::: " 
     + (System.currentTimeMillis() - st)  + "\n" );

//#5
st = System.currentTimeMillis();
Iterator<Map.Entry<String, String>> iter = 
     m.entrySet().iterator();
while (iter.hasNext()) 
{
   Map.Entry<String, String> entry = iter.next();
   System.out.println(">>Key = " + entry.getKey() 
          + ", Value = " + entry.getValue());
}
System.out.println(">>#5 - Run time ::: " 
     + (System.currentTimeMillis() - st)  + "\n" );

//#6
st = System.currentTimeMillis();
Iterator iter1 = m.entrySet().iterator();
while (iter1.hasNext()) 
{
   Map.Entry entry = (Map.Entry) iter1.next();
   String key = (String)entry.getKey();
   String value = (String)entry.getValue();
   System.out.println("Key = " + key + ", Value = " + value);
}
System.out.println(">>#6 - Run time ::: " 
     + (System.currentTimeMillis() - st)  + "\n" );

}//End main method


}//End class



This is the output of the class when run.

>>Key = One, Value = Number one
>>Key = Two, Value = Number two
>>Key = Three, Value = Number three
>>#1 - Run time ::: 1

>>Key = One, Value = Number one
>>Key = Two, Value = Number two
>>Key = Three, Value = Number three
>>#2 - Run time ::: 1

>>Key = One
>>Key = Two
>>Key = Three
>>#3 - Run time ::: 0

>>Value = Number one
>>Value = Number two
>>Value = Number three
>>#4 - Run time ::: 1

>>Key = One, Value = Number one
>>Key = Two, Value = Number two
>>Key = Three, Value = Number three
>>#5 - Run time ::: 1

Key = One, Value = Number one
Key = Two, Value = Number two
Key = Three, Value = Number three

>>#6 - Run time ::: 0

[Java Swing] DialogBox with Multiple Fields

Below are sample code on how to use dialog box as configuration window. It will require having multiple fields in dialog box. Below is the example on how to do it in Android.

Create a panel that will contain the panel that has the multiple fields.


//Create setup panel.
JPanel settingUITopPanel = new JPanel(new BorderLayout());
JPanel senderPanel = new JPanel();
senderPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("JMS SENDER"));
settingUITopPanel.add(senderPanel, BorderLayout.NORTH);
JPanel listenerPanel = new JPanel();
listenerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("JMS LISTENER"));
settingUITopPanel.add(listenerPanel, BorderLayout.SOUTH);
GroupLayout layout = new GroupLayout(senderPanel);
senderPanel.setLayout(senderSetup(layout));
GroupLayout layout1 = new GroupLayout(listenerPanel);
listenerPanel.setLayout(listenerSetup(layout1));

//Displa dialog box with multiple fields.

int result = JOptionPane.showConfirmDialog(null,settingUITopPanel,"Setting",JOptionPane.OK_CANCEL_OPTION);

if(result == JOptionPane.OK_OPTION){

   //Get fields value here...
}


Method that will return sender setup layout.

private GroupLayout senderSetup(GroupLayout layout){
   JLabel l1 = new JLabel("MQ Host Name");
   JLabel l2 = new JLabel("MQ Port Number");
   JLabel l3 = new JLabel("MQ Manager");
   JLabel l4 = new JLabel("MQ Queue");
   JLabel l5 = new JLabel("MQ Channel");
   JLabel l6 = new JLabel("MQ Username");
   JLabel l7 = new JLabel("MQ Password");
   layout.setAutoCreateGaps(true);
   layout.setAutoCreateContainerGaps(true);
   GroupLayout.SequentialGroup hGroup = 
      layout.createSequentialGroup();
   hGroup.addGroup(layout.createParallelGroup().
   addComponent(l1).
   addComponent(l2).
   addComponent(l3).
   addComponent(l4).
   addComponent(l5).
   addComponent(l6).
   addComponent(l7));

   hGroup.addGroup(layout.createParallelGroup().

   addComponent(mqSenderHost).
   addComponent(mqSenderManager).
   addComponent(mqSenderQueue).
   addComponent(mqSenderChannel).
   addComponent(mqSenderUser).
   addComponent(mqSenderPassword)
            );
   layout.setHorizontalGroup(hGroup);

   GroupLayout.SequentialGroup vGroup = 
      layout.createSequentialGroup();
     vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l1).addComponent(mqSenderHost));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l2).addComponent(mqSenderPort));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l3).addComponent(mqSenderManager));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l4).addComponent(mqSenderQueue));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l5).addComponent(mqSenderChannel));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l6).addComponent(mqSenderUser));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l7).addComponent(mqSenderPassword));
   
   layout.setVerticalGroup(vGroup);
   
   return layout;

}//End method



Method that will return listener setup layout.

private GroupLayout listenerSetup(GroupLayout layout){
   JLabel l1 = new JLabel("Enable JMS Reader");
   JLabel l2 = new JLabel("MQ Host Name"); 
   JLabel l3 = new JLabel("MQ Port Number");
   JLabel l4 = new JLabel("MQ Manager");
   JLabel l5 = new JLabel("MQ Queue");
   JLabel l6 = new JLabel("MQ Channel");
   JLabel l7 = new JLabel("MQ Username");
   JLabel l8 = new JLabel("MQ Password");
   listenerEnabledCheckbox.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        if (!((JCheckBox) event.getSource()).isSelected()) {
        contentPane.remove(mainPanel);
        senderTa.setSize(40, 100);
        mainPanel = JPanelUtil.getJMSSenderOnlyPanel(senderTa);
        mainPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white));
        contentPane.add(mainPanel, BorderLayout.CENTER);
        frame.setSize(1000, 800);
        frame.setBounds(50, 50, 800, 600);
        frame.pack();
       
        listenBtn.setEnabled(false);
    suspendBtn.setEnabled(false);
   
        SetupUIUtil.enableListenerSetupFields(
        false,
        mqListenerHost,
        mqListenerPort,
        mqListenerManager,
        mqListenerQueue,
        mqListenerChannel,
        mqListenerUser,
        mqListenerPassword);
       
        if( jmsThread != null && jmsThread.isThreadAlive()){
            jmsThread.suspend();
            }
       
        } else {
        contentPane.remove(mainPanel);
        senderTa.setSize(20, 100);
        receiverTa.setSize(20, 100);
        mainPanel = JPanelUtil.getJMSSenderAndListenerPanel(senderTa, receiverTa);
        mainPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white));
        contentPane.add(mainPanel, BorderLayout.CENTER);
        frame.setSize(1000, 800);
        frame.setBounds(50, 50, 800, 600);
        frame.pack();
       
        listenBtn.setEnabled(true);
    suspendBtn.setEnabled(false);
   
        SetupUIUtil.enableListenerSetupFields(
        true,
        mqListenerHost,
        mqListenerPort,
        mqListenerManager,
        mqListenerQueue,
        mqListenerChannel,
        mqListenerUser,
        mqListenerPassword);
        }
    }
});
  layout.setAutoCreateGaps(true);
  layout.setAutoCreateContainerGaps(true);
  GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

  hGroup.addGroup(layout.createParallelGroup().
            addComponent(l1).
            addComponent(l2).
            addComponent(l3).
            addComponent(l4).
            addComponent(l5).
            addComponent(l6).
            addComponent(l7).
            addComponent(l8));
  hGroup.addGroup(layout.createParallelGroup().
            addComponent(listenerEnabledCheckbox).
            addComponent(mqListenerHost).
            addComponent(mqListenerPort).
            addComponent(mqListenerManager).
            addComponent(mqListenerQueue).
            addComponent(mqListenerChannel).
            addComponent(mqListenerUser).
            addComponent(mqListenerPassword));
  layout.setHorizontalGroup(hGroup);
   
  GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l1).addComponent(listenerEnabledCheckbox));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l2).addComponent(mqListenerHost));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l3).addComponent(mqListenerPort));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l4).addComponent(mqListenerManager));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l5).addComponent(mqListenerQueue));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l6).addComponent(mqListenerChannel));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l7).addComponent(mqListenerUser));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l8).addComponent(mqListenerPassword));
   
   layout.setVerticalGroup(vGroup);
   
   return layout;
}//End method




Monitor Log using Baretail

I am using baretail as log monitoring tool for Windows application. It is a handy tool when you want to monitor application server (e.g. Websphere) logs during startup, or during runtime. The tool works like 'tail' command in Unix or Linux. 

Baretail can be downloaded from this link - https://www.baremetalsoft.com/baretail/


To use baretail: open the log file in baretail window. Baretail monitor multiple logs; just 
drag and drop the file. New log is displayed in a new Tab.


Happy baretail'ing!

Tuesday, August 11, 2015

[Java] Reflection

Sample classes that uses reflection.

1. Create interface class to define a contract (optional).

interface FactoryInterface {

   public void hello();

}

2. Create implementation class.

public class FactoryImpl implements FactoryInterface {

   @Override
   public void hello() {
      System.out.println("Hello world!");
   }

}

3. Create main class.

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class FactoryTest {


   public static void main(String[] args) {

      try {
         final Class<?> c = Class.forName("FactoryImpl");
         final Object implClassInstance = c.newInstance();
         Method classMethod = implClassInstance.getClass().
                              getMethod("hello", null);
         classMethod.invoke(implClassInstance, null);

      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      } catch (SecurityException e) {
         e.printStackTrace();
      } catch (InstantiationException e) {
         e.printStackTrace();
      } catch (IllegalAccessException e) {
 e.printStackTrace();
      } catch (NoSuchMethodException e) {
 e.printStackTrace();
      } catch (IllegalArgumentException e) {
e.printStackTrace();
      } catch (InvocationTargetException e) {
e.printStackTrace();
      }

   }

}

Monday, August 10, 2015

[Java] Check if string is empty


Sample utility class to check if String object is empty or null.



public class EmptyStringCheck {


   public static boolean isStrNull(String str)
   {
      boolean isNull = false;

      if (str != null)
      {
         isNull = false;
      }
      else
      {
         isNull = true;
      }

      return isNull;

   }//End method


   public static boolean isStrEmpty(String str)
   {
      boolean isEmpty = false;

      if (isStrNull(str))
      {
         isEmpty = true;
      }
      else
      {
         if (!("".equalsIgnoreCase(str)))
 {
    isEmpty = false;
 }
 else
 {
    isEmpty = true;
 }
      }

      return isEmpty;

   }//End method


   //Main method.
   public static void main(String[] args) {

      String str = "Test";
      String str1 = "";

      System.out.println(">>Is empty :::" + isStrEmpty(str));
      System.out.println(">>Is empty ::: " + isStrEmpty(str1));

      //Or
      System.out.println(">>Is empty :::" + str.isEmpty());
      System.out.println(">>Is empty ::: " + str1.isEmpty());

   }//End main method




}//End class

[Java] String left and right padding

Below is a sample Java class that left or right pad a String. 



public class PadString {


   public static String leftPad(
String str, 
int totalSizeOfStr, 
String charToPad){

      String paddedString = null;
        
      //Check if String is null.
      if (str != null)
      {
         StringBuilder sb = new StringBuilder();

         while (sb.length() < 
             Math.abs(totalSizeOfStr - str.length()))
         {
            sb.append(charToPad);
         }

         paddedString = sb.toString() + str;
      }

      return paddedString;
   }// End method


   public static String rightPad(
String str, 
int totalSizeOfStr, 
String charToPad){

      String paddedString = null;

      if (str != null)
      {
         StringBuilder sb = new StringBuilder();
         sb.append(str);

         while (sb.length() <= totalSizeOfStr)
         {
            sb.append(charToPad);
         }

          paddedString = sb.toString();
       }

       return paddedString;
   }



   public static void main(String[] args) {
      String str = "1234";

//Left padding
      String paddedStr = leftPad(str, 10, "0");
      System.out.println(paddedStr);

//Right padding
      paddedStr = rightPad(str, 10, "0");
      System.out.println(paddedStr);

   }//End main method


}//End class

[Java] Split a String Using Guava API

Below is a sample Java class that split a String to specific length. The example uses Guava API which can be found from this link -  https://code.google.com/p/guava-libraries/



import org.apache.hadoop.thirdparty.guava.common.base.Preconditions;

import org.apache.hadoop.thirdparty.guava.common.base.Splitter;
import org.apache.hadoop.thirdparty.guava.common.collect.Iterables;

public class SplitString {


   //Main method    

   public static void main(String[] args) {
      String str = "The quick brown fox jumped 
                    over the lazy dog";
      int splitLength = 4; //Split to 4 characters

      Preconditions.checkArgument(splitLength > 0);

      String[] strArray  = 
       Iterables.toArray(
         Splitter.fixedLength(splitLength).split(str),
         String.class);

      //Print to verify

      for(String s : strArray){
         System.out.println(s);
      }

   }//End main method


}//End class