Tuscany2.0构建RMI服务

来源:互联网 发布:淘宝试用中心首页 编辑:程序博客网 时间:2024/06/09 22:51
一、需要引入的jar包:
     <dependencies>
       <!-- tuscany rmi -->
       <dependency>
           <groupId> org.apache.tuscany.sca</groupId >
            <artifactId> tuscany-base-runtime </artifactId>
           <version> 2.0.1</ version>
       </dependency>
       <dependency>
           <groupId> org.apache.tuscany.sca</groupId >
           <artifactId> tuscany-binding- rmi-runtime</ artifactId>
           <version> 2.0.1</ version>
            <scope> runtime</ scope>
       </dependency>
       <dependency>
           <groupId> org.apache.tuscany.sca</groupId >
           <artifactId> tuscany-implementation-spring-runtime </artifactId>
           <version> 2.0.1</ version>
       </dependency>
     </dependencies >

二、定义接口和实现:
@Remotable
public interface HelloWorld {
    String sayHello(String name);
}

public class HelloWorldImpl implements HelloWorld {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

三、定义composite配置文件:
helloworld.composite文件
<?xml version= "1.0" encoding ="UTF-8"?>
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
      xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
      targetNamespace="http://hello" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      name= "helloworld-contribution" >
     <component name="HelloWorldComponent">
           <implementation.spring location="helloworld-context.xml" />
           <service name= "HelloworldService">
               <tuscany:binding.rmi uri="rmi://localhost:8085/HelloworldService" />
           </service>
     </component >
</composite>

helloworld-context.xml文件内容
<?xml version= "1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca-j/spring/200810"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd       
     http://www.springframework.org/schema/sca
     http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd" >
     <sca:service name="HelloworldService" target="helloworldService" />
     <bean id="helloworldService" class="com.solidwang.tuscany.HelloWorldImpl" />
</beans>

四、定义服务启动类:
public class ServerStart {
    @Test
    public void testSayHello() throws NoSuchServiceException, IOException, InterruptedException {
        Node node = NodeFactory.newInstance().createNode( "helloworld.composite");
        node.start();
        while (true ) {
            TimeUnit. SECONDS.sleep(Long.MAX_VALUE);
        }
    }
}

五、RMI远程调用测试:
public class RmiTest {
    public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
        HelloWorld h = (HelloWorld) Naming.lookup("//localhost:8085/HelloworldService");
        System. out.println("h=" + h.sayHello("solid......" ));
    }
}

0 0
原创粉丝点击