Summary: Space Worker Dump Example
Simple Space Dump Example Using IWorkerYou can download a simple IWorker example. 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
<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 |