OSGi原理与最佳实践——字典服务declarative Service实现
2015-09-15 10:11
399 查看
一、重构DictQueryProject 删除Activator类
二、删除LocalDictQuery里面的Activator类,新建文件夹OSGI-INF,在此文件夹里面创建一个component.xml文件。
修改component.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?> <component name="DictQueryService"> <implementation class="com.grocal.localdictquery.local.impl.LocalDictQueryServiceImpl" /> <service> <provide interface="com.grocal.dictquery.query.QueryService" /> </service> </component>implementation标记:指定接口的实现类 service标记:接口的定义类 同理修改DictWeb里面的DictServlet,删除BoundContext相关的应用,修改后的DictServlet如下:
package com.grocal.dictweb.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpService; import org.osgi.service.http.NamespaceException; import com.grocal.dictquery.query.QueryService; public class DictServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private QueryService queryService; private HttpService httpService; public void setHttpService(HttpService httpService) { this.httpService = httpService; try { httpService.registerServlet("/dict/queryServlet", this, null, null); httpService.registerResources("/dict/page", "page", null); System.out.println("注册service /dict/page/dictquery.html"); } catch (ServletException e) { e.printStackTrace(); } catch (NamespaceException e) { e.printStackTrace(); } } public void unsetHttpService(HttpService httpService) { if (httpService != this.httpService) { return; } this.httpService.unregister("/dict/queryServlet"); this.httpService.unregister("/dict/page"); System.out.println("取消service注册"); this.httpService = null; } public void setQueryService(QueryService queryService) { this.queryService = queryService; } public void unsetQueryService(QueryService queryService) { if (queryService != this.queryService) { return; } this.queryService = null; } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); String queryWord = req.getParameter("query_word"); resp.setContentType("text/html;charset=utf-8"); ServletOutputStream output = resp.getOutputStream(); if (queryService == null) { output.println("No available dictquery service"); output.close(); return; } try { output.println("Result is " + queryService.query(queryWord)); output.close(); return; } catch (Exception e) { output.println("Error occurs"); output.println(e.toString()); output.close(); return; } } }component的内容如下:
<?xml version="1.0" encoding="UTF-8"?> <component name="DictQueryServlet"> <implementation class="com.grocal.dictweb.servlet.DictServlet" /> <reference name="QueryService" interface="com.grocal.dictquery.query.QueryService" bind="setQueryService" unbind="unsetQueryService" policy="dynamic" cardinality="0..1"/> <reference name="HttpService" interface="org.osgi.service.http.HttpService" bind="setHttpService" unbind="unsetHttpService" policy="dynamic"/> </component>运行工程,在这里值得注意的一点是需要加入另外两个bundle,以及设置bundle的start level 为2 阅读更多
相关文章推荐
- 按照《OSGi 原理与最佳实践》进行OSGi部署,发生的问题解决方法
- atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
- atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
- OSGi原理与最佳实践:第一章 OSGi框架简介(2)
- OSGi原理与最佳实践:第一章 OSGi框架简介(1)Equinox
- IPv6转换服务——业务快速支持IPv6最佳实践
- 基于 OSGi 服务模型实现组件之间松耦合通信
- SVN分支/合并原理及最佳实践2
- Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据
- 广播的最佳实践-实现强制下线功能
- WebGIS最佳实践-1 为GeoServer增加GP(Geometry Processing)服务
- (一)Java EE 5实现Web服务(Web Services)及多种客户端实例-原理
- 《深入理解Android:Telephon原理剖析与最佳实践》学习笔记(系统框架)
- activeMQ实践(四)--大型系统的最佳实现之集群
- atitit.导航的实现最佳实践and声明式编程
- Spring Boot 实践3 --基于spring cloud 实现微服务的简单调用
- 大话存储-网络存储系统原理精解与最佳实践
- 【Redis】初探dict字典原理的实现(一)
- 广播最佳实践---实现强制下线
- 淘宝HSF服务的原理以及简单的实现