<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Extreme Pattern: Hello TargetSourceLifecycleListener</title>
    <link>http://blog.extremepattern.com/articles/2006/12/11/hello-targetsourcelifecyclelistener</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>enjoy</description>
    <item>
      <title>Hello TargetSourceLifecycleListener</title>
      <description>&lt;h3&gt;源起&lt;/h3&gt;


	&lt;p&gt;啟動 jetty bundle 後，如果想要修改 jetty 由 spring 控制的 init() 啟動 port 為 8080，
這時在 osgi console 下 update 指令，其他 bundle 註冊的 webapp 是否會改由新的
 8080 進入，需要哪些設定，想要觀察一下。&lt;/p&gt;


	&lt;h3&gt;jetty 發佈服務設定&lt;/h3&gt;


	&lt;p&gt;服務端的設定如下 :&lt;/p&gt;


&lt;pre&gt;
&amp;lt;bean id="httpServiceImpl" 
  class="haha.service.jetty.HttpServiceImpl" 
  init-method="init" 
  destroy-method="destroy" 
  p:port="80" 
  p:debugEnable="true"/&amp;gt;
&amp;lt;osgi:service ref="httpServiceImpl" 
  interface="haha.service.publishing.WebPublishingService"/&amp;gt;
&lt;/pre&gt;

	&lt;h3&gt;使用發布服務並將資料交給發布者&lt;/h3&gt;


	&lt;p&gt;之前的做法如下 :&lt;/p&gt;


&lt;pre&gt;
&amp;lt;osgi:reference id="webPublishingService" 
  interface="haha.service.publishing.WebPublishingService" /&amp;gt;
&amp;lt;bean id="publisher" class="haha.service.jetty.PublisherImpl" 
  init-method="init" destroy-method="destroy" p:webappDir="webapp" 
  p:contextPath="/foo" autowire="byType" /&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;結果 update jetty 後，該發佈服務並沒有跟著改為新的 port，
因為之前的做法綁定在 init()，並無法反映服務的中斷與重新啟用，
，所以需要類似 OSGi DS 規範的服務綁定功能。&lt;/p&gt;


	&lt;h3&gt;TargetSourceLifecycleListener&lt;/h3&gt;


	&lt;p&gt;之前的做法是利用 setter 將服務注入，並在 init() 發布，現在改用 spring-osgi
提供的 TargetSourceLifecycleListener 介面加以實做，在 bind/unbind 中寫下使用
的服務內容，用 osgi:listener 來注入這個服務。&lt;/p&gt;


&lt;pre&gt;
&amp;lt;bean id="publisher" class="haha.service.jetty.PublisherImpl" 
  p:webappDir="webapp" p:contextPath="/foo" /&amp;gt;
&amp;lt;osgi:reference id="webPublishingService" 
  interface="haha.service.publishing.WebPublishingService"&amp;gt;
  &amp;lt;osgi:listener ref="publisher" /&amp;gt;
&amp;lt;/osgi:reference&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;因為由 bind 注入因素，可以省掉  autowire=&amp;#8221;byType&amp;#8221; 的注入方式。&lt;/p&gt;


	&lt;h3&gt;觀察&lt;/h3&gt;


	&lt;ol&gt;
	&lt;li&gt;osgi:listener 可以設定 bind/unbind 的 method，幫助現有 &lt;span class="caps"&gt;API&lt;/span&gt; 轉用，不一定要用 TargetSourceLifecycleListener。&lt;/li&gt;
		&lt;li&gt;這裡用內含新 p:port 的 bundle 來並下載更新來達到目的，如果要由客戶端
改自己改要如何做 ?&lt;/li&gt;
	&lt;/ol&gt;</description>
      <pubDate>Mon, 11 Dec 2006 02:00:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:59fecc86-ecc5-43e7-898b-d567a5262f96</guid>
      <author>LIN</author>
      <link>http://blog.extremepattern.com/articles/2006/12/11/hello-targetsourcelifecyclelistener</link>
      <category>java</category>
    </item>
  </channel>
</rss>

