Space Worker - Space Schema

  GigaSpaces 5.X

Documentation Home
Quick Start Guide
Release Notes

Previous release

  Search Here
Searching GigaSpaces Platform 5.X Documentation

                                               

Summary: Setting worker names, implementation classes and arguments.

Overview

The space workers allows you to run your business logic in the space JVM. The space starts the worker business logic when started and stops it when closed. As opposed to the space filters and the replications filters, the space does not call the worker implementation when a space operation is called.

You may register for notifications, call the take or read operations or any other space operation from within the worker. The worker accesses the space in embedded mode – no remote calls between the worker and the space are made. You can start multiple threads at from worker to perform operations in parallel. Please note that the worker will consume memory from the same JVM the space is running, so be careful with the memory the worker uses. The default public empty constructor must be defined for the implemented worker. Examples for space workers are monitoring facilities, calculation intensive facilities, etc.

Worker Interface

public interface com.j_spaces.worker.IWorker extends java.lang.Runnable.

Return value Method
void init(IJSpace proxy, String workerName, String arg)
Called when space is started.
void run()
Called after the init method is called. This method should include the Worker business logic. The Worker running in a separate thread within the space.
void close()
This method should close worker resources including workers spawned threads.

Worker Schema

The space schema file should include the following when introducing a worker to the space:

<space-config>
   <workers>
      <worker-names>WorkerName</worker-names>
      <!--interrupt the worker on shutdown-->
      <interrupt>false</interrupt>
      <WorkerName>
         <enabled>true</enabled>
         <class-name>worker_class_implementation</class-name>
         <arg>some free text</arg>
         <description>worker description</description>
         <active-when-backup>false</active-when-backup>
         <shutdown-space-on-init-failure>true</shutdown-space-on-init-failure>
         <instances>1</instances>
      </WorkerName>
   </workers>
 </space-config>
Element XPath Property Description
<workers> space-config.
workers
Under this tag, all space workers should be defined.
<worker-names> space-config.
workers.
worker-names
Comma separated values of all workers' names.
<enabled> space-config.
workers.
WorkerName.enabled
Boolean value. True will start the worker.
<class-name> space-config.
workers.
WorkerName.class-name
The worker implementation class name
<arg> space-config.
workers.
WorkerName.arg
Free text argument passed into the worker init() method.
<active-when-backup> space-config.
workers.
WorkerName.active-when-backup
Controls the worker activity. When set to false, the worker's init and run methods are called when the space is in active mode (i.e., the backup space is moved to active mode). When set to true, the worker's init() and run() methods are also called for spaces running in backup mode.
<shutdown-space-on-init-failure> space-config.
workers.
WorkerName.shutdown-space-on-init-failure
When set to true, in case of a worker initialization failure, shuts down the space.
<instances> space-config.
workers.
WorkerName.instances
Amount of worker instances to invoke. Each worker runs in a separate thread.
For more details about using IWorker in a partitioned cluster topology, refer to the Partitioned Space Topology section.
New in GigaSpaces 5.2
The <active-when-backup> and <shutdown-space-on-init-failure> tags are new in GigaSpaces version 5.2 and onwards.

Example

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()

Wiki Content Tree


Your Feedback Needed!

We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.

Labels

 
(None)