public interface RemoteService {
String getMessage();
}
@Service
public class RemoteServiceImpl implements RemoteService {
@Override
public String getMessage() {
return "Hello from remote service!";
}
}
@Configuration
public class RmiServerConfig {
@Bean
public RemoteService remoteService() {
return new RemoteServiceImpl();
}
@Bean
public RmiServiceExporter rmiExporter(RemoteService remoteService) {
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setServiceInterface(RemoteService.class);
exporter.setService(remoteService);
exporter.setServiceName("RemoteService");
exporter.setRegistryPort(1099);
return exporter;
}
}
@Configuration
public class RmiClientConfig {
@Bean
public RemoteService remoteService() {
RmiProxyFactoryBean proxy = new RmiProxyFactoryBean();
proxy.setServiceInterface(RemoteService.class);
proxy.setServiceUrl("rmi://localhost/RemoteService");
proxy.afterPropertiesSet();
return (RemoteService) proxy.getObject();
}
}