Hello TargetSourceLifecycleListener
11
Dec
Dec
0
源起
啟動 jetty bundle 後,如果想要修改 jetty 由 spring 控制的 init() 啟動 port 為 8080, 這時在 osgi console 下 update 指令,其他 bundle 註冊的 webapp 是否會改由新的 8080 進入,需要哪些設定,想要觀察一下。
jetty 發佈服務設定
服務端的設定如下 :
<bean id="httpServiceImpl" class="haha.service.jetty.HttpServiceImpl" init-method="init" destroy-method="destroy" p:port="80" p:debugEnable="true"/> <osgi:service ref="httpServiceImpl" interface="haha.service.publishing.WebPublishingService"/>
使用發布服務並將資料交給發布者
之前的做法如下 :
<osgi:reference id="webPublishingService" interface="haha.service.publishing.WebPublishingService" /> <bean id="publisher" class="haha.service.jetty.PublisherImpl" init-method="init" destroy-method="destroy" p:webappDir="webapp" p:contextPath="/foo" autowire="byType" />
結果 update jetty 後,該發佈服務並沒有跟著改為新的 port, 因為之前的做法綁定在 init(),並無法反映服務的中斷與重新啟用, ,所以需要類似 OSGi DS 規範的服務綁定功能。
TargetSourceLifecycleListener
之前的做法是利用 setter 將服務注入,並在 init() 發布,現在改用 spring-osgi 提供的 TargetSourceLifecycleListener 介面加以實做,在 bind/unbind 中寫下使用 的服務內容,用 osgi:listener 來注入這個服務。
<bean id="publisher" class="haha.service.jetty.PublisherImpl" p:webappDir="webapp" p:contextPath="/foo" /> <osgi:reference id="webPublishingService" interface="haha.service.publishing.WebPublishingService"> <osgi:listener ref="publisher" /> </osgi:reference>
因為由 bind 注入因素,可以省掉 autowire=”byType” 的注入方式。
觀察
- osgi:listener 可以設定 bind/unbind 的 method,幫助現有 API 轉用,不一定要用 TargetSourceLifecycleListener。
- 這裡用內含新 p:port 的 bundle 來並下載更新來達到目的,如果要由客戶端 改自己改要如何做 ?