Tuscany与OSGI的结合有多种方式,这里将分一篇文章一个方式进行描述,每一种方式的示例均已Helloworld演示。大部分结构尽量与官网的sample一样。注明:请先下载Tuscany-sca-2.0-M5,并配置好JDK。我采用的是Myeclipse8.5 进行插件开发,没有打上Tuscany的开发插件。
通过在插件服务里面设置Tuscany相关配置,然后通过Tuscany运行发布服务
1 在Myeclipse8.5中新建“标准的OSGI”插件工程。
2 导入Tuscany开发包,右键单击工程,选择Build Path->add External Archives
然后选中Tuscany包中tuscany-sca-2.0-M5/features/tuscany-sca-manifest.jar,
Myeclipse会自动导入所有的包
3 新建包com.test;com.test.impl;
4 在com.test包下新建Helloworld结合,并定义一个方法sayHello(String name);
5 在com.test.impl包下新建HelloworldImpl类,实现Helloworld
6 右键单击工程新建文件夹OSGI-INF,并在下面新建子文件夹sca-config
7 在OSGI-INF文件夹下新建文件hello-component.xml作为服务组件实现,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component name="HelloComponent"
xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">
<implementation class="com.test.impl.HelloWorldImpl" />
<service>
<provide interface="com.test.HelloWorld" />
</service>
</scr:component>
在sca-config文件夹下新建文件hello-config.xml作为sca发布使用的配置文件,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<scact:sca-config targetNamespace="http://hello"
xmlns:scact="http://www.osgi.org/xmlns/scact/v1.0.0" xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1">
<sca:binding.ws name="Hello"
uri="http://192.168.1.102:9000/HelloService">
</sca:binding.ws>
</scact:sca-config>
这里将helloworld作为webservice发布在局域网.
修改META-INF下的MANIFEST.MF文件:添加如下几行代码:
SCA-Configuration: OSGI-INF/sca-config/hello-config.xml
Service-Component-Disabled: OSGI-INF/hello-component.xml
在Import-Package中添加org.oasisopen.sca.annotation;version="2.0.0"
由于我这里没有打上tuscany的插件,所有会显示错误。不用管,直接导出为hello.jar包,注意在选择JAR manifest specifation时需要选择自己的配置。
8 将hello.jar放到tuscany-sca-2.0-M5/modules下,并在tuscany-sca-2.0-M5/features/configuration/config.ini文件osgi.bundles里面添加一行helloworld.jar@:start,/
9 打开命令行,定位到/tuscany-sca-2.0-M5/bin目录,输入以下命令启动:
java -jar ../modules/osgi-3.5.0-v20090520.jar -clean -console -configuration ../features/configuration
10 在浏览器输入http://192.168.1.102:9000/HelloService?wsdl可以看到文档,没看到表示发布没有成功,可以看看自己的环境以及是否在正确的局域网和IP地址。
Tuscany SCA与OSGI的结合方式(一)