Home     RSSRSS

Posts Tagged ‘Java’

Monitoring Java based Servers with JMX

January 5, 2014 by kiranbadi1991 | 1 Comment | Filed in Development, Environment, Performance Center, Performance Engineering, Web Server

Many times I have seen people struggling to do monitoring set up on the Java based servers like Web logic , Web sphere or even commercial servers like Tibco etc. Any server which supports Java based application can be monitored with JMX Solutions. Even the commercial tools like Sitescope, Silk Performer Monitors use the Custom JMX Client classes behind the scenes to monitor the java based servers.

JMX provides the powerful API to monitor and manage any Java based application servers or infrastructure. Below are some  benefits which I have seen we get by using the JMX API,

  1. Using the JMX we can monitor the health of all servers in the environment. Even if we have non Java based servers , we can always  write the custom wrapper classes which can pull the performance stats from those servers. However this approach consumes some time, but is doable if we have skilled java resources. However if we have Java based infrastructure than probably other than enabling JMX , there is hardly any code is required.
  2. Configuring Resources on the server is also possible with JMX API. With JMX API we can monitor and configure the application and its services either fully or in part by exposing it to the JMX API.
  3. JMX also helps in debugging. Debugging can be enabled at runtime in case if required.
  4. JMX can also send out the alert notification, emails for critical events.However a listener needs to be Configured to send out the actual data.

Setting the JMX Based monitoring on the java based server involves the enabling the JMX Port in the start up option of the server. Normally it’s the flag which needs to be set up in java options of the server.

In this post I will show you as how to do set up in Apache Tomcat 7x server. I have installed the Apache Tomcat on my local machine as windows service.Once you have installed Tomcat as service, navigate to the bin directory of the server and right click tomcat7w.exe file. Please make sure that you run this with administrator rights.

image

In apache Tomcat 7 Properties windows, click on Java tab, you need to enter below options for local monitoring solutions.Please note that with below options we have turned off SSL and authentication is also switched off which means that you don’t need user id and password to connect to the server.

-Dcom.sun.management.jmxremote=true 
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false

For remote monitoring of the server we need to enable remote port for jmx. Below options I have shown as how to set JMX Monitoring with user credentials, however if you don’t need user credentials, then probably, you can use options jmxremote.authenthicate as false.

-Dcom.sun.management.jmxremote.port=(your port number)
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.password.file= path to password file

Below screenshot shows you the setting I have given in my tomcat server. Since JMX is based on JMX Specification, set up is pretty should be similar in other java based servers. However in those servers, we might set the java options in class path options while starting the servers.

image

Once the JMX options is enabled, we can use any JMX Client to connect to the server and gather the performance metrics. However please do note that application and server need to expose its functionality via Mbeans. Below screenshots shows the connection with JConsole,

image

Enabling the JMX Options is the first step you need to do if ever you want to monitor any java based servers with JMX.

Lot of people also say that enabling the JMX is cumbersome process, but my experience was bit different and after enabling it did not break any thing on my environment set up.

Technorati Tags: ,,

Tags: , ,

Unit Tests Test Case – Is My Code Output Correct

November 20, 2013 by kiranbadi1991 | Comments Off on Unit Tests Test Case – Is My Code Output Correct | Filed in Development, Others, Testing

One of the goals for writing unit tests is to ensure that code does what it is supposed to do. When we write and compile the code, we know it gets through perfectly without any errors. However not having any errors do not necessarily mean that it does what it’s supposed to do. We need to have some mechanism to know that the code produces the results we require and we also have other means to validate the code output. Your test case for Unit test should test for results or code output validity.

Consider the below simple program add 2 numbers (I know this example is too easy and not practical one ,however this is best example coming to my mind now while writing this post,sorry I do have some great unit tests for my piece of code, I will share those as well later on)

public class AddTwoNumbers {

public double add (double number1, double number2) {

Return number1 + number2;

       }

   }

The Intended purpose of AddTwoNumbers is to take 2 double values and do the addition. We need some mechanism to know that when this program gets executed we are indeed doing addition and program is also not giving us back any runtime exception. Probably we can write the test for this program something like below,

public class AddTwoNumbersTest {

public static void main(String[] args) {

AddTwoNumbers addtwonum = new AddTwoNumbers();

double output = addtwonum.add(40,50);

if (output != 90) {

System.out.println("Bad result: " + result);

                      }

                }

    }

One of the other ways to test this program is with Junit and use its assertEquals Method.

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class AddTwoNumbersTest {

@Test

public void testAddTwoNumbers() {

AddTwoNumbers addtwonum = new AddTwoNumbers();

double output = addtwonum.add(40,50);

assertEquals(60, result, 0);

            }

 }

Junit probably is one of the best library for unit testing the java code. Probably sometimes later I will write more on Junit. In case while writing the code ,the requirements are not clear, then probably making some assumptions surely helps. However we need to ensure that our assumptions are repeatedly clarified with users or relevant stakeholders.

So the first unit test case for your piece of code should be to verify that your code does what it is supposed to do.


 

Tags: , , , ,

Know your Default Initial and Max heap size of JVM

June 7, 2012 by kiranbadi1991 | Comments Off on Know your Default Initial and Max heap size of JVM | Filed in Development, Environment, Memory, Others, Performance Engineering

At times it becomes necessary that we know the default heap size allocated to the JVM in order to debug some issues, for those cases, I suggest run the below command on the command line of the server box to get this information,

java -XX:+PrintCommandLineFlags -version

 

On my machine, where I have tomcat server installed, I get the information something like,

 

C:\Users\kiran>java -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=16777216 -XX:MaxHeapSize=268435456 -XX:+PrintCommandLineFlag
s -XX:-UseLargePagesIndividualAllocation
java version “1.6.0_32”
Java(TM) SE Runtime Environment (build 1.6.0_32-b05)
Java HotSpot(TM) Client VM (build 20.7-b02, mixed mode, sharing)

 

Tags: , , ,