由于项目有组件式开发的需求,网上找了一阵,发现有OSGI和SpringDM都能支持,我选了equinox的实现(新版本注册Servlet已经抛弃HttpService)
网上学习的过程中,发现资料极少,而且大部分发帖时间都在08、09年左右,找来找去只有那几个博客,最近一直在折腾,总算略懂一二。
环境配置:
我的开发工具: eclipse 4.4.2 + JDK 1.8
1 保证OSGI环境能运行:
第一步:下载OSGI环境 http://download.eclipse.org/equinox/drops/R-Neon-201606061100/download.php?dropFile=equinox-SDK-Neon.zip
第二步:解压equinox-SDK-Neon.zip,/equinox-SDK-Neon/plugins下面,找到下面这7个jar(不是必须的,因为一共有200多个,后面很难找):
javax.servlet_3.1.0.v201410161800
org.eclipse.osgi_3.11.0.v20160603-1336
org.eclipse.osgi.services_3.5.100.v20160504-1419
org.apache.felix.gogo.command_0.10.0.v201209301215
org.apache.felix.gogo.runtime_0.10.0.v201209301036
org.apache.felix.gogo.shell_0.10.0.v201212101605
org.eclipse.equinox.console_1.1.200.v20150929-1405
复制到 my-osgi文件夹
第三步:打开eclipse > window > Preferrence > Plug-in-Development > add (Nothing:xxxx) > next > add 选中之前的 my-osgi文件夹
第四步:打开eclipse > Run > Run Configurations > OSGi Framework > 右键new > 选中Bundles下的所有Target Platform(第三步的7个JAR):
点击run,如果控制台出现 osgi > 则说明OSGI环境没问题了(如果不行,请百度,这个不是本文重点)。
2 配置Jetty:
第一步:下载Jetty Bundles(之前我是下的9.4,运行的时候看见控制台打印不是稳定的版本,所以找的9.3)
地址:http://mirrors.neusoft.edu.cn/eclipse/jetty/updates/jetty-bundles-9.x/9.3.10.v20160621/Jetty-bundles-repository-9.3.10.v20160621.zip
第二步:解压,\target\site\plugins下面找到如下10个JAR(同上,不是必须的):
org.eclipse.jetty.deploy_9.3.10.v20160621
org.eclipse.jetty.http_9.3.10.v20160621
org.eclipse.jetty.io_9.3.10.v20160621
org.eclipse.jetty.osgi.boot_9.3.10.v20160621
org.eclipse.jetty.security_9.3.10.v20160621
org.eclipse.jetty.server_9.3.10.v20160621
org.eclipse.jetty.servlet_9.3.10.v20160621
org.eclipse.jetty.util_9.3.10.v20160621
org.eclipse.jetty.webapp_9.3.10.v20160621
org.eclipse.jetty.xml_9.3.10.v20160621
复制到 my-osgi文件夹,刷新Target Platform: 打开eclipse > window > Preferrence > Plug-in-Development > 选中 reload
第三步:设置启动参数,启用Jetty-boot,开启WEB端口:
打开eclipse > Run > Run Configurations > Arguments选项卡 > VM arguments 增加参数:
-Djetty.home=D:your-path\jetty-home -Djetty.http.port=8080
注意 -Djetty.home 是Jetty 配置文件的路径,jetty-home文件夹下必须有个etc目录,etc下是jetty的配置文件(jetty.xml, jetty-deployer.xml, jetty-http.xml等)
第四步:打开eclipse > Run > Run Configurations > OSGi Framework > 全选 > run 测试能否正常运行。
创建工程,测试Servlet:
1 创建plug-in project,编辑MANIFEST.MF, Imported Package > add > javax.servlet;version="3.1.0",
javax.servlet.http;version="3.1.0",
org.eclipse.jetty.http;version="9.3.10",
org.eclipse.jetty.server;version="9.3.10",
org.eclipse.jetty.server.handler;version="9.3.10",
org.eclipse.jetty.servlet;version="9.3.10",
org.osgi.framework;version="1.3.0"
2 创建Servlet
3 注册Servlet
public void start(BundleContext context) throws Exception {
//1. We create a Servlet Handler
ServletHandler handler = new ServletHandler();
//2. We register our Servlet and its URL mapping
handler.addServletWithMapping(MyServlet.class, "/test");
//3. We are creating a Servlet Context handler
ServletContextHandler ch = new ServletContextHandler();
//4. We are defining the context path
ch.setContextPath("/web");
//5. We are attaching our servlet handler
ch.setServletHandler(handler);
//6. We are creating an empty Hashtable as the properties
Hashtable props = new Hashtable();
// 7. Here we register the ServletContextHandler as the OSGi service
context.registerService(ContextHandler.class.getName(), ch, props);
// now you can test the URL: http://ip:port/web/test
}
4 启动,测试 http://ip:port/web/test
5 注册多个servlet, 关键在这句: handler.addServletWithMapping(MyServlet.class, "/test");
其他bundle里面通过bundleContext.getServiceReference(ContextHandler.class.getName()) 拿到ServletContextHandler 然后拿到ServletHandler
最后handler.addServletWithMapping(MyServlet.class, "/test2...3...4...5...");
ServiceReference ref = bundleContext.getServiceReference(ContextHandler.class.getName()) ;
ServletContextHandler sch = (ServletContextHandler) context.getService(ref);
sch.getservletHandler().addServletWithMapping
我的源码下载:
http://download.csdn.net/detail/chen42955/9597079
参考文章:
www.eclipse.org/jetty/documentation/9.4.x/framework-jetty-osgi.html
https://examples.javacodegeeks.com/enterprise-java/jetty/jetty-osgi-example/