一,服务器端
1.真正的业务interface
2.RMI interface
需要特别注意的是RMI interface 与真正的业务interface所定义的方法是一样的。
但是每个方法都应声明抛出异常(java.rmi.RemoteException)。
3.ServiceImpl
业务实现必须同时实现两个接口。
4.spring 配置文件
5.启动服务代码
这里也有地方不甚了解,spring怎么帮我把当前线程阻塞的?下列代码中的日志是有效的,
而记录完成后,进程并不会结束。
二,客户端
1.配置文件
2.使用远程模块
当然了,客户端工程里也要有业务接口的定义才行的。
1.真正的业务interface
package cn.com.legendapl.spring.service;
public interface OperationService {
public int add(int a, int b);
public int sub(int a, int b);
public int mul(int a, int b);
public int div(int a, int b);
}
2.RMI interface
需要特别注意的是RMI interface 与真正的业务interface所定义的方法是一样的。
但是每个方法都应声明抛出异常(java.rmi.RemoteException)。
package cn.com.legendapl.spring.rmi.service;
import java.rmi.RemoteException;
public interface OperationServiceRmiInterface extends java.rmi.Remote{
public int add(int a, int b) throws RemoteException;
public int sub(int a, int b) throws RemoteException;
public int mul(int a, int b) throws RemoteException;
public int div(int a, int b) throws RemoteException;
}
3.ServiceImpl
业务实现必须同时实现两个接口。
package cn.com.legendapl.spring.service;
import cn.com.legendapl.spring.rmi.service.OperationServiceRmiInterface;
public class OperationServiceImpl
implements OperationService, OperationServiceRmiInterface {
public int add(int a, int b) {
return a + b;
}
public int div(int a, int b) {
return a / b;
}
public int mul(int a, int b) {
return a * b;
}
public int sub(int a, int b) {
return a - b;
}
}
4.spring 配置文件
<beans>
<bean id="service" class="cn.com.legendapl.spring.service.OperationServiceImpl" />
<bean id="rmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="service" />
<property name="serviceInterface"
<!-- 这个地方的接口是实际的业务接口,不是那个继承自Remote接口的接口 -->
value="cn.com.legendapl.spring.service.OperationService" />
<property name="serviceName" value="operation" />
<property name="registryPort" value="8899" />
</bean>
<beans>
5.启动服务代码
这里也有地方不甚了解,spring怎么帮我把当前线程阻塞的?下列代码中的日志是有效的,
而记录完成后,进程并不会结束。
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Start {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("applicationContext.xml");
LOG.debug("start...");
}
}
二,客户端
1.配置文件
<bean id="rmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface" value="cn.com.legendapl.spring.service.OperationService" />
<property name="serviceUrl" value="rmi://localhost:8899/operation" />
</bean>
2.使用远程模块
当然了,客户端工程里也要有业务接口的定义才行的。
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.legendapl.spring.service.OperationService;
public class TestCase {
@Test
public void case1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
OperationService os = (OperationService) context.getBean("rmi");
System.out.println(os.mul(12, 3));
}
}
本文详细介绍了一种通过Java RMI实现远程服务调用的方法。包括服务器端接口定义、实现类编写、Spring配置及客户端调用流程。展示了如何在分布式环境中进行简单的算术运算。
563

被折叠的 条评论
为什么被折叠?



