<?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>Radek's Tech Blog &#187; periodical task</title>
	<atom:link href="http://urbas.tk/index.php/tag/periodical-task/feed/" rel="self" type="application/rss+xml" />
	<link>http://urbas.tk</link>
	<description>Radoslaw H. Urbas homepage / blog</description>
	<lastBuildDate>Tue, 01 Jun 2010 20:31:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Defining a background Job to run periodically</title>
		<link>http://urbas.tk/index.php/2009/03/01/defining-a-background-job-to-run-periodically/</link>
		<comments>http://urbas.tk/index.php/2009/03/01/defining-a-background-job-to-run-periodically/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 11:47:25 +0000</pubDate>
		<dc:creator>Radoslaw Urbas</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[earlystartup]]></category>
		<category><![CDATA[eclipse jobs api]]></category>
		<category><![CDATA[periodical task]]></category>

		<guid isPermaLink="false">http://urbas.tk/?p=81</guid>
		<description><![CDATA[One may need to run some task periodically in the background in Eclipse. This background task should run automatically and shouldn&#8217;t block user from regular usage of the application. My proposal for accomplishing this is using Eclipse Jobs API and org.eclipse.ui.startup extension-point. Adding org.eclipse.ui.startup extension: Dependencies: org.eclipse.ui org.eclipse.core.runtime Extension: &#60;?xml version="1.0" encoding="UTF-8"?&#62; &#60;?eclipse version="3.2"?&#62; &#60;plugin&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>One may need to run some task periodically in the background in Eclipse. This background task should run automatically and shouldn&#8217;t block user from regular usage of the application. My proposal for accomplishing this is using Eclipse Jobs API and <em>org.eclipse.ui.startup</em> extension-point.</p>
<ol>
<li>Adding <em>org.eclipse.ui.startup</em> extension:
<ul>
<li>Dependencies:
<ul>
<li><em>org.eclipse.ui</em></li>
<li><em>org.eclipse.core.runtime</em></li>
</ul>
</li>
<li>Extension:
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;?eclipse version="3.2"?&gt;
&lt;plugin&gt;
   &lt;extension point="org.eclipse.ui.startup"&gt;
      &lt;startup class="tk.urbas.eclipse.urbanlife.Startup"&gt;&lt;/startup&gt;
   &lt;/extension&gt;
&lt;/plugin&gt;</pre>
</li>
</ul>
</li>
<li>Implementing Job:

<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;">tk.urbas.eclipse.urbanlife</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.IProgressMonitor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.IStatus</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.Status</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.jobs.Job</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> RefreshDataJob <span style="color: #000000; font-weight: bold;">extends</span> Job <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> RefreshDataJob<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>name<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;">protected</span> IStatus run<span style="color: #009900;">&#40;</span>IProgressMonitor monitor<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Work to do in the background</span>
        <span style="color: #000000; font-weight: bold;">return</span> Status.<span style="color: #006633;">OK_STATUS</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>Scheduling Job to run periodically:

<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;">tk.urbas.eclipse.urbanlife</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.jobs.IJobChangeEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.jobs.Job</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.jobs.JobChangeAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.ui.IStartup</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> Startup <span style="color: #000000; font-weight: bold;">implements</span> IStartup <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> STARTUP_DELAY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 5 seconds delay for first run</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> JOB_INTERVAL <span style="color: #339933;">=</span> <span style="color: #cc66cc;">60000</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Job should run every 60 seconds</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> earlyStartup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Job updateJob <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RefreshDataJob<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Refreshing data in the background&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        updateJob.<span style="color: #006633;">schedule</span><span style="color: #009900;">&#40;</span>STARTUP_DELAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        updateJob.<span style="color: #006633;">addJobChangeListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> JobChangeAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            @Override
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> done<span style="color: #009900;">&#40;</span>IJobChangeEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">done</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                updateJob.<span style="color: #006633;">schedule</span><span style="color: #009900;">&#40;</span>JOB_INTERVAL<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
</ol>
<p>
Let me know if you have any better solution for implementing this. All improvements, suggestions and comments will be greatly appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://urbas.tk/index.php/2009/03/01/defining-a-background-job-to-run-periodically/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

