Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 1 | |
| 2 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <HTML> |
| 4 | |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 5 | <!-- Mirrored Site: felix.apache.org. File: /site/lifecycle-callback-handler.html. Date: Mon, 13 Oct 2008 06:53:06 GMT --> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 6 | <HEAD> |
| 7 | <TITLE>Apache Felix - Lifecycle Callback Handler</TITLE> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 8 | <LINK rel="stylesheet" href="media.data/site.css" type="text/css" media="all"> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 9 | <META http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
| 10 | </HEAD> |
| 11 | <BODY> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 12 | <DIV class="title"><DIV class="logo"><A href="index.html"><IMG border="0" alt="Apache Felix" src="media.data/logo.png"></A></DIV><DIV class="header"><A href="http://www.apache.org/"><IMG border="0" alt="Apache" src="media.data/apache.png"></A></DIV></DIV> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 13 | <DIV class="menu"> |
| 14 | <UL> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 15 | <LI><A href="news.html" title="news">news</A></LI> |
| 16 | <LI><A href="license.html" title="license">license</A></LI> |
| 17 | <LI><SPAN class="nobr"><A href="downloads.html" title="Visit page outside Confluence" rel="nofollow">downloads<SUP><IMG class="rendericon" src="../../cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></LI> |
| 18 | <LI><A href="documentation.html" title="documentation">documentation</A></LI> |
| 19 | <LI><A href="mailinglists.html" title="mailinglists">mailing lists</A></LI> |
| 20 | <LI><A href="contributing.html" title="Contributing">contributing</A></LI> |
| 21 | <LI><SPAN class="nobr"><A href="http://www.apache.org/" title="Visit page outside Confluence" rel="nofollow">asf<SUP><IMG class="rendericon" src="../../cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></LI> |
| 22 | <LI><SPAN class="nobr"><A href="http://www.apache.org/foundation/sponsorship.html" title="Visit page outside Confluence" rel="nofollow">sponsorship<SUP><IMG class="rendericon" src="../../cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></LI> |
| 23 | <LI><SPAN class="nobr"><A href="http://www.apache.org/foundation/thanks.html" title="Visit page outside Confluence" rel="nofollow">sponsors<SUP><IMG class="rendericon" src="../../cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 24 | <!-- ApacheCon Ad --> |
| 25 | <IFRAME src="http://www.apache.org/ads/button.html" style="border-width:0; float: left" frameborder="0" scrolling="no" width="135" height="135"></IFRAME> |
| 26 | <P style="height: 100px"> |
| 27 | <!-- ApacheCon Ad --></LI> |
| 28 | </UL> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 29 | </DIV> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 30 | <DIV class="main"> |
| 31 | <TABLE class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><TBODY><TR> |
| 32 | <TD class="confluenceTd" valign="top" width="80%"> |
| 33 | <H1><A name="LifecycleCallbackHandler-Lifecyclecallbacks"></A>Life cycle callbacks</H1> |
| 34 | |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 35 | <P>It is often necessary to create a POJO object as soon the instance becomes valid. Moreover, it is also often needed to be able to stop it nicely. The lifecycle callback handler allows invoking methods (callbacks) on the instance when instance's state changed. For example, it allows invoking a <EM>start</EM> method when the instance becomes valid and a <EM>stop</EM> method when the instance becomes invalid. Moreover, this handler allows the creation of <EM>immediate</EM> component. This page presents how to use this handler.</P> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 36 | |
| 37 | <H2><A name="LifecycleCallbackHandler-InstanceLifecycle"></A>Instance Lifecycle</H2> |
| 38 | |
| 39 | <P>IPOJO instances have a very simple lifecycle. This lifecycle contains two states: INVALID and VALID. Once an instance is created, this instance can only be valid if all its plugged handlers are valid. For example, an instance requiring a service (and so using the dependency handler) cannot be valid if the required service is unavailable. Indeed, the dependency handler will be invalid.</P> |
| 40 | |
| 41 | <P>An instance starts and stops in the invalid state.</P> |
| 42 | |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 43 | <P><IMG src="lifecycle-callback-handler.data/lifecycle.png" align="absmiddle" border="0"></P> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 44 | |
| 45 | <H1><A name="LifecycleCallbackHandler-Lifecyclecallback"></A>Lifecycle callback</H1> |
| 46 | |
| 47 | <P>This handler supports two kinds of callback. The INVALID=>VALID callback are invoked when the instance becomes valid (at starting or when an event allows the instance to become valid). The VALID=>INVALID callback are invoked when the instance becomes invalid (at stopping or when an event invalids the instance).</P> |
| 48 | |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 49 | <P><IMG src="lifecycle-callback-handler.data/callback.png" align="absmiddle" border="0"></P> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 50 | |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 51 | <P><IMG src="lifecycle-callback-handler.data/callbackOnTransition.png" align="absmiddle" border="0"></P> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 52 | |
| 53 | <H2><A name="LifecycleCallbackHandler-Anexample"></A>An example</H2> |
| 54 | |
| 55 | <P>Let's take an example. The following class requires a FooService and has two lifecycle callbacks: start and stop.</P> |
| 56 | <DIV class="code"><DIV class="codeContent"> |
| 57 | <PRE class="code-java"><SPAN class="code-keyword">public</SPAN> class Foo { |
| 58 | FooService fs; |
| 59 | <SPAN class="code-keyword">private</SPAN> void start() { |
| 60 | <SPAN class="code-comment">// Starting method |
| 61 | </SPAN> <SPAN class="code-comment">//... |
| 62 | </SPAN> fs.foo(); |
| 63 | <SPAN class="code-comment">//... |
| 64 | </SPAN> } |
| 65 | <SPAN class="code-keyword">protected</SPAN> void stop() { |
| 66 | <SPAN class="code-comment">// Stopping method |
| 67 | </SPAN> <SPAN class="code-keyword">if</SPAN>(fs!=<SPAN class="code-keyword">null</SPAN>) { fs.foo(); } |
| 68 | } |
| 69 | }</PRE> |
| 70 | </DIV></DIV> |
| 71 | <P>For this class, we define the following component type:</P> |
| 72 | <DIV class="code"><DIV class="codeContent"> |
| 73 | <PRE class="code-xml"><SPAN class="code-tag"><component className=<SPAN class="code-quote">"...Foo"</SPAN>></SPAN> |
| 74 | <SPAN class="code-tag"><dependency field=<SPAN class="code-quote">"fs"</SPAN>/></SPAN> |
| 75 | <SPAN class="code-tag"><callback transition=<SPAN class="code-quote">"validate"</SPAN> method=<SPAN class="code-quote">"start"</SPAN>/></SPAN> |
| 76 | <SPAN class="code-tag"><callback transition=<SPAN class="code-quote">"invalidate"</SPAN> method=<SPAN class="code-quote">"stop"</SPAN>/></SPAN> |
| 77 | <SPAN class="code-tag"></component></SPAN></PRE> |
| 78 | </DIV></DIV> |
| 79 | <P>When an instance of this component type is created, the start method is called as soon as the <EM>Foo</EM> Service (service requirement) becomes available. If the <EM>Foo</EM> Service is no more available or when the instance is stopped, the stop method is called.</P> |
| 80 | |
| 81 | <P>The invoked methods have no argument, but could be private, protected or public. Public methods can be in parent classes too. Moreover, the INVALID=>VALID (validate) method can use service dependencies (the instance becomes valid means that all required services are available); however, in the stop method (invalidate) it is possible that one of these dependency can be <EM>null</EM>. Indeed, the departure of a service can be the cause of the instance invalidation.</P> |
| 82 | |
| 83 | <H2><A name="LifecycleCallbackHandler-Managingthreads"></A>Managing threads</H2> |
| 84 | |
| 85 | <P>One usage of lifecycle callback is when the instance needs to create threads. Indeed, the thread can be created in the validate callback, and stopped in the invalidate method. The next class shows an example of a class handling a thread by using lifecycle callbacks.</P> |
| 86 | <DIV class="code"><DIV class="codeContent"> |
| 87 | <PRE class="code-java"><SPAN class="code-keyword">public</SPAN> class HelloRequesterImpl <SPAN class="code-keyword">implements</SPAN> <SPAN class="code-object">Runnable</SPAN> { |
| 88 | |
| 89 | <SPAN class="code-keyword">final</SPAN> <SPAN class="code-keyword">static</SPAN> <SPAN class="code-object">int</SPAN> DELAY=10000; |
| 90 | HelloService\[\] m_hello;&nbsp; <SPAN class="code-comment">// Service Dependency |
| 91 | </SPAN> <SPAN class="code-object">boolean</SPAN> end; |
| 92 | |
| 93 | <SPAN class="code-keyword">public</SPAN> void run() { |
| 94 | <SPAN class="code-keyword">while</SPAN> (\!end) { |
| 95 | <SPAN class="code-keyword">try</SPAN> { |
| 96 | <SPAN class="code-keyword">synchronized</SPAN> (<SPAN class="code-keyword">this</SPAN>) { |
| 97 | <SPAN class="code-keyword">for</SPAN>(<SPAN class="code-object">int</SPAN> i = 0; i < m_hello.length; i++) { |
| 98 | <SPAN class="code-object">System</SPAN>.out.println(m_hello\[i\].sayHello(<SPAN class="code-quote">"Clement"</SPAN>)); |
| 99 | } |
| 100 | } |
| 101 | <SPAN class="code-object">Thread</SPAN>.sleep(DELAY); |
| 102 | } <SPAN class="code-keyword">catch</SPAN> (InterruptedException ie) { |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 103 | /* will recheck quit */ |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | <SPAN class="code-keyword">public</SPAN> void starting() { |
| 109 | <SPAN class="code-object">Thread</SPAN> T = <SPAN class="code-keyword">new</SPAN> <SPAN class="code-object">Thread</SPAN>(<SPAN class="code-keyword">this</SPAN>); |
| 110 | end = <SPAN class="code-keyword">false</SPAN>; |
| 111 | T.start(); |
| 112 | } |
| 113 | |
| 114 | <SPAN class="code-keyword">public</SPAN> void stopping() { end = <SPAN class="code-keyword">true</SPAN>; }</PRE> |
| 115 | </DIV></DIV> |
| 116 | <P>For this component type, the metadata are :</P> |
| 117 | <DIV class="code"><DIV class="codeContent"> |
| 118 | <PRE class="code-xml"><SPAN class="code-tag"><component className=<SPAN class="code-quote">"... HelloRequesterImpl"</SPAN>></SPAN> |
| 119 | <SPAN class="code-tag"><dependency field=<SPAN class="code-quote">"HelloService"</SPAN>/></SPAN> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 120 | <SPAN class="code-tag"><callback transition=<SPAN class="code-quote">"validate"</SPAN> method=<SPAN class="code-quote">"starting"</SPAN>/></SPAN> |
| 121 | <SPAN class="code-tag"><callback transition=<SPAN class="code-quote">"invalidate"</SPAN> method=<SPAN class="code-quote">"stopping"</SPAN>/></SPAN> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 122 | <SPAN class="code-tag"></component></SPAN></PRE> |
| 123 | </DIV></DIV> |
| 124 | |
| 125 | <H2><A name="LifecycleCallbackHandler-Immediatecomponent"></A>Immediate component</H2> |
| 126 | |
| 127 | <P>An instance of an immediate component type is instantiated as soon it becomes valid. It means that, when the instance becomes valid, the constructor of the implementation class is called. This can replace the validate callback. However, it stills a difference between the immediate and the validate callback. The constructor is call only once time. The validate callback is re-called each time the instance becomes valid.</P> |
| 128 | |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 129 | <P><IMG src="lifecycle-callback-handler.data/immediate.png" align="absmiddle" border="0"></P> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 130 | |
| 131 | <P>However as there is no destructor in Java, the invalidate callback is necessary if some actions are needed when stopping.</P> |
| 132 | |
| 133 | <H2><A name="LifecycleCallbackHandler-Callbackonseveralobjects"></A>Callback on several objects</H2> |
| 134 | |
| 135 | <P>If you instance has created several objects (called the implementation class constructor several times), the callback is called on each object in the creation order.</P></TD> |
| 136 | <TD class="confluenceTd" valign="top" width="20%"> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 137 | <H6><A name="LifecycleCallbackHandler-Overview"></A><B>Overview</B></H6> |
| 138 | <UL> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 139 | <LI><A href="apache-felix-ipojo.html" title="Apache Felix iPOJO">Home Page</A></LI> |
| 140 | <LI><A href="apache-felix-ipojo-feature-overview.html" title="Apache Felix iPOJO Feature Overview">iPOJO Feature Overview</A></LI> |
| 141 | <LI><A href="download.html" title="Download">Download & Install </A></LI> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 142 | </UL> |
| 143 | |
| 144 | |
| 145 | <H6><A name="LifecycleCallbackHandler-GettingStarted"></A><B>Getting Started</B></H6> |
| 146 | <UL> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 147 | <LI><A href="ipojo-in-10-minutes.html" title="iPOJO in 10 minutes">iPOJO in 10 minutes</A></LI> |
| 148 | <LI><A href="ipojo-hello-word-maven-based-tutorial.html" title="iPOJO Hello Word (Maven-Based) tutorial">iPOJO Hello Word (Maven-Based) tutorial</A></LI> |
| 149 | <LI><A href="ipojo-advanced-tutorial.html" title="iPOJO Advanced Tutorial">iPOJO Advanced Tutorial</A></LI> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 150 | </UL> |
| 151 | |
| 152 | |
| 153 | <H6><A name="LifecycleCallbackHandler-UserGuide"></A><B>User Guide</B></H6> |
| 154 | <UL> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 155 | <LI><A href="describing-components.html" title="Describing components">Describing components (handler list) </A></LI> |
| 156 | <LI><A href="how-to-use-ipojo-annotations.html" title="How to use iPOJO Annotations">How to use iPOJO Annotations</A></LI> |
| 157 | <LI><A href="using-xml-schemas.html" title="Using XML Schemas">Using XML Schemas</A></LI> |
| 158 | <LI><A href="ipojo-advanced-topics.html" title="iPOJO Advanced Topics">Advanced Topics</A></LI> |
| 159 | <LI><A href="ipojo-faq.html" title="iPOJO FAQ">FAQ</A></LI> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 160 | </UL> |
| 161 | |
| 162 | |
| 163 | <H6><A name="LifecycleCallbackHandler-Tools"></A><B>Tools</B></H6> |
| 164 | <UL> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 165 | <LI><A href="ipojo-eclipse-plug-in.html" title="iPOJO Eclipse Plug-in">iPOJO Eclipse Plug-in</A></LI> |
| 166 | <LI><A href="ipojo-ant-task.html" title="iPOJO Ant Task">iPOJO Ant Task</A></LI> |
| 167 | <LI><A href="ipojo-maven-plug-in.html" title="iPOJO Maven Plug-in">iPOJO Maven Plug-in</A></LI> |
| 168 | <LI><A href="ipojo-concepts-overview.html" title="iPOJO Concepts Overview">iPOJO concepts overview</A></LI> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 169 | </UL> |
| 170 | |
| 171 | |
| 172 | <H6><A name="LifecycleCallbackHandler-DeveloperGuide"></A><B>Developer Guide</B></H6> |
| 173 | <UL> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 174 | <LI>API: <SPAN class="nobr"><A href="http://people.apache.org/~clement/ipojo/api/0.8/" title="Visit page outside Confluence" rel="nofollow">0.8<SUP><IMG class="rendericon" src="../../cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></LI> |
| 175 | <LI><A href="how-to-write-your-own-handler.html" title="How to write your own handler">How to write your own handler</A></LI> |
| 176 | <LI><A href="how-to-use-ipojo-manipulation-metadata.html" title="How to use iPOJO Manipulation Metadata">How to use iPOJO Manipulation Metadata</A></LI> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 177 | </UL> |
| 178 | |
| 179 | |
| 180 | <H6><A name="LifecycleCallbackHandler-Misc%26Contact"></A><B>Misc & Contact</B></H6> |
| 181 | <UL> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 182 | <LI><A href="apache-felix-ipojo-issuestracker.html" title="apache-felix-ipojo-issuestracker">Issues Tracker</A></LI> |
| 183 | <LI><A href="apache-felix-ipojo-supportedvms.html" title="apache-felix-ipojo-supportedVMs">Supported JVMs</A></LI> |
| 184 | <LI><A href="apache-felix-ipojo-supportedosgi.html" title="apache-felix-ipojo-supportedOSGi">Supported OSGi Implementations</A></LI> |
| 185 | <LI><A href="future-ideas.html" title="Future Ideas">Future Ideas</A></LI> |
| 186 | <LI><A href="contact.html" title="Contact">Contact</A></LI> |
| 187 | <LI><A href="related-works.html" title="Related Works">Related Works</A></LI> |
| 188 | <LI><A href="article-presentations.html" title="Article & Presentations">Article & Presentations</A></LI> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 189 | </UL> |
| 190 | |
| 191 | |
| 192 | <HR> |
| 193 | <DIV class="" align="center"> |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 194 | <P><SPAN class="nobr"><A href="http://cwiki.apache.org/confluence/createrssfeed.action?types=blogpost&statuses=created&statuses=modified&spaces=FELIX&labelString=iPOJO&rssType=atom&maxResults=10&timeSpan=5&publicFeed=true&title=iPOJO%20Atom%20Feed" title="Stay tuned!" rel="nofollow"><IMG src="../../cwiki.apache.org/confluence/images/icons/feed-icon-32x32.png" align="absmiddle" border="0"><SUP><IMG class="rendericon" src="../../cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></P></DIV></TD></TR></TBODY></TABLE> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 195 | </DIV> |
| 196 | </BODY> |
| 197 | |
Clement Escoffier | 8350751 | 2008-10-13 07:33:03 +0000 | [diff] [blame] | 198 | <!-- Mirrored Site: felix.apache.org. File: /site/lifecycle-callback-handler.html. Date: Mon, 13 Oct 2008 06:53:06 GMT --> |
Clement Escoffier | 6b928e9 | 2008-05-16 20:33:54 +0000 | [diff] [blame] | 199 | </HTML> |