Spring Remoting: Remote Method Invocation (RMI)

5 篇文章 0 订阅
2 篇文章 0 订阅

http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp

http://shouce.jb51.net/spring/remoting.html

http://www.javaworld.com/article/2072246/simple-spring-http-remoting-example.html

 Required Libraries

    aopalliance.jar
    commons-logging.jar
    log4j.jar
    org.springframework.aop.jar
    org.springframework.asm.jar
    org.springframework.beans.jar
    org.springframework.context.jar
    org.springframework.context.support.jar
    org.springframework.core.jar
    org.springframework.expression.jar

Server 端:springremotingrmiserver

package com.studytrails.tutorials.springremotingrmiserver;
 
public interface GreetingService {
 
String getGreeting(String name);
}
package com.studytrails.tutorials.springremotingrmiserver;
 
public interface GreetingService {
 
String getGreeting(String name);
}

package com.studytrails.tutorials.springremotingrmiserver;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class StartRmiServer {
 
public static void main(String[] args) {
	ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-server.xml");
	System.out.println("Waiting for Request from Client ...");
	 
}
}

spring-config-server.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
<bean id="greetingService"
class="com.studytrails.tutorials.springremotingrmiserver.GreetingServiceImpl" />
 
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="greetingService" />
<property name="service" ref="greetingService" />
<property name="serviceInterface" value="com.studytrails.tutorials.springremotingrmiserver.GreetingService"/>
<property name="registryPort" value="1099" />
</bean>
 
</beans>


Client 端:springremotingrmiclient

package com.studytrails.tutorials.springremotingrmiserver;
//和server的一样
public interface GreetingService {

	String getGreeting(String name);
}
package com.studytrails.tutorials.springremotingrmiclient;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.studytrails.tutorials.springremotingrmiserver.GreetingService;
 
public class TestSpringRemotingRmi {
 
public static void main(String[] args) {
	ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-client.xml");
	GreetingService greetingService = (GreetingService)context.getBean("greetingService");
	String greetingMessage = greetingService.getGreeting("Alpha");
	System.out.println("The greeting message is : " + greetingMessage);
}
}

spring-config-client.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
<bean id="greetingService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/greetingService"/>
<property name="serviceInterface" value="com.studytrails.tutorials.springremotingrmiserver.GreetingService"/>
</bean>
 
</beans>

先启动Server, 后运行Client, 输出:

The greeting message is : Hello Alpha!


目录结构:








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值