使用Eclipse3.2.1+STP.0.4.0+Apache Tuscany开发SCA的Java组件(2)代码篇
四、编辑服务端代码
1、项目建成后,包含一些文件。
在src目录下相应包内有:HelloWorld.java、HelloWorldImpl.java和HelloWorldServer.java三个文件;
在META-INF/sca目录下有default.scdl文件;
在bin目录下有HelloWorld_sca.jar文件。如图所示:
2、修改HelloWorld.java文件
原文件为:
packageorg.eclipse.stp.test;
importorg.osoa.sca.annotations.Remotable;
importorg.osoa.sca.annotations.Service;
@Service()
@Remotable()
publicinterfaceHelloWorld{
}
importorg.osoa.sca.annotations.Remotable;
importorg.osoa.sca.annotations.Service;
@Service()
@Remotable()
publicinterfaceHelloWorld{
}
修改为:
packageorg.eclipse.stp.test;
importorg.osoa.sca.annotations.Remotable;
importorg.osoa.sca.annotations.Service;
@Service()
@Remotable()
publicinterfaceHelloWorld{
publicvoidsayHello(Stringvalue);
}
importorg.osoa.sca.annotations.Remotable;
importorg.osoa.sca.annotations.Service;
@Service()
@Remotable()
publicinterfaceHelloWorld{
publicvoidsayHello(Stringvalue);
}
3、修改HelloWorldImpl.java文件
第一步、增加sayHello()方法
原文件为:
packageorg.eclipse.stp.test;
importorg.osoa.sca.annotations.Service;
importorg.osoa.sca.annotations.OneWay;
@Service(org.eclipse.stp.test.HelloWorld.class)
publicclassHelloWorldImplimplementsHelloWorld{
}
importorg.osoa.sca.annotations.Service;
importorg.osoa.sca.annotations.OneWay;
@Service(org.eclipse.stp.test.HelloWorld.class)
publicclassHelloWorldImplimplementsHelloWorld{
}
修改为:(*注意:并没有完全修改完毕,这里只是手工添加了一个方法)
packageorg.eclipse.stp.test;
importorg.osoa.sca.annotations.Service;
importorg.osoa.sca.annotations.Session;
@Session()
@Service(org.eclipse.stp.test.HelloWorld.class)
publicclassHelloWorldImplimplementsHelloWorld{
publicvoidsayHello(Stringvalue){
System.out.println("Hello"+value);
}
}
importorg.osoa.sca.annotations.Service;
importorg.osoa.sca.annotations.Session;
@Session()
@Service(org.eclipse.stp.test.HelloWorld.class)
publicclassHelloWorldImplimplementsHelloWorld{
publicvoidsayHello(Stringvalue){
System.out.println("Hello"+value);
}
}
第二步、利用工具为sayHello方法添加注释 Annotation
选中HelloWorldImpl.java中的sayHello方法名称,在Annotation view中会显示如图的信息将红色的OneWay设置成true。
图(1)选中方法。(下图)
图(2)设置OneWay值为true。(下图)
图(3)代码同步完成注释添加。(下图)
第三步、利用工具为HelloWorldImpl类添加注释 Annotation
选中HelloWorldImpl名,如图示:
最后,代码修改完毕,如下:
packageorg.eclipse.stp.test;
importorg.osoa.sca.annotations.Service;
importorg.osoa.sca.annotations.OneWay;
importorg.osoa.sca.annotations.Session;
@Session(maxIdleTime="30")
@Service(org.eclipse.stp.test.HelloWorld.class)
publicclassHelloWorldImplimplementsHelloWorld{
@OneWay()
publicvoidsayHello(Stringvalue){
System.out.println("Hello"+value);
}
}
importorg.osoa.sca.annotations.Service;
importorg.osoa.sca.annotations.OneWay;
importorg.osoa.sca.annotations.Session;
@Session(maxIdleTime="30")
@Service(org.eclipse.stp.test.HelloWorld.class)
publicclassHelloWorldImplimplementsHelloWorld{
@OneWay()
publicvoidsayHello(Stringvalue){
System.out.println("Hello"+value);
}
}
五、创建客户端代码
第一步、新建java project ,选中Java Project,Next
输入项目名称为:HelloWorldRMIClient,其他保持默认值,Next.
Java Settings也保持默认值,Finish.
第二步、创建一个新的Interface。文件如下:
packageorg.eclipse.stp.test;
importjava.rmi.Remote;
publicinterfaceHelloWorldextendsRemote{
publicvoidsayHello(Stringvalue);
}
importjava.rmi.Remote;
publicinterfaceHelloWorldextendsRemote{
publicvoidsayHello(Stringvalue);
}
第三步、创建一个新的Class。文件如下:
packageorg.eclipse.stp.test;
importjava.rmi.Naming;
publicclassHelloWorldRMIClient{
publicstaticvoidmain(String[]args){
try{
HelloWorldservice=(HelloWorld)Naming.lookup("rmi://localhost:1099/HelloWorldRemoteService");
service.sayHello("WelcometoSOAWorld!");
}catch(Exceptione){
e.printStackTrace();
}
}
}
importjava.rmi.Naming;
publicclassHelloWorldRMIClient{
publicstaticvoidmain(String[]args){
try{
HelloWorldservice=(HelloWorld)Naming.lookup("rmi://localhost:1099/HelloWorldRemoteService");
service.sayHello("WelcometoSOAWorld!");
}catch(Exceptione){
e.printStackTrace();
}
}
}
rmi信息参照:default.scdl文件中的rmi:binding.rmi内容,default.scdl内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<compositexmlns="http://www.osoa.org/xmlns/sca/1.0"xmlns:rmi="http://incubator.apache.org/tuscany/xmlns/binding/rmi/1.0-incubator-M2"name="HelloWorldComposite">
<componentname="HelloWorldComponent">
<implementation.javaclass="org.eclipse.stp.test.HelloWorldImpl"/>
</component>
<servicename="HelloWorldService">
<interface.javainterface="org.eclipse.stp.test.HelloWorld"/>
<rmi:binding.rmihost="localhost"port="1099"serviceName="HelloWorldRemoteService"/>
<reference>HelloWorldComponent</reference>
</service>
</composite>
<compositexmlns="http://www.osoa.org/xmlns/sca/1.0"xmlns:rmi="http://incubator.apache.org/tuscany/xmlns/binding/rmi/1.0-incubator-M2"name="HelloWorldComposite">
<componentname="HelloWorldComponent">
<implementation.javaclass="org.eclipse.stp.test.HelloWorldImpl"/>
</component>
<servicename="HelloWorldService">
<interface.javainterface="org.eclipse.stp.test.HelloWorld"/>
<rmi:binding.rmihost="localhost"port="1099"serviceName="HelloWorldRemoteService"/>
<reference>HelloWorldComponent</reference>
</service>
</composite>
<待续>