osgi入门(二)

xiaoxiao2021-02-28  117

这是osgi servlet的小实验 特别说明:本文主要参考于http://www.cnblogs.com/eastson/archive/2012/05/17/2505897.html 但是在实际操作过程中,有一些不同,另外补充了一些遇到的问题以及解决方法。

1.新建工程

File—new—plug-in project,然后在Target Platfrom里面,注意这里不用修改,直接是默认的equinox。这里是准备创建servlet,不同于上篇博客中的hello world,上篇博客中,这里选择standard。 next—next之后,这里依然选择Hello OSGI Bundle。这里与上篇博客中一样。 新的工程已经创建好了。 添加工程的依赖性 最终MANIFEST.MF内容如下

Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Osgi_demo2 Bundle-SymbolicName: osgi_demo2 Bundle-Version: 1.0.0.qualifier Bundle-Activator: osgi_demo2.Activator Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Import-Package: org.osgi.framework;version="1.3.0" Bundle-ActivationPolicy: lazy Require-Bundle: javax.servlet;bundle-version="3.1.0", org.eclipse.osgi.services;bundle-version="3.5.100", org.apache.felix.gogo.command;bundle-version="0.10.0", org.apache.felix.gogo.runtime;bundle-version="0.10.0", org.apache.felix.gogo.shell;bundle-version="0.10.0", org.eclipse.equinox.console;bundle-version="1.1.200", org.eclipse.equinox.http.registry;bundle-version="1.1.400"

修改Activator.java,代码如下(有没有用到的方法,调试过程中尝试的)

import java.util.Dictionary; import java.util.Hashtable; import javax.servlet.Servlet; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceEvent; import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpService; public class Activator implements BundleActivator, ServiceListener { private BundleContext context; private ServiceReference<HttpService> ref; /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext context) throws Exception { System.out.println("Hello World!!"); this.context=context; //context.addServiceListener(this, "(objectClass="+HttpService.class.getName()+")"); System.out.println("Hello World end!!"); ServiceReference serviceReference = context.getServiceReference(HttpService.class.getName()); HttpService service = (HttpService) context.getService(serviceReference); //ref=context.getServiceReference(HttpService.class); //HttpService service=context.getService(ref); service.registerServlet("/demo/hello", new HelloServlet(context),null,null); System.out.println("/demo/hello已经被注册"); } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { System.out.println("Goodbye World!!"); context.removeServiceListener(this); } @Override public void serviceChanged(ServiceEvent event) { switch(event.getType()){ case ServiceEvent.REGISTERED:registerServlet();break; case ServiceEvent.UNREGISTERING:unregisterServlet();break; } } private void registerServlet(){ if(ref==null){ ref=context.getServiceReference(HttpService.class); if(ref!=null){ try{ HttpService service=context.getService(ref); service.registerServlet("/demo/hello", new HelloServlet(context),null,null); System.out.println("/demo/hello已经被注册"); }catch(Exception ex){ ex.printStackTrace(); } } } } private void unregisterServlet(){ if(ref!=null){ try{ HttpService httpservice=context.getService(ref); httpservice.unregister("/demo/hello"); System.out.println("/demo/hello已经被卸载"); ref=null; }catch(Exception ex){ ex.printStackTrace(); } } } }

HelloServlet.java

import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.osgi.framework.BundleContext; public class HelloServlet extends HttpServlet{ private BundleContext context; public HelloServlet(BundleContext context){ this.context=context; } public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{ HttpSession session=request.getSession(); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>hello world</title>"); out.println("</head>"); out.println("<body>"); out.println("hello wolrd!"); out.println("</body>"); } }

然后运行 在运行配置中,将Target Platfrom先取消,不要勾选全部的,以免会出各种报错。 然后依次勾选这些 javax.servlet org.eclipse.osgi.services org.eclipse.equinox.http.servlet org.eclipse.equinox.http.jetty 最终运行,在浏览器中输入地址http://localhost/demo/hello,是看不到任何结果的,并且在eclipse的输出框中,还有错误提示 Caused by: java.lang.NullPointerException: A null service reference is not allowed.

2.遇到的问题以及解决办法

2.1 Caused by: java.lang.NullPointerException: A null service reference is not allowed.

报这种问题,应该就是在Run Configurations中勾选Bundle不够导致的。 我最开始时候,在这里卡住半天,后来参考http://www.cnblogs.com/sunzhenchao/p/3958275.html,添加了一些bundle,分别是 javax.servlet javax.xml org.apache.felix.gogo.command org.apache.felix.gogo.runtime org.apache.felix.gogo.shell org.eclipse.equinox.console org.eclipse.core.jobs org.eclipse.equinox.common org.eclipse.equinox.concurrent org.eclipse.equinox.event org.eclipse.equinox.http.jetty org.eclipse.equinox.http.registry org.eclipse.equinox.http.servlet org.eclipse.equinox.registry org.eclipse.jetty.continuation org.eclipse.jetty.http org.eclipse.jetty.io org.eclipse.jetty.security org.eclipse.jetty.server org.eclipse.jetty.servlet org.eclipse.jetty.util org.eclipse.osgi org.eclipse.osgi.compatibility.state org.eclipse.osgi.services org.eclipse.wst.jsdt.nashorn.extension

备注:可能实际上需要添加的bundles的比这上面的少,我没有去一一验证了,如果有刚好全的bundles,欢迎留言。 添加之后,点击一下旁边的按钮“validate bundles”,如果有提示缺少什么的,可以点击一下“add required bundles”.最后运行,浏览器中 http://localhost/demo/hello依然是无法访问。但是eclipse的输出框中,也没有任何提示。

2.2 运行无错误,但是浏览器中运行没有任何内容

需要在arguments中添加-Dorg.osgi.service.http.port=9080,然后就可以在浏览器中输入http://localhost:9080/demo/hello,就可以看到输出的内容了 注意:这里的端口号只要不是默认的80端口,其他任意没有被系统占用的端口都可以。从网上搜索得知“后来检查资料发现从ubuntu10.04起,默认是关闭1024一下的端口”。

3.开启与关闭

在eclipse的console框中输入,在osgi> 的提示后输入ss,就可以看到所有bundles的启动状况,找到自己的工程bundle,“26 ACTIVE osgi_demo2_1.0.0.qualifier” ,其id是26; 可以停止servlet,直接在osgi>后面输入stop 26。如果是在调试模式下,没有看到osgi>,可以直接输入回车,就会有的了。停止之后,显示 备用: osgi的java标准文档 https://osgi.org/javadoc/r4v43/core/org/osgi/framework/Constants.html#FRAMEWORK_SYSTEMPACKAGES_EXTRA

参考 [1]OSGi系列 - 开发服务端Web应用之一:Servlet实现 http://www.cnblogs.com/eastson/archive/2012/05/17/2505897.html [2]osgi: HttpService A null service reference is not allowed. http://www.cnblogs.com/sunzhenchao/p/3958275.html

转载请注明原文地址: https://www.6miu.com/read-61730.html

最新回复(0)