2 Minutes To Code (β Version)

JVisualVM - 2 minute tutorial

Typical Code : Source code of app that we will analyze using jvisualvm in this tutorial

package com.techfundaes.workshopBag;

import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;

public class Launcher
{
	static MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    
    public static void main(String[] args)
    {
    	try
        {
            for(int i = 0; i <= 9; i++)
    		{
    			Workshop workshop = new Workshop();
    			Thread thread = new Thread(workshop);
    			workshop.thread = thread;
    			thread.setName("Workshop-"+i);
    			thread.start();
    			
    			//Register as JMX Bean
    			ObjectName workshopName = new ObjectName("WorkshopGroup:type=Workshops,name=Workshop-"+i);
    			server.registerMBean(workshop, workshopName);
    	    }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
	}
}


package com.techfundaes.workshopBag;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.management.AttributeChangeNotification;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;

public class Workshop extends NotificationBroadcasterSupport implements WorkshopMBean, Runnable
{
	
	long localCarsCount = 0;
	int batchSize = 5;
	long sleepInterval = 1000;
	Thread thread = null;
	int sequenceNumber = 0;
	List<Car> paintedCarsList = new ArrayList<Car>();
	
	public void run()
	{
		while(true)
		{
			paintCars();
		}
	}

	public void paintCars()
	{
		for(int i = 0; i < batchSize; i++)
		{
			Car car = new Car(Calendar.getInstance().getTime(), thread.getName());
			paintedCarsList.add(car);
			localCarsCount += 1;
		}	
		try { Thread.sleep(sleepInterval);}catch(Exception e){e.printStackTrace();}
	}
	
	public long getLocalCarsCount()
	{
		return localCarsCount;
	}
	
	public String stopPaintingCars()
	{
		sleepInterval = 100000000000000l;
		return "Stopped Painting Cars";
	}
	
	public String startPaintingCars()
	{
		sleepInterval = 1000;
		thread.interrupt();
		return "Started Painting Cars";
	}
	
	public int getBatchSize()
	{
		return batchSize;
	}
	
	public String setBatchSize(int batchSize)
	{
		this.batchSize = batchSize;
		Notification n = new AttributeChangeNotification(this, sequenceNumber++, System.currentTimeMillis(), "CacheSize changed", "CacheSize", 
				"int", batchSize, batchSize);

		sendNotification(n);
		return "batch size set to "+ batchSize + ".";
	}
	
	public String setBatchSizeAndSleepTime(int batchSize, long sleepInterval)
	{
		this.batchSize = batchSize;
		this.sleepInterval = sleepInterval;
		return "batch size set to "+ batchSize + ", sleepInterval to "+sleepInterval +" ms";
	}
	
	
}


package com.techfundaes.workshopBag;

public interface WorkshopMBean
{
	public long getLocalCarsCount();
	public String stopPaintingCars();
	public String startPaintingCars();
	public int getBatchSize();
	public String setBatchSize(int batchSize);
	public String setBatchSizeAndSleepTime(int batchSize, long sleepInterval);
}



package com.techfundaes.workshopBag;

import java.util.Date;


public class Car
{
	private Date paintDate = null;
	private String paintShop = null;
	
	public Car(Date paintDate, String paintShop)
	{
		this.paintDate = paintDate;
		this.paintShop = paintShop; 
	}
}