Monday, November 23, 2015

IBM Websphere MQ clustering - load balancing

These are simple steps on how to cluster IBM Websphere MQ. Clustered MQ requires MQ Manager as gateway and set of MQ Managers as consumers of message. 
Clustering helps improve performance since process can be spread across different applications. Vertical clustering can be used for high-specification machines and horizontal for low-specification machines.

Step 1:

Create gateway queue manager GATEWAYMGR. This is the queue manager that external application will use when sending a message. 




















































Step 2: 
Create two or more MQ manager consumers, e.g. ConsumerMgr1 and ConsumerMgr2.







































Step 3:
Create cluster queue manager, e.g. CLUSTERMGR. Select GATEWAYQMGR as first full repository and ConsumerMgr1 as second full repository.































Step 4:
Create partial repository. Right-click on cluster manager and add queue manager ConsumerMgr2.




























Step 5:
Create local queue to in both ConsumerMgr1 and ConsumerMgr2. 































Step 6:
Create local queue in GATEWAYQMGR with attribute SYSTEM.CLUSTER.TRANSMIT.QUEUE. Message sent to Q.LOC of GATEWAYQMGR will go to either ConsumerMgr1 or 2.




Saturday, November 21, 2015

Clustering/Load balancing using Apache HTTP server

Simple step to cluster Web Service using Apache HTTP server. The HTTP server act as a load balancer for 2 or more resources.









A. Ensure that the following modules are enabled.
  LoadModule actions_module modules/mod_actions.so
  LoadModule alias_module modules/mod_alias.so
  LoadModule allowmethods_module modules/mod_allowmethods.so
  LoadModule asis_module modules/mod_asis.so
  LoadModule auth_basic_module modules/mod_auth_basic.so
  LoadModule authn_core_module modules/mod_authn_core.so
  LoadModule authn_file_module modules/mod_authn_file.so
  LoadModule authz_core_module modules/mod_authz_core.so
  LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
  LoadModule authz_host_module modules/mod_authz_host.so
  LoadModule authz_user_module modules/mod_authz_user.so
  LoadModule autoindex_module modules/mod_autoindex.so
  LoadModule cgi_module modules/mod_cgi.so
  LoadModule dir_module modules/mod_dir.so
  LoadModule env_module modules/mod_env.so
  LoadModule include_module modules/mod_include.so
  LoadModule info_module modules/mod_info.so
  LoadModule isapi_module modules/mod_isapi.so
  LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
  LoadModule log_config_module modules/mod_log_config.so
  LoadModule mime_module modules/mod_mime.so
  LoadModule negotiation_module modules/mod_negotiation.so
  LoadModule proxy_module modules/mod_proxy.so
  LoadModule proxy_balancer_module modules/mod_proxy_balancer.so 
  LoadModule proxy_html_module modules/mod_proxy_html.so
  LoadModule proxy_http_module modules/mod_proxy_http.so
  LoadModule setenvif_module modules/mod_setenvif.so
  LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
  LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
  LoadModule ssl_module modules/mod_ssl.so
  LoadModule status_module modules/mod_status.so
  LoadModule xml2enc_module modules/mod_xml2enc.so


B. Add httpd-proxy-balancer.conf.
  Include conf/extra/httpd-proxy-balancer.conf

C. Create httpd-proxy-balancer.conf inside extra folder.
<VirtualHost *>
    #--- Set to Off since it is not request forwarder.
     ProxyRequests Off
    #--- Display load balance status.
     ProxyStatus On
    #--- Not passing request to proxied host.
     ProxyPreserveHost Off
     ProxyPass /balancer-manager !
     ProxyPass /server-status !
    #-- Balancer cluster name.
     ProxyPass / balancer://mycluster/ STICKYSESSION=mysticky_session nofailover=Off
    #-- Logging.
     CustomLog C:/Apache24/logs/balancer_log.txt combined
    #-- Use as gateway.
     ProxyPassReverse / balancer://mycluster
 
    #-- Cluster / balancer.
     <Proxy balancer://mycluster>
      BalancerMember http://{hostname}:{port} route=first loadfactor=5
      BalancerMember http://{hostname}:{port} route=second loadfactor=5
    </Proxy>
 
    <Location /balancer-manager>
     SetHandler balancer-manager
    </Location>
 
    <Location /server-status>
     SetHandler server-status
    </Location>
</VirtualHost>
D. Start Apache HTTP server.
/bin/httpd.exe
E. Access the application via port 80 if using default port.
http://{host name}:{port}/{app web-context}
F. Status of balancer can be checked from accessing Balance Manager URL.
  http://{hostname}:{port}/balancer-manager

Monday, September 14, 2015

[Java] Stored procedure call for AS400

Sample code to call SQL stored procedure in IBM iDB2 for i.

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;


public class TestProcedureCall{


 public static void main(String[] args) {

  String storedProcedure = "{ CALL MYPROC (?, ?, ?) }";
  byte[] blankByte = " ".getBytes();

  try{
   Class.forName("com.ibm.as400.access.AS400JDBCDriver");
   Connection con = DriverManager.getConnection(
     "jdbc:as400://LOCALHOST", "USER1", "PASSWORD1");
   
   CallableStatement  stmt = 
     con.prepareCall(storedProcedure);

  stmt.setString(1,"ROBIN"); //Name
   stmt.setBytes(2,blankByte); //Nothing
   stmt.setInt(3,30); //Age

  stmt.registerOutParameter(1,Types.CHAR);
   stmt.registerOutParameter(2,Types.CHAR);
   stmt.registerOutParameter(5,Types.DECIMAL);

ResultSet rs = callableStatement.executeQuery();
   ResultSetMetaData rsMetaData = rs.getMetaData();
   int colCount = rsMetaData.getColumnCount();

   for (int i = 1 ; i <= colCount ; i++){
    System.out.println(rsMetaData.getColumnName(i));
  }

   rs.close();
   callableStatement.close();
   con.close();

  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }

 }//End main

}//End class

Monday, September 7, 2015

[Java] IBM AS400 JDBC Call

Sample AS400 JDBC class.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class AS400Query {

 public static void main(String[] args) {

ArrayList<String> arl = new ArrayList<String>();

try {
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
Connection con =   
       DriverManager.getConnection("jdbc:as400://<server>", 
       "<user>", "<password>");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT <col> FROM <table>");

while (rs.next()){
arl.add(rs.getString(1));
}

rs.close();

for(String s : arl){
String sql = " UPDATE <table> SET COL= " + s;
System.out.println(sql);
stmt.execute(sql);
}

stmt.close();
con.close();

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}


}


}

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