您好,登錄后才能下訂單哦!
1.業(yè)務(wù)接口類及其實(shí)現(xiàn)
/** * 定義一個(gè)遠(yuǎn)程接口 */ public interface HelloService { /** * 需要遠(yuǎn)程調(diào)用的方法 * @param msg * @return */ String sayHello(String msg); }
public class HelloServiceImpl implements HelloService { public String sayHello(String msg) { return "server received the msg : " + msg; } }
2.RmiServiceExporter(服務(wù)端)
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!--RMI的服務(wù)實(shí)現(xiàn)類--> <bean id="helloService" class="com.rmi.spring.HelloServiceImpl" /> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <!--配置RMI的服務(wù)實(shí)現(xiàn)類--> <property name="service" ref="helloService" /> <!--配置RMI的服務(wù)接口--> <property name="serviceInterface" value="com.rmi.spring.HelloService"/> <!--暴露的對(duì)外服務(wù)名--> <property name="serviceName" value="hello" /> <!--服務(wù)本地注冊(cè)端口--> <property name="registryPort" value="9123" /> <!--服務(wù)對(duì)外暴露端口--> <property name="servicePort" value="9123" /> <!--注冊(cè)服務(wù)--> <property name="alwaysCreateRegistry" value="true" /> </bean> </beans>
3.RmiProxyFactoryBean(客戶端)
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://localhost:9123/hello" /> <property name="serviceInterface" value="com.rmi.spring.HelloService"/> </bean> </beans>
4.測(cè)試
public class HelloServer { public static void main(String[] args) { new ClassPathXmlApplicationContext("spring-rmi-server.xml"); System.err.println("rmi 服務(wù)啟動(dòng)!"); } }
public class HelloClient { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-rmi-client.xml"); HelloService helloService = (HelloService)applicationContext.getBean("helloService"); System.err.println(helloService.sayHello("測(cè)試測(cè)試!")); } }
注:Registry服務(wù)的注冊(cè)問題,有時(shí)會(huì)出現(xiàn)服務(wù)啟動(dòng)報(bào)錯(cuò).
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。