<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ActiveEon &#187; Integration</title>
	<atom:link href="http://www.activeeon.com/blog/category/integration/feed" rel="self" type="application/rss+xml" />
	<link>http://www.activeeon.com</link>
	<description>ActiveEon is your dedicated technology partner for accelerating and scaling up your enterprise applications.</description>
	<lastBuildDate>Mon, 16 Jan 2012 14:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Sunflow distributed rendering with ProActive Scheduler</title>
		<link>http://www.activeeon.com/blog/all/integration/sunflow-distributed-rendering-with-proactive-scheduler</link>
		<comments>http://www.activeeon.com/blog/all/integration/sunflow-distributed-rendering-with-proactive-scheduler#comments</comments>
		<pubDate>Fri, 14 Aug 2009 16:34:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Integration]]></category>

		<guid isPermaLink="false">http://exp.activeeon.com/?p=411</guid>
		<description><![CDATA[Sunflow is an open source rendering engine for photo-realistic image synthesis. Its integration with the ProActive Scheduler system enables an easy-to-use distributed rendering of 3D models. Main Features Fully Java Open Source Easy installation and minimal configuration Direct integration into the Sunflow GUI Automatic file-transfer management Fault-Tolerance Description This integration makes the installation and usage [...]]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-514 alignleft" style="margin-right: 10px;" title="sunflow-logo" src="http://www.activeeon.com/media_prod_server/2009/09/sunflow-logo.png" alt="sunflow-logo" width="160" height="56" />Sunflow is an open source rendering engine for photo-realistic image synthesis. Its integration with the ProActive Scheduler system enables an easy-to-use distributed rendering of 3D models.</p>
<h2>Main Features</h2>
<ul>
<li>Fully Java Open Source</li>
<li>Easy installation and minimal configuration</li>
<li>Direct integration into the Sunflow GUI</li>
<li>Automatic file-transfer management</li>
<li>Fault-Tolerance</li>
</ul>
<h2>Description</h2>
<p>This integration makes the installation and usage as simple as possible but here is a little explanation for minimal understanding. If you are already familiar with the ProActive Scheduler concepts skip this section.</p>
<p>Executions of parallel tasks on distributed resources (what we call &#8216;nodes&#8217;), such as networks of desktops or clusters, require a main system for managing resources and handling task executions. The ProActive Scheduler is connected to a Resource Manager providing the resource abstraction and enables users to submit jobs, containing one or several tasks that will be executed on available resources. A job is the entity to be submitted to the scheduler. It is composed of one or more tasks. The task is the smallest schedulable entity, it is included in a job and will be executed in accordance with the scheduling policy on the available resources. For more information you can consult <a href="http://proactive.inria.fr/release-doc/scheduler/single_html/SchedulerManual.htmlhttp://proactive.inria.fr/release-doc/scheduler/single_html/SchedulerManual.html" target="_blank">the official ProActive Scheduler documentation</a>.</p>
<p>For the management and advanced control of the Scheduler a remotely connectable GUI is available that offers additional interactions and statistics (a console admin client is also available). Please refer to <a href="http://proactive.inria.fr/release-doc/scheduler/single_html/SchedulerManual.html#Starting_GUI" target="_blank">the Scheduler GUI documentation</a>. Same kind of GUI is available for the Resource Manager, please refer to <a href="http://proactive.inria.fr/release-doc/scheduler/single_html/SchedulerManual.html#Resources_Manager_Welcome_screen" target="_blank">the Resource Manager GUI documentation</a>.</p>
<p style="text-align: center;"><img class="size-full wp-image-486 aligncenter" title="distributed-rendering-architecture_medium" src="http://www.activeeon.com/media_prod_server/2009/09/distributed-rendering-architecture_medium.png" alt="distributed-rendering-architecture_medium" /></p>
<p style="text-align: center;"><strong>Figure 1: Distributed rendering architecture</strong></p>
<p>The Sunflow scene is decomposed into a fixed number of buckets, then each bucket can be rendered independently from the others. A single bucket rendering process is formulated as a task, therefore the whole scene can be seen as a job. However, the user can increase the number of buckets to be rendered per a single task.</p>
<p>Once a remote task is finished, it is sended back through the Scheduler to the Sunflow engine and appears on main screen immediately.</p>
<p style="text-align: center;"><img class="size-full wp-image-510 aligncenter" title="scene_decomposition" src="http://www.activeeon.com/media_prod_server/2009/09/scene_decomposition.png" alt="scene_decomposition" /></p>
<p style="text-align: center;"><strong>Figure 2: Example of 6 buckets per task decomposition</strong></p>
<p>If you look at the figure 2 you can see that the scene is roughly decomposed into 60 buckets, with 6 buckets per task the job will have 10 tasks. This is almost the perfect decomposition if there are 10 computing nodes, since it will require only 10 communications because by default the Scheduler will try to use all available resources. However, if you would like to watch intermediate results in real time and this way be able to quickly detect obvious incorrectness and to cancel the on-going rendering process, simply decrease the number of buckets per task. Depending on the complexity of the scene and resources availaibility, the user can change the bucket/task ratio directly from the Sunflow GUI.</p>
<h2>Detailed Features</h2>
<ul>
<li>The source code is included in the modified sunflow.jar archive.</li>
<li>The user can submit/cancel a job and change the number of buckets per task directly from the Sunflow GUI.</li>
<li>The user doesn&#8217;t need to specify required files (textures, bump maps, images etc.) manually, the rendering tasks will dowload missing files automatically.</li>
<li>The ProActive Scheduler offers resource fault-tolerance by using a database to store activities. For example, if a resource shuts down during a rendering process the rendering task will be executed elsewhere.</li>
</ul>
<p>Be sure to have at least Java SE 5 installed on any computer you want to use. The JDK is required, because the Scheduler uses the database only available in the JDK bundle. You can get the latest release of Java SE from the official <a href="http://java.sun.com/javase/downloads/index.jsp" target="_blank">Java web site</a>.</p>
<h2>Downloads</h2>
<ul>
<li> Get the latest 1.0.2 version of the ProActive Scheduler from <a href="http://www.activeeon.com/community-downloads" target="_blank">http://www.activeeon.com/community-downloads</a> (ProActive Scheduling -&gt; Scheduler Core)</li>
<li>For Windows users get the ProActive Agent from <a href="http://www.activeeon.com/community-downloads" target="_blank">http://www.activeeon.com/community-downloads</a> (ProActive Agent -&gt; ProActive Windows Agent)</li>
<li>Get the slightly modified version of Sunflow that contains some examples scenes here: <a href="http://blog.activeeon.com/wp-content/uploads/2009/08/distributed-sunflow-src-v0.07.2.zip" target="_blank">distributed-sunflow-src-v0.07.2.zip</a> (compiled with Java 1.5)</li>
</ul>
<h2>Installation, Configuration and Execution for Windows Users</h2>
<ul>
<li>From the command-line, go to the Scheduler <strong>bin\windows</strong> directory and<strong> </strong>run the<strong> startScheduler_newDB.bat</strong> script to start the Scheduler with an embedded Resource Manager once started it will print its URL</li>
<li>There are 2 way to create computing nodes on remote computers:
<ul>
<li>First way is to install the ProActive Agent on each computer that you want to use for computation and configure it (double-click on agent tray icon and click on <strong><em>GUI Edit </em></strong>button), you can save the configuration and load it on another computer:
<ol>
<li>In the General tab set the <strong><em>ProActive Location</em></strong> field to the Scheduler directory</li>
<li>In the Connection tab select <strong><em>Resource Manager Registration</em></strong> as Enabled Connection</li>
<li>Fill the <strong><em>Resource Manager URL</em></strong> field with the url of the Resource Manager like rmi://HOSTNAME_OR_IP_ADDRESS:1099/</li>
<li>Fill the <strong><em>Node Name</em></strong> field with for example &#8220;workerNode&#8221;</li>
<li>Use &#8220;demo&#8221; for both <strong><em>Username</em></strong> and <em><strong>Password</strong></em> fields</li>
<li>Save the configuration, close it and start the agent</li>
</ol>
</li>
</ul>
<ul>
<li>Second way is to start a computing node from command-line from Scheduler <strong>bin\windows</strong> directory by running the<strong> startNode.bat &lt;NODE_NAME&gt;</strong> then adding the url of the node to the Resource Manager through the Resource Manager GUI or the admin client.</li>
</ul>
</li>
<li>Start the modified version of Sunflow from command-line by running the <strong>run_sunflow.bat </strong><strong>&lt;SCHEDULER_HOME&gt;</strong> the parameter must be the absolute path of the Scheduler, from the Sunflow GUI select an example<strong> </strong>scene then in case of distributed rendering enter the URL of the Scheduler and use &#8220;demo&#8221; for both username and password.</li>
</ul>
<h2>Installation, Configuration and Execution for Linux Users</h2>
<ul>
<li>From the command-line go to the Scheduler <strong>bin\unix</strong> directory and<strong> </strong>run the<strong> startScheduler_newDB.sh</strong> script to start the Scheduler with an embedded Resource Manager</li>
<li>There are 2 ways to create computing nodes on remote computers:
<ul>
<li>Start a computing node from command-line by running <strong>$SCHEDULER_HOME/bin/unix/startNode.sh &lt;NODE_NAME&gt;</strong> then add the url of the node to the Resource Manager through the Resource Manager GUI</li>
<li>Deploy several computing nodes by using a <a href="http://proactive.inria.fr/release-doc/scheduler/single_html/SchedulerManual.html#rm_add_node" target="_blank">GCM Deployement Descriptor</a></li>
</ul>
</li>
</ul>
<h2>Example</h2>
<p style="text-align: center;"><img class="size-full wp-image-508 aligncenter" title="rendered-julia" src="http://www.activeeon.com/media_prod_server/2009/09/rendered-julia.PNG" alt="rendered-julia" /></p>
<p style="text-align: center;"><strong>Figure 3: The rendered </strong><strong><em>julia.sc</em> scene</strong></p>
<p>For example the distributed rendering of the <strong>julia.sc</strong> (see figure 3) scene using a completely heterogeneous infrastructure composed of 1 qual-core Windows XP (4 computing nodes) , 1 dual-core Windows Vista (2 computing nodes), 1 dual-core MacBook (2 computing nodes) and 1 dual-core Linux (2 computing nodes) is enough to significantly reduce the computing time.</p>
<p style="text-align: center;"><a href="http://www.activeeon.com/media_prod_server/2009/09/rendering_times_classic_vs_distributed_-_julia_sc.png"><img class="size-full wp-image-509 aligncenter" title="rendering_times_classic_vs_distributed_-_julia_sc" src="http://www.activeeon.com/media_prod_server/2009/09/rendering_times_classic_vs_distributed_-_julia_sc.png" alt="rendering_times_classic_vs_distributed_-_julia_sc" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.activeeon.com/blog/all/integration/sunflow-distributed-rendering-with-proactive-scheduler/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Distribute a Spring Batch job on the Proactive Scheduler</title>
		<link>http://www.activeeon.com/blog/all/integration/distribute-a-spring-batch-job-on-the-proactive-scheduler</link>
		<comments>http://www.activeeon.com/blog/all/integration/distribute-a-spring-batch-job-on-the-proactive-scheduler#comments</comments>
		<pubDate>Mon, 25 May 2009 10:06:50 +0000</pubDate>
		<dc:creator>emil salageanu</dc:creator>
				<category><![CDATA[Integration]]></category>

		<guid isPermaLink="false">http://exp.activeeon.com/?p=429</guid>
		<description><![CDATA[As its name states, this article describes the steps to perform in order to have the execution of  Spring Batch job processed remotely on an infrastructure managed by the ProActive Scheduling platform]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" title="partitionning_scheduler1" src="http://blog.activeeon.com/wp-content/uploads/2009/05/partitionning_scheduler1.png" alt="partitionning_scheduler1" width="600" height="400" /></p>
<p>As its name states, this article describes the steps to perform in order to have the execution of  Spring Batch job processed remotely on an infrastructure managed by the ProActive Scheduling platform.<br />
The solution presented is based on Spring Batch Partitioning; you should take a look at section <a href="http://static.springframework.org/spring-batch/reference/html/scalability.html"> 7.4. Partitioning </a> in the Spring Batch Documentation.</p>
<p><span id="more-429"></span></p>
<p>Here&#8217;s the schema from the spring batch documentation:</p>
<p style="text-align: center;"><img class="aligncenter" style="width: 479px; height: 365px;" src="http://static.springframework.org/spring-batch/reference/html/images/partitioning-overview.png" alt="Spring BatchPartitioning Overview" /><br />
<img style="width: 555px; height: 421px;" src="http://static.springframework.org/spring-batch/reference/html/images/partitioning-spi.png" alt="Spring BatchPartitioning Overview" /><br />
If you have not already done it, you should download spring batch from <a href="http://www.springsource.org/download">http://www.springsource.org/download</a> and ProActive Scheduling from<a href="http://www.activeeon.com"> http://www.activeeon.com</a> (you should download the Scheduling Project and  eclipse RCPs (the Graphical clients).<br />
This article is based on spring batch 2.0.0 Release and the Scheduling 1.1 release.</p>
<h1><a id="part_1"> Part 1 &#8211; Implementation </a></h1>
<h4><a id="start_point"> Start Point </a></h4>
<p>The job I have used for this purpose, as a start point,  is the job defined in<br />
org.springframework.batch-2.0.0.RELEASE/sources/org.springframework.batch.core/ src/test/resources/org/springframework/batch/core/partition/launch-context.xml</p>
<p>This spring-batch example job is composed by:</p>
<ul>
<li>simple hello world ItemReader and ItemWriter<br />
(org.springframework.batch.core.partition.ExampleItemReader<br />
and org.springframework.batch.core.partition.ExampleItemWriter)</li>
<li>a Simple Job implementation (org.springframework.batch.core.job.SimpleJob)</li>
<li>a partition step (org.springframework.batch.core.partition.support.PartitionStep) – this is used as implementation for the master step</li>
<li>a step execution splitter, used by the partition step (org.springframework.batch.core.partition.support.SimpleStepExecutionSplitter)</li>
<li> A PartitionHandler, also used by the Partition Step (org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler)</li>
</ul>
<p>Bellow, the xml file which defines this job, taken from</p>
<p>org.springframework.batch-2.0.0.RELEASE/sources/org.springframework.batch.core/ src/test/resources/org/springframework/batch/core/partition/launch-context.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span> <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/p&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot; http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:/org/springframework/batch/core/repository/dao/data-source-context.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;job1&quot;</span> <span style="color: #000066;">parent</span>=<span style="color: #ff0000;">&quot;simpleJob&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;steps&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;step1:master&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;partitionHandler&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;taskExecutor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;step&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;step1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;gridSize&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;stepExecutionSplitter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;step1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;step1&quot;</span> <span style="color: #000066;">parent</span>=<span style="color: #ff0000;">&quot;simpleStep&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;itemReader&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;step&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;itemWriter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;simpleJob&quot;</span> <span style="color: #000066;">abstract</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;restartable&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;simpleStep&quot;</span> <span style="color: #000066;">abstract</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;startLimit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;commitInterval&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jobLauncher&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">p:databaseType</span>=<span style="color: #ff0000;">&quot;hsql&quot;</span> <span style="color: #000066;">p:dataSource-ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">p:transactionManager-ref</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><span style="color: #000000; font-weight: bold;"> </span></span></span></p>
<h4><a id="modify_the_example"> </a>Modify the example partition job to distribute it on the ProActive Scheduler</h4>
<p>The ProActive Scheduler offers a java API in order to allow users to create, submit jobs on the scheduler and gather the results. See full documentation and tutorial at http://proactive.inria.fr/release-doc/scheduler/multiple_html/index.html</p>
<p>In order to run a job on the Proactive Scheduler we have to create a specific PartitionHandler.<br />
I&#8217;ll call this class <strong><em>ProactiveSchedulerPartitionHandler</em></strong>.</p>
<p>In odrer to implement this class, I took the <em>org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler</em> implementation, changed the handle method and add some properties.<br />
Let&#8217;s take a look at the handle method which creates a ProActive Scheduler job, iterates over the step executions returned by the Stpet Execution Splitter and creates a ProActive Scheduling task for each step execution. The created job encapsulates these tasks. The Scheduler will execute them remotely on  the available resources.<br />
Bellow, the code of the handle method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Collection</span> handle<span style="color: #009900;">&#40;</span>
            StepExecutionSplitter stepExecutionSplitter,
            StepExecution masterStepExecution<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">Collection</span> result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// A job to be scheduled on the ProactiveScheduler</span>
        TaskFlowJob proactiveSchedulerJob <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TaskFlowJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        proactiveSchedulerJob.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Master Step: &quot;</span>
                <span style="color: #339933;">+</span> masterStepExecution.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// the step executions</span>
        <span style="color: #003399;">Set</span> stepExecutions <span style="color: #339933;">=</span> stepExecutionSplitter.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span>
                masterStepExecution, gridSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StepExecution stepExecution <span style="color: #339933;">:</span> stepExecutions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">String</span> jobExecutionId <span style="color: #339933;">=</span> stepExecution.<span style="color: #006633;">getJobExecution</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> stepExecutionId <span style="color: #339933;">=</span> stepExecution.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> stepName <span style="color: #339933;">=</span> step.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            JavaTask jt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JavaTask<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span>stepExecutionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">setExecutableClassName</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #006633;">JOB_EXECUTION_ID</span>, jobExecutionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #006633;">STEP_EXECUTION_ID</span>, stepExecutionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #006633;">STEP_NAME</span>, stepName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #006633;">JOB_CONFIG_FILE</span>,
                    <span style="color: #0000ff;">&quot;/launch-context.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            proactiveSchedulerJob.<span style="color: #006633;">addTask</span><span style="color: #009900;">&#40;</span>jt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// for all StepExecution</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setJobClasPath</span><span style="color: #009900;">&#40;</span>proactiveSchedulerJob<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        SchedulerAuthenticationInterface auth <span style="color: #339933;">=</span> SchedulerConnection
                .<span style="color: #006633;">join</span><span style="color: #009900;">&#40;</span>proactiveSchedulerUrl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        UserSchedulerInterface uischeduler <span style="color: #339933;">=</span> auth.<span style="color: #006633;">logAsUser</span><span style="color: #009900;">&#40;</span>
                proactiveSchedulerUserName, proactiveSchedulerPassword<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        JobId id <span style="color: #339933;">=</span> uischeduler.<span style="color: #006633;">submit</span><span style="color: #009900;">&#40;</span>proactiveSchedulerJob<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// blocking loop until we have the result:</span>
&nbsp;
        JobResult jr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>jr <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jr <span style="color: #339933;">=</span> uischeduler.<span style="color: #006633;">getJobResult</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">Map</span> jobResults <span style="color: #339933;">=</span> jr.<span style="color: #006633;">getAllResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// the jobresults map contains entries of type:</span>
        <span style="color: #666666; font-style: italic;">// and the task_name is the same as the corresponding step execution id</span>
        <span style="color: #666666; font-style: italic;">// Therefore we iterate over the stepExecutions list and get the result</span>
        <span style="color: #666666; font-style: italic;">// for each stepExecution</span>
        <span style="color: #666666; font-style: italic;">// This will allow us to manage exceptions on the remote tasks</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StepExecution stepExecution <span style="color: #339933;">:</span> stepExecutions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            TaskResult taskResult <span style="color: #339933;">=</span> jobResults.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>stepExecution.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                StepExecution se <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>StepExecution<span style="color: #009900;">&#41;</span> taskResult.<span style="color: #006633;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                result.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>se<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// an exception has been thrown during the launch of the task on</span>
                <span style="color: #666666; font-style: italic;">// the remote node</span>
                <span style="color: #666666; font-style: italic;">// we will mark the local step execution as FAILED, attach the</span>
                <span style="color: #666666; font-style: italic;">// exception to it and addd it to the results</span>
&nbsp;
                ExitStatus exitStatus <span style="color: #339933;">=</span> ExitStatus.<span style="color: #006633;">FAILED</span>
                        .<span style="color: #006633;">addExitDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TaskExecutor rejected the task for this step.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                stepExecution.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>BatchStatus.<span style="color: #006633;">FAILED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                stepExecution.<span style="color: #006633;">setExitStatus</span><span style="color: #009900;">&#40;</span>exitStatus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                stepExecution.<span style="color: #006633;">addFailureException</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                result.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>stepExecution<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// we print the stacktrace:</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;an error occured while executing the step &quot;</span>
                        <span style="color: #339933;">+</span> stepExecution.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                t.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>First we create a TaskFlowJob (a job containing a set of tasks that may have dependencies between them).<br />
This job will be sent to the Scheduler for distributed execution.<br />
Then we obtain a list of step execution from the stepExecutionSplitter. We iterate over this list and, for each step execution, we create a task for our job. The java code for the task is defined by the StepExecutable class. Each task will receive in argument the job execution id, the step execution id  and the initial job configuration file so the respective step execution could be retrieved from an application context on the remote node.<br />
Next thing to notice is the job classpath: we add to our job a job environment containing the classes of our application so they could be found on the remote node.<br />
Finally we connect to the Scheduler, send the job and wait for the results.<br />
See  the full code of the class below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.activeeon.sandbox.spring</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.SchedulerAuthenticationInterface</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.SchedulerConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.UserSchedulerInterface</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.job.JobEnvironment</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.job.JobId</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.job.JobResult</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.job.TaskFlowJob</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.task.JavaTask</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.task.TaskResult</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.BatchStatus</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.ExitStatus</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.Step</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.StepExecution</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.partition.PartitionHandler</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.partition.StepExecutionSplitter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.InitializingBean</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.util.Assert</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * A {@link PartitionHandler} that creates and submit a job on the ProActive
 * Scheduler
 *
 * @author Emil Salageanu
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProactiveSchedulerPartitionHandler <span style="color: #000000; font-weight: bold;">implements</span> PartitionHandler,
        InitializingBean <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> gridSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> proactiveSchedulerUrl<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> proactiveSchedulerUserName<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> proactiveSchedulerPassword<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Step step<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPropertiesSet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">notNull</span><span style="color: #009900;">&#40;</span>step, <span style="color: #0000ff;">&quot;A Step must be provided.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Passed to the {@link StepExecutionSplitter} in the
     * {@link #handle(StepExecutionSplitter, StepExecution)} method, instructing
     * it how many {@link StepExecution} instances are required, ideally. The
     * {@link StepExecutionSplitter} is allowed to ignore the grid size in the
     * case of a restart, since the input data partitions must be preserved.
     *
     * @param gridSize
     *            the number of step executions that will be created
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setGridSize<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> gridSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">gridSize</span> <span style="color: #339933;">=</span> gridSize<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Setter for the {@link Step} that will be used to execute the partitioned
     * {@link StepExecution}. This is a regular Spring Batch step, with all the
     * business logic required to complete an execution based on the input
     * parameters in its {@link StepExecution} context.
     *
     * @param step
     *            the {@link Step} instance to use to execute business logic
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStep<span style="color: #009900;">&#40;</span>Step step<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">step</span> <span style="color: #339933;">=</span> step<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @see PartitionHandler#handle(StepExecutionSplitter, StepExecution)
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Collection</span> handle<span style="color: #009900;">&#40;</span>
            StepExecutionSplitter stepExecutionSplitter,
            StepExecution masterStepExecution<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">Collection</span> result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// A job to be scheduled on the ProactiveScheduler</span>
        TaskFlowJob proactiveSchedulerJob <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TaskFlowJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        proactiveSchedulerJob.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Master Step: &quot;</span>
                <span style="color: #339933;">+</span> masterStepExecution.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// the step executions</span>
        <span style="color: #003399;">Set</span> stepExecutions <span style="color: #339933;">=</span> stepExecutionSplitter.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span>
                masterStepExecution, gridSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StepExecution stepExecution <span style="color: #339933;">:</span> stepExecutions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">String</span> jobExecutionId <span style="color: #339933;">=</span> stepExecution.<span style="color: #006633;">getJobExecution</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> stepExecutionId <span style="color: #339933;">=</span> stepExecution.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> stepName <span style="color: #339933;">=</span> step.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            JavaTask jt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JavaTask<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span>stepExecutionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">setExecutableClassName</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #006633;">JOB_EXECUTION_ID</span>, jobExecutionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #006633;">STEP_EXECUTION_ID</span>, stepExecutionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #006633;">STEP_NAME</span>, stepName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span>StepExecutable.<span style="color: #006633;">JOB_CONFIG_FILE</span>,
                    <span style="color: #0000ff;">&quot;/launch-context.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            proactiveSchedulerJob.<span style="color: #006633;">addTask</span><span style="color: #009900;">&#40;</span>jt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// for all StepExecution</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setJobClasPath</span><span style="color: #009900;">&#40;</span>proactiveSchedulerJob<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        SchedulerAuthenticationInterface auth <span style="color: #339933;">=</span> SchedulerConnection
                .<span style="color: #006633;">join</span><span style="color: #009900;">&#40;</span>proactiveSchedulerUrl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        UserSchedulerInterface uischeduler <span style="color: #339933;">=</span> auth.<span style="color: #006633;">logAsUser</span><span style="color: #009900;">&#40;</span>
                proactiveSchedulerUserName, proactiveSchedulerPassword<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        JobId id <span style="color: #339933;">=</span> uischeduler.<span style="color: #006633;">submit</span><span style="color: #009900;">&#40;</span>proactiveSchedulerJob<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// blocking loop until we have the result:</span>
&nbsp;
        JobResult jr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>jr <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jr <span style="color: #339933;">=</span> uischeduler.<span style="color: #006633;">getJobResult</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">Map</span> jobResults <span style="color: #339933;">=</span> jr.<span style="color: #006633;">getAllResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// the jobresults map contains entries of type:</span>
        <span style="color: #666666; font-style: italic;">// and the task_name is the same as the corresponding step execution id</span>
        <span style="color: #666666; font-style: italic;">// Therefore we iterate over the stepExecutions list and get the result</span>
        <span style="color: #666666; font-style: italic;">// for each stepExecution</span>
        <span style="color: #666666; font-style: italic;">// This will allow us to manage exceptions on the remote tasks</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StepExecution stepExecution <span style="color: #339933;">:</span> stepExecutions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            TaskResult taskResult <span style="color: #339933;">=</span> jobResults.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>stepExecution.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                StepExecution se <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>StepExecution<span style="color: #009900;">&#41;</span> taskResult.<span style="color: #006633;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                result.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>se<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// an exception has been thrown during the launch of the task on</span>
                <span style="color: #666666; font-style: italic;">// the remote node</span>
                <span style="color: #666666; font-style: italic;">// we will mark the local step execution as FAILED, attach the</span>
                <span style="color: #666666; font-style: italic;">// exception to it and addd it to the results</span>
&nbsp;
                ExitStatus exitStatus <span style="color: #339933;">=</span> ExitStatus.<span style="color: #006633;">FAILED</span>
                        .<span style="color: #006633;">addExitDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TaskExecutor rejected the task for this step.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                stepExecution.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>BatchStatus.<span style="color: #006633;">FAILED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                stepExecution.<span style="color: #006633;">setExitStatus</span><span style="color: #009900;">&#40;</span>exitStatus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                stepExecution.<span style="color: #006633;">addFailureException</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                result.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>stepExecution<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// we print the stacktrace:</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;an error occured while executing the step &quot;</span>
                        <span style="color: #339933;">+</span> stepExecution.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                t.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setProactiveSchedulerUrl<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> proactiveSchedulerUrl<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">proactiveSchedulerUrl</span> <span style="color: #339933;">=</span> proactiveSchedulerUrl<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setProactiveSchedulerUserName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> proactiveSchedulerUserName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">proactiveSchedulerUserName</span> <span style="color: #339933;">=</span> proactiveSchedulerUserName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setProactiveSchedulerPassword<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> proactiveSchedulerPassword<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">proactiveSchedulerPassword</span> <span style="color: #339933;">=</span> proactiveSchedulerPassword<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setJobClasPath<span style="color: #009900;">&#40;</span>org.<span style="color: #006633;">ow2</span>.<span style="color: #006633;">proactive</span>.<span style="color: #006633;">scheduler</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">job</span>.<span style="color: #006633;">Job</span> job<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> appClassPath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">File</span> appMainFolder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getProtectionDomain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">getCodeSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLocation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            appClassPath <span style="color: #339933;">=</span> appMainFolder.<span style="color: #006633;">getAbsolutePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">net</span>.<span style="color: #006633;">URISyntaxException</span> e1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            e1.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        JobEnvironment je <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JobEnvironment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            je.<span style="color: #006633;">setJobClasspath</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> appClassPath <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;job classpath: &quot;</span> <span style="color: #339933;">+</span> appClassPath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">io</span>.<span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        job.<span style="color: #006633;">setEnvironment</span><span style="color: #009900;">&#40;</span>je<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Note that I have removed the taskExecutor property from the bean (we don&#8217;t need a task executroanymore), and added 3 properties for the connection to  the Scheduler (scheduler url, username, password).</p>
<p>The second and last class we need to implement is the StepExecutable class which defines the code to be executed on the remote node.  It defines an execute method which does nothing more than creating a spring application context, gathering the step execution and executing it.  See the code bellow:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.activeeon.sandbox.spring</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.task.TaskResult</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.task.executable.JavaExecutable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.JobInterruptedException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.Step</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.StepExecution</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.batch.core.explore.JobExplorer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.BeanFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.ApplicationContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.support.ClassPathXmlApplicationContext</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StepExecutable <span style="color: #000000; font-weight: bold;">extends</span> JavaExecutable <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> JOB_EXECUTION_ID <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jobExecutionId&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> STEP_EXECUTION_ID <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;stepExecutionId&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> STEP_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;stepName&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> JOB_CONFIG_FILE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;JobConfigFile&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> jobExecutionId<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> stepExecutionId<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> stepName<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> configFile<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
        jobExecutionId <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #006633;">parseLong</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>JOB_EXECUTION_ID<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        stepExecutionId <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #006633;">parseLong</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>STEP_EXECUTION_ID<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        stepName <span style="color: #339933;">=</span> args.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>STEP_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        configFile <span style="color: #339933;">=</span> args.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>JOB_CONFIG_FILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Serializable</span> execute<span style="color: #009900;">&#40;</span>TaskResult... <span style="color: #006633;">arg0</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        ApplicationContext context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassPathXmlApplicationContext<span style="color: #009900;">&#40;</span>
                configFile, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ApplicationContext ClassLoader:&quot;</span>
                <span style="color: #339933;">+</span> context.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        BeanFactory factory <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
        Step step <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Step<span style="color: #009900;">&#41;</span> factory.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span>stepName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        JobExplorer je <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JobExplorer<span style="color: #009900;">&#41;</span> factory.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jobExplorer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        StepExecution stepExecution <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>StepExecution<span style="color: #009900;">&#41;</span> je.<span style="color: #006633;">getStepExecution</span><span style="color: #009900;">&#40;</span>
                jobExecutionId, stepExecutionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            step.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>stepExecution<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JobInterruptedException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            stepExecution.<span style="color: #006633;">addFailureException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> stepExecution<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h1><a id="part2"/> Part 2 &#8211; Configuration and run</h1>
<h4><a id="conf_files"> Configuration files</a></h4>
<p>The <strong>job configuration xml file</strong> is the shown bellow:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span> <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/p&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot; http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;my-data-source-context.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;PAJOB&quot;</span> <span style="color: #000066;">parent</span>=<span style="color: #ff0000;">&quot;simpleJob&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span> value =<span style="color: #ff0000;">&quot;myJob44&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;steps&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;step1:master&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;partitionHandler&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;step&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;step1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;gridSize&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> name = <span style="color: #ff0000;">&quot;proactiveSchedulerUrl&quot;</span> value = <span style="color: #ff0000;">&quot;rmi://192.168.1.10:1099&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> name = <span style="color: #ff0000;">&quot;proactiveSchedulerUserName&quot;</span> value = <span style="color: #ff0000;">&quot;admin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> name = <span style="color: #ff0000;">&quot;proactiveSchedulerPassword&quot;</span> value = <span style="color: #ff0000;">&quot;admin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;stepExecutionSplitter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;step1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;step1&quot;</span> <span style="color: #000066;">parent</span>=<span style="color: #ff0000;">&quot;simpleStep&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;itemReader&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;step&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;itemWriter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;simpleJob&quot;</span> <span style="color: #000066;">abstract</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;restartable&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;simpleStep&quot;</span> <span style="color: #000066;">abstract</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;startLimit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;commitInterval&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jobLauncher&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jobRepository&quot;</span> <span style="color: #000066;">p:databaseType</span>=<span style="color: #ff0000;">&quot;mysql&quot;</span> <span style="color: #000066;">p:dataSource-ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">p:transactionManager-ref</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jobExplorer&quot;</span> <span style="color: #000066;">p:dataSource-ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">p:tablePrefix</span>=<span style="color: #ff0000;">&quot;BATCH_&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>I  use a <strong>mysql database  as datasource</strong>, defined in the my-data-source-context.xml file, bellow:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;initScripts&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>schema-mysql.sql<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Now, we still need two configuration files for the Proactive managed infrastructure &#8230; well, you didn&#8217;t expect to finish this tutorial in 15 minutes, did you? We&#8217;re almost finished &#8230;</p>
<p>We will  use ProActive Resource Manager in order to deploy a set of remote resources (start remote JVMs and use them to perform computation): A GCMApplication file and a GCMDeployment file.<br />
<strong>GCMApplication</strong><br />
By default, the remote jvm&#8217;s classpath is defined in the file Scheduler1_0/config/deployment/GCMNodeSourceApplication.xml (we&#8217;re talking about the JVMs deployed by the Resource Manager). You should either modify this file or create a new one, in order to add spring jars in the classpath of remote nodes.<br />
My GCMNodeSourceApplication.xml is :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="wml" style="font-family:monospace;">&lt;!-- this variable will be replaced by a path of a GCMD to deploy, by RMAdmin --&gt;
&lt;!-- Commented dependencies are referenced through the Jar-Index of Scheduler jars --&gt;
&lt;!-- Script engines must be explicitly in application classpath --&gt;
&lt;!-- Needed explicitly by VFS (file transfer in pre/post script --&gt;
&lt;!--
&lt;pathElement base=&quot;proactive&quot; relpath=&quot;/dist/lib/script-api.jar&quot; /&gt;
&lt;pathElement base=&quot;proactive&quot; relpath=&quot;/dist/lib/js.jar&quot; /&gt;
&lt;pathElement base=&quot;proactive&quot; relpath=&quot;/dist/lib/jruby.jar&quot; /&gt;
&lt;pathElement base=&quot;proactive&quot; relpath=&quot;/dist/lib/jython.jar&quot; /&gt; --&gt;
&nbsp;
&lt;!-- Spring jars  --&gt;
&lt;!-- MySQL JDBC Driver --&gt;</pre></td></tr></table></div>

<p>I have added, in this GCMApplication file the spring-batch jars I need, the jar for the java mysql driver and a folder containing my context-xml file accessible on the remote node.</p>
<p>NOTE: We hope that, in the next version of Scheduling, you will not have to specify the folder containing the spring job&#8217;s definition xml file – this should be<br />
automatically transfered by proactive.</p>
<p><strong>GCMDeployment</strong></p>
<p>Choose a GCMDeployment file in [Scheduler_Home]/config/deployment and modify it accoring to your infrastructure or just use<br />
[Scheduler_Home]/config/deployment/Local4JVMDeployment.xml to specify that you want 4 jvms deployed on localhost.</p>
<h4><a id="run_app"> Run the Application </a></h4>
<p>Here we are &#8230;<br />
First of all, start the Resource Manager and the Scheduler Servers:</p>
<p><strong>Start Resource Manager:</strong><br />
go to [Scheduing_Home]/bin/unix and  type</p>
<pre class="code">./startRM.sh –deploy [Scheduler_Home]/config/deployment/Local4JVMDeployment.xml</pre>
<p>Note: if you have not modified the GCMNodeSourceApplication.xml (to add spinrg jars, remember?) but created a different file, you have to modify the startRM.sh file and add  this option to the JAVACMD:</p>
<pre class="code">  -Dpa.rm.gcm.template.application.file=path/to/my/GCMNodeSourceApplication.xml</pre>
<p>Note: you can also start the RM without the deploy option and deploy later from the RM User Interface.<br />
<strong> Start the Scheduler:</strong></p>
<p>go to  [Scheduing_Home]/bin/unix  and type:</p>
<pre class="code">  ./startScheduler_newDB.sh</pre>
<p>Now you can <strong>start the Graphical User Interfaces</strong> (RCP&#8217;s) for the Resource Manager and the Scheduler and connect them to the respective servers.</p>
<p>And finally, <strong>start your application</strong>.<br />
You can use org.springframework.batch.core.launch.support.CommandLineJobRunner with arguments launch-context.xml PAJOB (do not forget to add spring jars for your application).<br />
You can download source and configuration files here <a href="/wp-content/uploads/2009/05/springbatchandschedulerexamplesrc_tar.gz">SpringBatchAndSchedulerSRC</a></p>
<p>Hope it works, if it doesn&#8217;t, leave a message.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.activeeon.com/blog/all/integration/distribute-a-spring-batch-job-on-the-proactive-scheduler/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performing Grid Computing from an Application Server</title>
		<link>http://www.activeeon.com/blog/all/integration/performing-grid-computing-from-an-application-server</link>
		<comments>http://www.activeeon.com/blog/all/integration/performing-grid-computing-from-an-application-server#comments</comments>
		<pubDate>Fri, 22 May 2009 15:07:28 +0000</pubDate>
		<dc:creator>emil salageanu</dc:creator>
				<category><![CDATA[Integration]]></category>

		<guid isPermaLink="false">http://exp.activeeon.com/?p=431</guid>
		<description><![CDATA[The goal of this Article is to show a use case of delegating the computation performed within a J2EE Application Server (Jboss is used in this article) by connecting an EJB3 application to a grid computing open source technology (ProActive Scheduler). This is very useful in case of applications that require computational intensive algorithms for [...]]]></description>
			<content:encoded><![CDATA[<p>The goal of this Article is to show a use case of delegating the computation performed within a J2EE Application Server (Jboss is used in this article) by connecting an EJB3 application to a grid computing open source technology (ProActive Scheduler). This is very useful in case of applications that require computational intensive algorithms for responding to a user request. See the picture bellow.</p>
<p style="text-align: center;"><img title="schema_scheduler_jboss" src="http://blog.activeeon.com/wp-content/uploads/2009/05/schema.png" alt="schema_scheduler_jboss" width="490" height="280" /></p>
<p><strong> Organization of The Article/Tutorial</strong></p>
<p><em>Part ONE: Write a very simple EJB application</em></p>
<p>In the first part, we will create a simple EJB application composed by one single session bean that exposes e method for counting the number of divisors of a number n.</p>
<p><em> Part TWO: Use ProActive Scheduler to perform the computation on a grid</em></p>
<p>In this part we will show how to connect to the ProActive Scheduler, split input data and create tasks, create a job wich contains the tasks and submit it to the scheduler, merge the results of the computation and send the final result to the user.</p>
<p>The tutorial includes the installation steps of the required (open source) software (Jboss server, ProActive Scheduler, Eclipse ).<br />
<img title="More..." src="http://blog.activeeon.com/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt=" Sounds nice, I wanna read this ... " /></p>
<p><strong>The Target Application</strong></p>
<p>We will implement a simple application that computes the number of divisors of a (big) integer number.</p>
<p><strong>Install and Configuration</strong></p>
<ol>
<li>Download Eclipse Ganymede (the package “Eclipse IDE for Java EE Developers”) from http://www.eclipse.org/downloads/packages/ and extract it into a local folder. Lets call it ECLIPSE_HOME</li>
<li>Download JBOSS Server (I used the version 4.2.3 GA) from http://www.jboss.org/jbossas/downloads and extract into a local folder (lets call it JBOSS_HOME)</li>
<li>Download the ProActive Scheduler product from http://www.activeeon.com/community-downloads (I used the 4.0 version) and put it in a folder (lets call it SCHEDULER_HOME)</li>
</ol>
<p><em>Note</em>: It is recommended that folders that contain JBoss and ProActive do not contain spaces.</p>
<p><em>Note</em>: If you want to start the JBoss server from eclipse, into Eclipse Workbench go to “Servers View” → right click → New→ Server; Choose Jboss → the version your are using (v.4.2 for me) → Next → Provide application server directory (browse to your JBOSS_HOME folder) → Finish</p>
<h1><a id="Part_one"> Part ONE: Write a very simple EJB3 application</a></h1>
<p>We will now write a simple EJB3 application (compound of only one session bean), deploy it on the server and use a client application to send a request to the bean. Let&#8217;s write an application that counts the number of divisors of a number n. In the Eclipse workspace, create a new simple java project called DivisorCounterEJB</p>
<h4><a id="import_jars"> Import jars from Jboss </a></h4>
<p>Go to Project → Properties → Java Build Path → Libraries and click on Add External Jars. Navigate to JBOSSS_HOME/server/default/lib and import the jars:</p>
<ul>
<li>ejb3-persistence.jar</li>
<li>jboss-ejb3x.jar</li>
</ul>
<h4><a id="creation_interface"> Creation of the interface </a></h4>
<p>Create a new interface in your project</p>
<ul>
<li>Package: com.activeeon.demos.ejb.divisors</li>
<li>Interface Name: DivisorCounter</li>
</ul>
<p>We will annotate the interface with the @javax.ejb.Remote annotation and define our business method countDivisors(BigInteger n)</p>
<p>The code of the interface is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.activeeon.demos.ejb.divisors</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigInteger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.ApplicationException</span><span style="color: #339933;">;</span>
  @javax.<span style="color: #006633;">ejb</span>.<span style="color: #003399;">Remote</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> DivisorCounter <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> countDivisors<span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<h4><a id="creation_bean"> Creation of the Bean </a></h4>
<div>
<p>Create a new class:</p></div>
<ul>
<li>package:  com.activeeon.demos.ejb.divisors</li>
<li>class name: DivisorCounterBean</li>
</ul>
<p>The class implements the interface DivisorCounter We will annotate the class with @Stateleful. (Stateless version works as well, see notes at the end of the article) We have to implement the method countDivisors. For now we will compute the number of divisors sequentially, in this method. In the second part of this article, we will perform the computation on a grid. The code we obtain is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">       <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.activeeon.demos.ejb.divisors</span><span style="color: #339933;">;</span>
       <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigInteger</span><span style="color: #339933;">;</span>
       <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.Stateful</span><span style="color: #339933;">;</span>
       @Stateful
       <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DivisorCounterBean <span style="color: #000000; font-weight: bold;">implements</span> DivisorCounter<span style="color: #009900;">&#123;</span>
      	@Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> countDivisors<span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  <span style="color: #000066; font-weight: bold;">long</span> divisors <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;New request received for counting divisor for number &quot;</span><span style="color: #339933;">+</span>n.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span> i <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span>n.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">=</span>i.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	         <span style="color: #009900;">&#123;</span>
		        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>n.<span style="color: #006633;">remainder</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span>.<span style="color: #006633;">ZERO</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		            	divisors<span style="color: #339933;">++;</span>
           		<span style="color: #009900;">&#125;</span>
	       <span style="color: #666666; font-style: italic;">//we add the last divisor which is n itself</span>
         		divisors<span style="color: #339933;">+=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
      	      <span style="color: #000000; font-weight: bold;">return</span> divisors<span style="color: #339933;">;</span>
         	<span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span></pre></div></div>

<h4><a id="generating_the_jar_file_and_deploying_the_bean_on_the_jboss_server" name="generating_the_jar_file_and_deploying_the_bean_on_the_jboss_server">Generating the JAR file and deploying the Bean on the JBoss Server:</a></h4>
<p>We will have to generate the jar file of our application and deploy it on the application server. In the Eclipse Workbench, go to File → Export → Java→ Jar File Choose the project DivisorCounterEJB. Choose the name of the jar file DivisorCounter.jar and the location JBOSS_HOME/servers/default/deploy If your jboss server is not yet running, you can start it (from Eclipse go to Java EE perspective, Servers View, start server). You should see on the output of the JBoss Serever the lines:</p>
<pre>12:58:16,702 INFO  [JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=DivisorCounter.jar,name=DivisorCounterBean,service=EJB3 with dependencies:
12:58:16,732 INFO  [EJBContainer] STARTED EJB: com.activeeon.demos.ejb.divisors.DivisorCounterBean ejbName: DivisorCounterBean
12:58:16,779 INFO  [EJB3Deployer] Deployed: file:/home/esalagea/ActiveEon/articles/SchedulerAndJboss/jboss-4.2.3.GA/server/default/deploy/DivisorCounter.jar</pre>
<p>Your bean is deployed on the server and ready to be used.</p>
<h4><a id="creating_a_client_application" name="creating_a_client_application">Creating a client Application:</a></h4>
<ul>
<li>
<div>Create a new Java Project called DivisorCounterClient.</div>
</li>
<li>
<div>Add the external jars Jnp-client.jar and Jbossall-client.jar from JBOSS_HOME/client (go to Properties, Java Build Path → Add External Jars → …)</div>
</li>
<li>
<div>We also need to add the jar file of the previous (EJB) application. Go to Add External Jars and navigate to JBOSS_HOME/server/default/deploy/DivisorCounter.jar</div>
</li>
<li>
<div>Now add a JNDI file to your client project. Create a (text) file callled jndi.properties. At running time this file should be found in the root of the bin folder of your project (you could put the file in the root of the src folder in order to be copied, at compile time, in the bin folder) The content of the file is:</div>
</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java.<span style="color: #006633;">naming</span>.<span style="color: #006633;">factory</span>.<span style="color: #006633;">initial</span><span style="color: #339933;">=</span>org.<span style="color: #006633;">jnp</span>.<span style="color: #006633;">interfaces</span>.<span style="color: #006633;">NamingContextFactory</span>
java.<span style="color: #006633;">naming</span>.<span style="color: #006633;">factory</span>.<span style="color: #006633;">url</span>.<span style="color: #006633;">pkgs</span><span style="color: #339933;">=</span>org.<span style="color: #006633;">jboss</span>.<span style="color: #006633;">naming</span><span style="color: #339933;">:</span>org.<span style="color: #006633;">jnp</span>.<span style="color: #006633;">interfaces</span>
java.<span style="color: #006633;">naming</span>.<span style="color: #006633;">provider</span>.<span style="color: #006633;">url</span><span style="color: #339933;">=</span>localhost</pre></div></div>

<p>Now, let&#8217;s write the client class. The client should connect to the<br />
bean application and send a request in order to obtain the number of<br />
divisors of a number n. The content of the class is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;"> com.activeeon.demos.ejb.divisors.client</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigInteger</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.InitialContext</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.NamingException</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.activeeon.demos.ejb.divisors.DivisorCounter</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DivisorCounterTester <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">BigInteger</span> bi <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;123456789&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span>
	<span style="color: #009900;">&#123;</span>
	 <span style="color: #003399;">InitialContext</span> ctx <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InitialContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 DivisorCounter calculator <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DivisorCounter<span style="color: #009900;">&#41;</span> ctx.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DivisorCounterBean/remote&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Beginning computation in sequential version&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000066; font-weight: bold;">long</span> beginTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000066; font-weight: bold;">long</span> d<span style="color: #339933;">=</span> calculator.<span style="color: #006633;">countDivisors</span><span style="color: #009900;">&#40;</span>bi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000066; font-weight: bold;">long</span> endTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>d<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; divisors have been found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Answer have been received after &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>endTime<span style="color: #339933;">-</span>beginTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; milliseconds. &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*****************<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Run the client application</p>
<p>On my machine, the output of the client is:</p>
<pre> Beginning computation.
 12 divisors have been found
 Answer have been received after 33570 milliseconds.</pre>
<h1><a id="part_twouse_proactive_scheduler_to_perform_the_computation_on_a_grid" name="part_twouse_proactive_scheduler_to_perform_the_computation_on_a_grid">Part TWO: Use ProActive Scheduler to perform the computation on a grid.</a></h1>
<div>
<p><em> Note</em>: We will start by performing the computation on several JVMs on localhost. Then, if you have some available machines, you can use those ones in order to perform the computation.</p>
<p>I assume you have downloaded the last version of the ProActive Scheduler in the SCHEDULER_HOME folder. (for me this folder is “/home/ProActiveScheduler” )</p>
<p><em> Note:</em> we will go on with the implementation and start the scheduler server later.</p>
<h4><a id="add_proactive_jars" name="add_proactive_jars">Add ProActive jars</a></h4>
<div>
<p>Lets go back to our DivisorCounterEJB project and modify our bean to make it delegate the computation on the grid. First, we add jars for the ProActive Scheduler. As we want to deploy these jars on the JBoss server we will copy them in a lib folder of our project. Create a new Folder called “lib” in the DivisorCounterEJB project. Use your system navigator to navigate to the SCHEDULER_HOME/dist/lib folder. Select and copy the files: bouncycastle.jar,fractal-adl.jar,fractal.jar,javassist.jar, log4j.jar, ProActive_resource-manager.jar, ProActive_scheduler.jar, ProActive.jar. Then go back to the Eclipse Workbench and paste these files into the previously created “lib” folder. Now import these jars into the project (Properties → Java Build Path → Add Jars and select all the jars from the “lib” folder)</p></div>
<h4><a id="modify_the_class_divisorcounterbean" name="modify_the_class_divisorcounterbean">Modify the class DivisorCounterBean</a></h4>
<p>First, we have to establish a connection to the Scheduler. Note that the Scheduler will accept one connection per Thread (this is due to the ProActive ActiveObjects programming pattern). As JBoss will reuse the threads we will have to keep a static variable that contain a mapping between the threads and the connections to the Scheduler. We also add three static variables containing the url of the scheduler, username and password. Lets add the next lines to the DivisorCounterBean class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> schedulerURL <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;//192.168.1.10&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> schedulerUserName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;user1&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> userPassword<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pwd1&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> numberOfTasks <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> UserSchedulerInterface schedulerUI<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> SchedulerAuthenticationInterface schedulerAuth<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> HashMap<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>Long, UserSchedulerInterface<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> connections<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>Long, UserSchedulerInterface<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We will use the  schedulerUI and  schedulerAuth objects in order to connect to the Scheduler and submit a job.</p>
<h4><a id="connect_to_the_scheduler" name="connect_to_the_scheduler">Connect to the scheduler</a></h4>
<p>We will start by writing a private method to connect to the scheduler.</p>
<p><strong>Important Note:</strong> This is is the trickiest thing in this tutorial. First, we need to keep in mind that we need to connect to the ProActive Scheduler and therefore we are now in a ProActive programming model context. the ProActive Scheduler accepts only one connection per caller Thread (this is because the Scheduler accepts one connection per ActiveObject caller, plase see the ProActive programming model if you want to have a deeper understanding of all this). On the other hand, JBoss uses a thread pool in order to serve user requests. So, if at one point, I want to know if my session bean is connected to the Scheduler, I actually need to check if the current Thread has a connection to the Scheduler. I know it sounds complicated but it isn&#8217;t ..</p>
<p>As we need to keep one connection per Thread we will start by verifying if the current thread already has a connection attached, and, if not, create one and ad an entry in the connections HashMap. The method we obtain is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> connectToScheduler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SchedulerException, LoginException
<span style="color: #009900;">&#123;</span>
      	<span style="color: #000066; font-weight: bold;">long</span> threadId <span style="color: #339933;">=</span> <span style="color: #003399;">Thread</span>.<span style="color: #006633;">currentThread</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	schedulerUI <span style="color: #339933;">=</span> DivisorCounterBean.<span style="color: #006633;">connections</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Long</span><span style="color: #009900;">&#40;</span>threadId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>schedulerUI<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;already logged in&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
	 schedulerAuth <span style="color: #339933;">=</span> SchedulerConnection.<span style="color: #006633;">join</span><span style="color: #009900;">&#40;</span>schedulerURL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 schedulerUI <span style="color: #339933;">=</span> schedulerAuth.<span style="color: #006633;">logAsUser</span><span style="color: #009900;">&#40;</span>schedulerUserName, userPassword<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 DivisorCounterBean.<span style="color: #006633;">connections</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Long</span><span style="color: #009900;">&#40;</span>threadId<span style="color: #009900;">&#41;</span>, schedulerUI<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;new Connection to the schdeuler has been created. Nb of connections: &quot;</span><span style="color: #339933;">+</span>connections.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4><a id="add_ejbcreate_method" name="add_ejbcreate_method">Add ejbCreate() method</a></h4>
<p>We want to connect to the scheduler when the bean is created. We&#8217;ll just add a method called ejbCreate() that we annotate with @javax.ejb.Init in which we call the connectToScheduler() method. The method looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@javax.<span style="color: #006633;">ejb</span>.<span style="color: #006633;">Init</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> ejbCreate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Creation of DivicorCounterBean. &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
			connectToScheduler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
		  e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We have connected our bean to the scheduler, we only need now to split<br />
the data, create the Tasks which count the number of divisors, send<br />
them to the Scheduler and addition the results.</p>
<h4><a id="implement_the_task_class" name="implement_the_task_class">Implement the Task class</a></h4>
<p>Lets start by implementing the Task code. A task will take in argument the number n for which we are counting divisors and interval in which we are looking for divisors, given by its lower and upper bounds. A Scheduler Task will have its input data in a HashMap of arguments. The execute method can take as input a list of results of the Tasks on which this task depends on, but, as our tasks don&#8217;t have dependencies, this list will always be empty. Furthermore, the code for the Task class it is easy to understand. Here it is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.activeeon.demos.ejb.divisors</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigInteger</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.objectweb.proactive.core.util.wrapper.StringWrapper</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ow2.proactive.scheduler.common.task.TaskResult</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DivisorsTask <span style="color: #000000; font-weight: bold;">extends</span> org.<span style="color: #006633;">ow2</span>.<span style="color: #006633;">proactive</span>.<span style="color: #006633;">scheduler</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">task</span>.<span style="color: #006633;">executable</span>.<span style="color: #006633;">JavaExecutable</span><span style="color: #009900;">&#123;</span>
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Serializable</span> execute<span style="color: #009900;">&#40;</span>TaskResult... <span style="color: #006633;">arg0</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> lowerBoundString <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getArgs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LowerBound&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">BigInteger</span> lowerBound <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span>lowerBoundString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//BigInteger fromNumbber = new BigInteger(from);</span>
	<span style="color: #003399;">String</span> upperBoundString <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getArgs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UpperBound&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">BigInteger</span> upperBound <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span>upperBoundString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">String</span> n <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getArgs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">BigInteger</span> nNumber <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">long</span> divisors <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span> i <span style="color: #339933;">=</span> lowerBound<span style="color: #339933;">;</span> i.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span>upperBound<span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">=</span>i.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>nNumber.<span style="color: #006633;">remainder</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span>.<span style="color: #006633;">ZERO</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			divisors<span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Long</span><span style="color: #009900;">&#40;</span>divisors<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	StringWrapper sw <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringWrapper<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> sw<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Our bean is almost finished. We only need to create a job (which is formed of  tasks) and submit it to the Scheduler.</p>
<p>Lets first add to our com.activeeon.demos.ejb.divisors.DivisorCounter interface a method</p>
<pre>public long distributedCountDivisors(BigInteger n);</pre>
<p>We will create a TaskFlowJob (which is a job that can contain one or more task(s) ), will add tasks to this job and then submit it to the scheduler.</p>
<h4><a id="implement_the_distributedcountdivisors_method" name="implement_the_distributedcountdivisors_method">Implement the distributedCountDivisors method</a></h4>
<ul>
<li> job creation</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">TaskFlowJob job <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TaskFlowJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
job.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DivisorCounter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">//if error occur on one task, should we cancel the job?</span>
      job.<span style="color: #006633;">setCancelOnError</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     job.<span style="color: #006633;">setDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Job sent from an EJB application deployed on JBoss v4.2 AS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<ul>
<li> classpath</li>
</ul>
<p>As we need to execute the code in the DivisorTask class that we<br />
previously wrote, we need to have this class on the remote machines (on<br />
the nodes). We need so, either to provide a classpath containing the<br />
DivisorTask class when starting the nodes, either to provide this<br />
classpath to the job by using a JobEnvironment object. This is done by<br />
these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">		<span style="color: #003399;">String</span> appClassPath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">File</span> appMainFolder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>DivisorCounterBean.<span style="color: #000000; font-weight: bold;">class</span>
					.<span style="color: #006633;">getProtectionDomain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCodeSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLocation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #006633;">toURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			appClassPath <span style="color: #339933;">=</span> appMainFolder.<span style="color: #006633;">getAbsolutePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>URISyntaxException e1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>
					.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;An exception has occured while setting the job classpath.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			e1.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		JobEnvironment je <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JobEnvironment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		je.<span style="color: #006633;">setJobClasspath</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> appClassPath <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		job.<span style="color: #006633;">setEnv</span><span style="color: #009900;">&#40;</span>je<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<ul>
<li> creation of the tasks</li>
</ul>
<p>We will create m intervals in which we will be counting the divisors of the number n. To do so, we will split the interval (2, n/2+1) into m intervals (where m = numberOfTasks, statically defined), and provide one interval to each Task. An interval is defined by its lower bound and its upper bound. Our tasks will receive then 3 arguments: lower bound, upper bound and the number n. For two values lower bound and upper bound already computed, the code which creates the task is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">JavaTask jt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JavaTask<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jt.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Divisors Task &quot;</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jt.<span style="color: #006633;">setDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Count divisors of number &quot;</span><span style="color: #339933;">+</span>n.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; within the interval (&quot;</span><span style="color: #339933;">+</span>lowerBound.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; , &quot;</span><span style="color: #339933;">+</span>upperBound.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LowerBound&quot;</span>,lowerBound.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UpperBound&quot;</span>, upperBound.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;n&quot;</span>, n.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jt.<span style="color: #006633;">setPreciousResult</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jt.<span style="color: #006633;">setExecutableClassName</span><span style="color: #009900;">&#40;</span>DivisorsTask.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
job.<span style="color: #006633;">addTask</span><span style="color: #009900;">&#40;</span>jt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<ul>
<li> submit the job to the scheduler and wait for the results</li>
</ul>
<p>Once the job is submitted, we only have to wait for the results, addition them when arrived and send the final result to the client.<br />
<strong>Note</strong>: In this implementation we will just keep waiting (perform a Thread sleep) and ask the scheduler every n milliseconds if the result is available. A better solution would be to subscribe a listener to the Scheduler in order to be notified, by the Scheduler, when the result is available. See the <strong>Advanced</strong> section at the end of this article.</p>
<p>The code of the method is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> distributedCountDivisors<span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>schedulerUI <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			connectToScheduler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>LoginException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>SchedulerException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">long</span> nbOfDivisors <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		TaskFlowJob job <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TaskFlowJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		job.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DivisorCounter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// if error occur on one task, should we cancel the job?</span>
		job.<span style="color: #006633;">setCancelOnError</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		job
				.<span style="color: #006633;">setDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Job sent from an EJB application deployed on JBoss v4.2 AS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// handle classpath</span>
		<span style="color: #003399;">String</span> appClassPath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">File</span> appMainFolder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>DivisorCounterBean.<span style="color: #000000; font-weight: bold;">class</span>
					.<span style="color: #006633;">getProtectionDomain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCodeSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLocation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #006633;">toURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			appClassPath <span style="color: #339933;">=</span> appMainFolder.<span style="color: #006633;">getAbsolutePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>URISyntaxException e1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>
					.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;An exception has occured while setting the job classpath.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			e1.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		JobEnvironment je <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JobEnvironment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		je.<span style="color: #006633;">setJobClasspath</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> appClassPath <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		job.<span style="color: #006633;">setEnv</span><span style="color: #009900;">&#40;</span>je<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">BigInteger</span> halfN <span style="color: #339933;">=</span> n.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span>.<span style="color: #006633;">ONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">BigInteger</span> intervalSize <span style="color: #339933;">=</span> halfN.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span>
				DivisorCounterBean.<span style="color: #006633;">numberOfTasks</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">BigInteger</span> lowerBound<span style="color: #339933;">;</span>
		<span style="color: #003399;">BigInteger</span> upperBound<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> DivisorCounterBean.<span style="color: #006633;">numberOfTasks</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			lowerBound <span style="color: #339933;">=</span> intervalSize.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>
					<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>
					<span style="color: #003399;">BigInteger</span>.<span style="color: #006633;">ONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			upperBound <span style="color: #339933;">=</span> lowerBound.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>intervalSize
					.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span>.<span style="color: #006633;">ONE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// for the last interval</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>upperBound.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span>halfN<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				upperBound <span style="color: #339933;">=</span> halfN<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">// Creating the Divisors Task for the interval (upperBound,</span>
			<span style="color: #666666; font-style: italic;">// lowerBound)</span>
			JavaTask jt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JavaTask<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			jt.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Divisors Task &quot;</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			jt.<span style="color: #006633;">setDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Count divisors of number &quot;</span> <span style="color: #339933;">+</span> n.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; within the interval (&quot;</span> <span style="color: #339933;">+</span> lowerBound.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; , &quot;</span> <span style="color: #339933;">+</span> upperBound.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LowerBound&quot;</span>, lowerBound.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UpperBound&quot;</span>, upperBound.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			jt.<span style="color: #006633;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;n&quot;</span>, n.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			jt.<span style="color: #006633;">setPreciousResult</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			jt.<span style="color: #006633;">setExecutableClassName</span><span style="color: #009900;">&#40;</span>DivisorsTask.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			job.<span style="color: #006633;">addTask</span><span style="color: #009900;">&#40;</span>jt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		JobId jobId <span style="color: #339933;">=</span> schedulerUI.<span style="color: #006633;">submit</span><span style="color: #009900;">&#40;</span>job<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The Job has been submited to the scheduler.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		JobResult jResult <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">boolean</span> keepWaiting <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>keepWaiting <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>jResult <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				jResult <span style="color: #339933;">=</span> schedulerUI.<span style="color: #006633;">getJobResult</span><span style="color: #009900;">&#40;</span>jobId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>SchedulerException se<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				keepWaiting <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				se.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The results of the job are: &quot;</span> <span style="color: #339933;">+</span> jResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		HashMap<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String, TaskResult<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> results <span style="color: #339933;">=</span> jResult.<span style="color: #006633;">getAllResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>TaskResult t <span style="color: #339933;">:</span> results.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">String</span> rs <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>StringWrapper<span style="color: #009900;">&#41;</span> t.<span style="color: #006633;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">int</span> r <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span>rs<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">intValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			nbOfDivisors <span style="color: #339933;">+=</span> r<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// we add n as divisor of n</span>
		nbOfDivisors<span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</span>
		e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">return</span> nbOfDivisors<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Call the distributed method from the client</p>
<p>Lets call, from the client, the 2 methods (the sequential one and distributed one, in order to compare the computation times) Add the next lines to the main method oof the DivisorCounterTester class in the DivisorCounterClient project:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Begining computation in distributed version&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	beginTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	d<span style="color: #339933;">=</span> calculator.<span style="color: #006633;">distributedCountDivisors</span><span style="color: #009900;">&#40;</span>bi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	endTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>d<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; divisors have been found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Answer have been received after &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>endTime<span style="color: #339933;">-</span>beginTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; milliseconds. &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*****************/n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note: your client might not compile now because it does not now the distributedCountDivisors method. Just export again the DivisorCounterEJB project as a jar and import it again in the client project.</p>
<h4><a id="start_the_scheduler_and_run_your_application" name="start_the_scheduler_and_run_your_application">Start the scheduler and run your application</a></h4>
<p><strong>Important Note:</strong> before you do this, you should change the Proactive default rmi port in order not to have a conflict with JBoss port.</p>
<p>In your root folder you should have a folder called &#8220;.proactive&#8221; . Go inside and edit the file called ProActiveConfiguration.xml. Look for the line which defines the property &#8220;proactive.rmi.port&#8221; and change its value (1099 by default). In my case I put it to 1605 (it&#8217;s my credit card pin code <img src='http://www.activeeon.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )<br />
Proactive Scheduler relies on a Resource Manager server.</p>
<ul>
<li> Start the Resource manager:</li>
</ul>
<p>Open a command console and navigate to SCHEDULER_HOME/bin/unix (../windows if you are working on a windows system). Type ./startRM.sh (startRM.bat for windows) The Resource Manager will start and will deploy, by default, 4 nodes (jvm&#8217;s) on the localhost.</p>
<ul>
<li>
<div>Start the scheduler</div>
</li>
</ul>
<p>Open a new command console and navigate to SCHEDULER_HOME/bin/unix (../windows) Only for the first time your are launching the scheduler, you have to create its database (which contains information about jobs, their tasks, results, etc..) Type: startScheduler_newDB.sh (.. bat for windows) This will create the database and start the scheduler which will connect to the resource manager in order to obtain resources and deploy tasks.</p>
<ul>
<li> Start the JBoss server</li>
</ul>
<p><strong>Important Note</strong> add this property to the jboss launcher:   -Dlog4j.configuration=JBOSS_HOME/default/conf/jboss-log4j.xml<br />
<code><br />
./run.sh -Dlog4j.configuration=JBOSS_HOME/default/conf/jboss-log4j.xml</code></p>
<p>You have to do this in order to force ProActive use the log4j.configuration<br />
file of JBoss (in order not to have logger conflicts in the JBoss JVM).</p>
<ul>
<li>Run your client application.</li>
</ul>
<p>On my machine the output of the client is:</p>
<pre>Beginning computation in sequential version
12 divisors have been found
Answer have been received after 35638 milliseconds.
*****************</pre>
<pre>Beginning computation in distributed version
12 divisors have been found
Answer have been received after 18980 milliseconds.
*****************</pre>
<p>This is it, if you followed me until this point, you have your session bean which delegates the computation on a set of JVMs. I only have few words to add about adding remote resources (nodes), starting the Scheduler itself on a remote machine, using the <acronym title="Graphical User Interface">GUI</acronym> for administration of the Scheduler and Resource manager.</p>
<h2><a id="complementary_information" name="complementary_information">Additional Information</a></h2>
<h4><a id="scheduler_and_resource_manager_administration" name="scheduler_and_resource_manager_administration">Scheduler and Resource Manager Administration</a></h4>
<div>
<ul>
<li>
<div>First start the Resource Manager administration <acronym title="Graphical User Interface">GUI</acronym>. Right Click → connect to Resource Manager.</div>
</li>
</ul>
<p>You should see now the four JVMs that have been deployed by default on your machine.</p>
<ul>
<li>
<div>Now start the Scheduler administration <acronym title="Graphical User Interface">GUI</acronym>.  Right Click → connect to scheduler ..</div>
</li>
</ul>
<ul>
<li>
<div>Relaunch your application</div>
</li>
</ul>
<p>You can remove, if you want to, from the client, the call to the sequential method. You should see that, on the Resource Manager <acronym title="Graphical User Interface">GUI</acronym> some of the resources are marked as “occupied” (yellow), and, on the Scheduler Administration <acronym title="Graphical User Interface">GUI</acronym> you can see your job running.</div>
<h4><a id="add_remote_resources" name="add_remote_resources">Add remote resources</a></h4>
<div>
<p>Connect to another machine of your network. Open a command console and navigate to SCHEDULER_HOME/bin folder Type ./startNode.sh myNode The ProActive Runtime (jvm, node) is launched and the url of the node is printed at stdout. Go to the Resource Manager, right click → Add node(s) and type the url of the node you have just started. (you can also remove one node from the localhost). You can rerun your client application now.</p></div>
<h4><a id="running_the_scheduler_and_resource_manager_on_remote_machines" name="running_the_scheduler_and_resource_manager_on_remote_machines">Running the scheduler and resource manager on remote machines</a></h4>
<div>
<p>You can start the resource manager on a machine M1 and the scheduler on a machine M2. When you start the Scheduler, make sure you give the good url of the Resource Manager in argument (option -u //IpOfM2). Then modify the schedulerURL static variable in the DivisorCounterBean class with the ip of the machine M2. Export again the jar file of your application. Stop the JBoss server. When you start the JBoss server pass as argument “–host ip” where ip is the ip of your machine in the local network. Now run your client application. The bean now connects to the machine M2 on the scheduler server (which itself connects to the machine M1 to the Resource Manager server in order to obtain resources.). Run your client application again.</p></div>
<h4><a id="remarks" name="remarks">Remarks</a></h4>
<div>
<ul>
<li>
<div>You can specify your bean as “Stateless”</div>
</li>
</ul>
<p>(for our example, a stateless bean would do just fine). Note that in this case, the ejbCreate() method will not be called (and, the SchedulerUI will be instantiated at the first call of distributedCountDivisors method)</p>
<ul>
<li>
<div>In this example we have defined the number of task in a static variable.</div>
</li>
</ul>
<p>This should be defined by taking in account the size of the number for which we are counting divisors</p>
<ul>
<li>
<div>The results of the job can be obtained one by one.</div>
</li>
</ul>
<p>Each result can be obtained as soon as the respective task has finished. This could be useful if, for instance, you are using a web client and want to refresh the page after each result (write on the web page something like “23 divisors have been found until now. ”).</p>
<ul>
<li>
<div>ProActive Scheduler provides the possibility to define also Native Tasks</div>
</li>
</ul>
<p>(instead of Java Tasks), which means that they execute native applications.</p>
<ul>
<li>
<div>Dependencies between tasks can also be defined</div>
</li>
</ul>
<p>(Task k use the results of Task i and Task j)</p></div>
<h4><a id="fast_test" name="fast_test">Fast Test</a></h4>
<p>I have launched the application by using 4 nodes/JVMs (2 local JVMs and two JVMs on different machines). The output of my client application is:</p>
<pre>Beginning computation in sequential version
12 divisors have been found
Answer have been received after 33484 milliseconds.
*****************
Beginning computation in distributed version
12 divisors have been found
Answer have been received after 9163 milliseconds.
*****************</pre>
<p>So, by distributing the computation on 4 JVMs, I had an answering time from the server 3.65 times lower then the time for the sequential application.</p>
<h1><a id="Advanced"> Advanced </a></h1>
<p>In the distributedCountDivisors method from the DivisorCounterBean class we have created a job, submitted it to the ProActive Scheduler, and then, within a loop, we have &#8220;asked&#8221; the Scheduler, every n milliseconds, if it our result is available. This solution goes well for our little example but a much better solution would be to subscribe a Listener to the Scheduler in order to be notified when the result is available.<br />
In order to do so, you should create a Listener by implementing the org.ow2.proactive.scheduler.common.scheduler.SchedulerEventListener. You must subscribe your listener to the Scheduler, and let your listener play the role of proxy for the Scheduler. Moreover, your Listener should be an active object or, at least should belong to an Active Object.<br />
But this requires the understanding of some ProActive concepts. I might come back on this advanced solution in some other article. Do not hesitate to let me know if you&#8217;re planning to implement this solution, I might have some useful tips for you (especially about how to configure Jboss and ProActive Scheduler in order to solve some ClassLoading conflicts that might appear)</p>
<h2><a id="documentation" name="documentation">Documentation</a></h2>
<div>
<p>See JBoss at <a title="http://www.jboss.org" rel="nofollow" href="http://www.jboss.org/">www.jboss.org</a></p>
<p>See ProActive and ProActive Scheduler at <a title="http://www.activeeon.com" rel="nofollow" href="http://www.activeeon.com/">www.activeeon.com</a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.activeeon.com/blog/all/integration/performing-grid-computing-from-an-application-server/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

