Can't create RMI service in SpringBoot application

Viewed 52

I have a Spring boot application, where I try to inject an RMI service.

Currently I have my rmi server running wit the following code:

public static Registry registry;

    public static void main(String[] args) {
        try
        {
            // Create a server registry at default port 1099
            registry = LocateRegistry.createRegistry(1099);

            // Create engine of remote methods, running on the server
            RMIMethodsImpl remoteMethods = new RMIMethodsImpl();

            // Give a name to this engine
            String engineName = "moonlodge";

            // Register the engine by the name, which later will be given to the clients
            Naming.rebind("//localhost/" + engineName, remoteMethods);
        }

I have then tried to instansiate the RMI service inside one of my services, that I have marked with an @Component annotation:

@Component
public class HotelManagerService extends UnicastRemoteObject implements HotelManagerInterface {

    HotelManagerInterface hotelManagerService;
    protected HotelManagerService() throws RemoteException, MalformedURLException, NotBoundException {
    String remoteEngine = "rmi://localhost:1099/moonlodge";
    this.hotelManagerService = (HotelManagerInterface) Naming.lookup(remoteEngine);
    }

I have also tried with the @Autowired annotation, to create it as a dependency injection, but without any luck.

To ensure, that the springboot application "knows" about the service I have put the interface for the service inside the @ComponentScan annotation in my main class, where I start my application:

@SpringBootApplication
@ComponentScan(basePackageClasses = {HotelManagerController.class, HotelManagerInterface.class, HotelManagerService.class})
public class MoonlodgeFrontendApplication {
    
    public static void main(String[] args) {

        SpringApplication.run(MoonlodgeFrontendApplication.class, args).getBean(HotelManagerInterface.class);
    }

I have also ensured, that the interface (for the service) extends the Remote interface like such

...

Here is the exception I get:


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hotelManagerController': Unsatisfied dependency expressed through field 'hotelManagerService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelManagerService' defined in file [/home/kristoffer/Desktop/projects/schoolwork/moonlodge-frontend/target/classes/moonlodge/frontend/moonlodgefrontend/service/HotelManagerService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [moonlodge.frontend.moonlodgefrontend.service.HotelManagerService]: Constructor threw exception; nested exception is java.lang.ClassCastException: class com.sun.proxy.$Proxy52 cannot be cast to class moonlodge.frontend.moonlodgefrontend.contract.interfaces.HotelManagerInterface (com.sun.proxy.$Proxy52 and moonlodge.frontend.moonlodgefrontend.contract.interfaces.HotelManagerInterface are in unnamed module of loader 'app')

What is this $Proxy52, and why can't I get the actual service??

0 Answers
Related