<?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>Java, Programming, Struts2, jQuery, Linux and more &#187; springframework</title>
	<atom:link href="http://www.jgeppert.com/tag/springframework/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jgeppert.com</link>
	<description>Johannes Geppert</description>
	<lastBuildDate>Thu, 09 Feb 2012 16:38:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Mit dem Spring Framework einen Scheduler ausführen</title>
		<link>http://www.jgeppert.com/2008/07/mit-dem-spring-framework-einen-scheduler-ausfuhren/</link>
		<comments>http://www.jgeppert.com/2008/07/mit-dem-spring-framework-einen-scheduler-ausfuhren/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 09:35:25 +0000</pubDate>
		<dc:creator>jogep</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[quartz]]></category>
		<category><![CDATA[scheduler]]></category>
		<category><![CDATA[springframework]]></category>
		<category><![CDATA[timertask]]></category>

		<guid isPermaLink="false">http://www.jgeppert.com/?p=125</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.jgeppert.com/2008/07/mit-dem-spring-framework-einen-scheduler-ausfuhren/' addthis:title='Mit dem Spring Framework einen Scheduler ausführen '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Spring bietet uns die Möglichkeit, wiederkehrende Aufgaben auszuführen. Die einfachste Variante einen Scheduler zu implementieren, die ich hier zeigen werde, ist das Ausführen eines TimerTask. Dazu implementiert man als erstes eine Klasse die von TimerTask ableitet. In der Methode run() können wir jetzt unseren Code ausführen. In diesem Beispiel lassen wir uns von Spring gleich [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2008/07/mit-dem-spring-framework-einen-scheduler-ausfuhren/' addthis:title='Mit dem Spring Framework einen Scheduler ausführen ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.jgeppert.com/2008/07/mit-dem-spring-framework-einen-scheduler-ausfuhren/' addthis:title='Mit dem Spring Framework einen Scheduler ausführen '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><a href="http://www.springframework.org/">Spring</a> bietet uns die Möglichkeit, wiederkehrende Aufgaben auszuführen. Die einfachste Variante einen Scheduler zu implementieren, die ich hier zeigen werde, ist das Ausführen eines <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/TimerTask.html">TimerTask</a>.

Dazu implementiert man als erstes eine Klasse die von <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/TimerTask.html">TimerTask</a> ableitet. In der Methode <em>run()</em> können wir jetzt unseren Code ausführen. In diesem Beispiel lassen wir uns von <a href="http://www.springframework.org/">Spring</a> gleich noch ein DAO Objekt übergeben mit dem wir eine Datenbank Operation ausführen können.


<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;">class</span> MyTimerTask <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">TimerTask</span>
<span style="color: #009900;">&#123;</span>
	<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> Log log <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>MyTimerTask.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> MeinDAO meinDAO<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Starte MyTimerTask &quot;</span><span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</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;">// Etwas sinnvolles machen!</span>
		meinDAO.<span style="color: #006633;">deleteAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Beende MyTimerTask &quot;</span><span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</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>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMeinDAO<span style="color: #009900;">&#40;</span>MeinDAO meinDAO<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;">meinDAO</span> <span style="color: #339933;">=</span> meinDAO<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




Jetzt müssen wir nur noch unsere <em>applicationContext.xml</em> um folgende Einträge erweitern.


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">	<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;myTimerTask&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.jgeppert.timer.MyTimerTask&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;meinDAO&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;MeinDAO&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>
&nbsp;
	<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;scheduledTask&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.scheduling.timer.ScheduledTimerTask&quot;</span> <span style="color: #000066;">lazy-init</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #808080; font-style: italic;">&lt;!-- Die Startzeit nach der der TimerTask initial starten soll in Millisekunden --&gt;</span>
		<span style="color: #808080; font-style: italic;">&lt;!-- In diesem Beispiel also nach einer Minute --&gt;</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;delay&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;60000&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!-- Alle wieviel Millisekunden solle der Task wieder laufen --&gt;</span>
		<span style="color: #808080; font-style: italic;">&lt;!-- In diesem Beispiel läuft der Task alle 10 Minuten --&gt;</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;period&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;600000&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
		<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;timerTask&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;myTimerTask&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>
&nbsp;
	<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;timerFactory&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.scheduling.timer.TimerFactoryBean&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;scheduledTimerTasks&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;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;scheduledTask&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;/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></pre></div></div>




So lässt sich also auf einfache Art und Weise ein Timer mit <a href="http://www.springframework.org/">Spring</a> ausführen. Mehr Informationen und komplexere Beispiele mit dem Quartz Scheduler findet ihr in der <a href="http://static.springframework.org/spring/docs/2.5.5/reference/scheduling.html">Dokumentation</a>.<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2008/07/mit-dem-spring-framework-einen-scheduler-ausfuhren/' addthis:title='Mit dem Spring Framework einen Scheduler ausführen ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.jgeppert.com/2008/07/mit-dem-spring-framework-einen-scheduler-ausfuhren/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

