Summary: Space Worker Dump Example

Simple Space Dump Example Using IWorker

You can  download a simple IWorker example.
Extract the attached into:
<GigaSpaces Root>\examples\Advanced\Integration_Plugins
and run:
<GigaSpaces Root>\examples\Advanced\Integration_Plugins\iworker\startAll.bat

See the \iworker\config\schemas space schema with the following worker definition:

<workers>
        <worker-names>MyWorker</worker-names>
        <!--interrupt the worker on shutdown-->
        <interrupt>false</interrupt>
		<MyWorker>
           <enabled>true</enabled>
           <class-name>worker.MyWorker</class-name>
           <arg></arg>
           <description>MyWorker</description>
           <active-when-backup>false</active-when-backup>
           <shutdown-space-on-init-failure>true</shutdown-space-on-init-failure>
           <instances>1</instances>
        </MyWorker>
   </workers>

The Worker Implementation:

package worker;
import com.j_spaces.core.IJSpace;
import com.j_spaces.worker.IWorker;

public class MyWorker implements IWorker {

	public void init(IJSpace space, String arg1, String arg2) throws Exception {
		System.out.println("  ************ MyWorker init()");
		System.out.println("  ************ MyWorker space:" + space);
	}

	public void close() {
		System.out.println(" ************  MyWorker close()");
	}

	public void run() {
		System.out.println("  ************  MyWorker run()");
	}
}

When the space is started, the following should be displayed:

************ MyWorker init()
************ MyWorker space:com.j_spaces.core.client.JSpaceProxy@ededef6e
************ MyWorker run()

The Space Thread Dump Worker Example

This Worker can be configured to dump the stack trace, once the used memory is above a defined percentage. The threshold percentage is configured via the worker <arg> element:

<DebugWorker>
	<enabled>true</enabled>
	<class-name>com.gigaspaces.util.DebugWorker</class-name>
	<arg>70</arg>
	<description>DebugWorker </description>
	<!-- default is true -->
	<active-when-backup>true</active-when-backup>
	<!-- default is false -->
	<shutdown-space-on-init-failure>false</shutdown-space-on-init-failure>
	<!-- default is 1 -->                                                                               
	<instances>1</instances>
</DebugWorker>

The Worker code is illustrated below:

package com.gigaspaces.util;

import java.util.Date;
import java.util.Map;

import com.j_spaces.core.IJSpace;
import com.j_spaces.worker.IWorker;

public class DebugWorker implements IWorker {

	Runtime runtime = Runtime.getRuntime();
	long _maxMemory ;
	int	threshold = 80;
	public static void main(String[] args) {
		try {
			new DebugWorker().init(null, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	   private double getMemoryUsageRate( )
	   {
	      return ((runtime .totalMemory() - runtime .freeMemory())* 100.0) / _maxMemory ;
	   }  

	public void init(IJSpace arg0, String arg1, String arg2) throws Exception {
		try{
			threshold = Integer.parseInt(arg2);
		}
			catch (Exception e)
		{}
	}

	public void close() {
	}

	public void run() {
		_maxMemory = runtime.maxMemory();
		while (true) {
			long freeMemory = runtime.freeMemory();
			long maxMemory = runtime.maxMemory();
			long totalMemory = runtime.totalMemory();

			double usedMemoryPercentage =getMemoryUsageRate();

			System.out.println(new Date(System.currentTimeMillis()) +  
			" threshold[%]:" + threshold +" freeMemory :" + freeMemory + 
			" maxMemory :" + maxMemory + 
			" totalMemory :" + totalMemory + " 
			usedMemoryPercentage :" + usedMemoryPercentage );

			if (usedMemoryPercentage  > threshold) 
			{
				Map<Thread, StackTraceElement[]> st = Thread.getAllStackTraces();
	
				for (Map.Entry<Thread, StackTraceElement[]> e : st.entrySet()) {
					StackTraceElement[] el = e.getValue();
					Thread t = e.getKey();
					System.out.println("\"" + t.getName() + 
							"\"" + " "
							+ (t.isDaemon() ? "daemon" : "") + " prio="
							+ t.getPriority() + " Thread id=" + t.getId() 
							+ " "
							+ t.getState());
					for (StackTraceElement line : el) {
						System.out.println("\t" + line);
					}
					System.out.println("");
				}
				System.gc();
				System.out.println("-------------------------------------------");
			}
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
	}
}
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence