Help with java RMI, how to use the rebind method and lookup with the client class?

Asked

Viewed 545 times

2

I’m doing a job in college where I feel lost. I don’t know how to indicate the server with the method rebind and make lookup with the customer class. I wish you could look at the code I’m writing to see if I’m on the right track and teach you how to use the method rebind, do lookup and indicate me study materials. Thanks in advance.

Statement of the work:

You must describe and build a simple system, using RMI technology, to inform the customer of the current date and time.

Based on the interface, build a client that does the lookup by a service provided by the server, using JNDI. This service must return the date and time properly formatted. Then the customer must print the information on the console.

Interface InterfaceServidorDataEHora:

package javaee_tp1;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface InterfaceServidorDataEHora extends Remote
{
    public String getHoraData() throws RemoteException;
}

Class ServidorDataEHora:

package javaee_tp1;
import java.rmi.Remote.*;
import java.rmi.RemoteException;
import java.rmi.server.*;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

public class ServidorDataEHora extends UnicastRemoteObject implements InterfaceServidorDataEHora
{
    public ServidorDataEHora() throws RemoteException
    {
        System.out.println("ServidorDataHora iniciado... :)");
    }

    @Override
    public String getHoraData() throws RemoteException 
    {
        GregorianCalendar atual = new GregorianCalendar(); 
        SimpleDateFormat formatacao = new SimpleDateFormat("dd' de 'MMMMM' de 'yyyy' - 'HH':'mm'h'");
        return formatacao.format(atual.getTime());
    }
}

Class RegistradorServidor:

package javaee_tp1;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RegistradorServidor 
{
    public static void main(String[] args) 
    {
        System.out.println("Registrando Servidor no RMI.....");
        try 
        {
            Naming.rebind("ServidorDataEHora_0", new ServidorDataEHora());
        } catch (RemoteException | MalformedURLException ex) {
            System.out.println("Ocorreu um erro de registro \n"+ex.toString());
        }
    }
}

I haven’t written the client class yet because I think there’s something wrong with the classes above.

  • Hi. I took the java-ee tag because your question isn’t about Java EE after all. The RMI actually resides in Java SE, and there is nothing else in its issue that is specific to Java EE. It’s true that RMI is used a lot under the table by EJB in Java EE, but here you’re using it directly, so there’s no reason to apply this tag.

  • All right no problem, thanks for editing :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.