Duvida RMI

2 respostas
P

E ai pessoal blz!!! estou com uma duvida, implementei um servidor utilizando rmi e conecto pela porta 1099 e obtenho um obj remoto até ai blz. Mas quando tento obter um objeto remoto dele da erro. Alguém poderia me ajudar!!!

2 Respostas

javaBeats

Este objeto implementa java.io.Serializable? Se sim, então dê mais detalhes do seu erro. Se não, faça isso - é obrigatório que objetos remotos sejam serializáveis.

P

E ai blz!!! da uma olhada no codigo!!

Interfaces:

package interfaces;

public interface Servidor extends java.rmi.Remote{
    
    public interfaces.Servico getServico(Object usuario) throws java.rmi.RemoteException;
}
package interfaces;

public interface Servico extends java.rmi.Remote{
    
    public Formulario getAluno() throws java.rmi.RemoteException;

    public Formulario getAula() throws java.rmi.RemoteException;        
}
package interfaces;

public interface Formulario extends java.rmi.Remote{
   
    
    public Object salvar(Object objeto) throws java.rmi.RemoteException;

    public Object alterar(Object objeto) throws java.rmi.RemoteException;

    public Object apagar(Object objeto) throws java.rmi.RemoteException;
    
}

Interfaces implementada:

Servidor:

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry ;
public class Main extends java.rmi.server.UnicastRemoteObject implements interfaces.Servidor{
    
     private brandao.jcptbr.conexao.Conexao conexao;
     private brandao.jcptbr.Sessao sessao;

    /** Creates a new instance of Main */
    public Main() throws java.rmi.RemoteException{
        super(1098);
        
        try{
            this.setLog(new java.io.FileOutputStream("c:/LogRMIMain.txt"));
            brandao.jcptbr.Persistencia.driver           = "org.firebirdsql.jdbc.FBDriver";
            brandao.jcptbr.Persistencia.url              = "jdbc:firebirdsql:localhost/3050:E:/Projetos/AutoEscola/banco/autoescola.gdb";
            brandao.jcptbr.Persistencia.identificacao    = "SYSDBA";
            brandao.jcptbr.Persistencia.senha            = "masterkey";
            brandao.jcptbr.Persistencia.lc_ctype         = null;
            conexao = brandao.jcptbr.Persistencia.getConexao();
            conexao.conectar();
            conexao.getConfiguracao().setPacote("classes");
            sessao = conexao.getSessao();
            brandao.jcptbr.eventos.ListadeEventos lst = conexao.getEventos();
            remoteautoescola.eventos.EnvCadPessoa envpessoa = new remoteautoescola.eventos.EnvCadPessoa(conexao);
            remoteautoescola.eventos.EnvCadAluno  envaluno  = new remoteautoescola.eventos.EnvCadAluno(conexao);
            remoteautoescola.eventos.EnvCadRg      envrg    = new remoteautoescola.eventos.EnvCadRg(conexao);
            lst.novoEvento(new brandao.jcptbr.eventos.Evento("pessoa", brandao.jcptbr.eventos.Env.ACAO_SALVAR, brandao.jcptbr.eventos.Env.ANTES, envpessoa));
            lst.novoEvento(new brandao.jcptbr.eventos.Evento("pessoa", brandao.jcptbr.eventos.Env.ACAO_SALVAR, brandao.jcptbr.eventos.Env.DEPOIS, envpessoa));
            lst.novoEvento(new brandao.jcptbr.eventos.Evento("aluno", brandao.jcptbr.eventos.Env.ACAO_SALVAR, brandao.jcptbr.eventos.Env.ANTES, envaluno));
            lst.novoEvento(new brandao.jcptbr.eventos.Evento("aluno", brandao.jcptbr.eventos.Env.ACAO_SALVAR, brandao.jcptbr.eventos.Env.DEPOIS, envaluno));
            lst.novoEvento(new brandao.jcptbr.eventos.Evento("rg", brandao.jcptbr.eventos.Env.ACAO_SALVAR, brandao.jcptbr.eventos.Env.ANTES, envrg));
            lst.novoEvento(new brandao.jcptbr.eventos.Evento("rg", brandao.jcptbr.eventos.Env.ACAO_SALVAR, brandao.jcptbr.eventos.Env.DEPOIS, envrg));
            
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    
        
    }

    public interfaces.Servico getServico(Object usuario) throws java.rmi.RemoteException{
        ((classes.Usuario)usuario).novoCampopesquisa("login", brandao.jcptbr.extend.Psi.IGUAL);
        ((classes.Usuario)usuario).novoCampopesquisa("senha", brandao.jcptbr.extend.Psi.IGUAL);
        try{
            brandao.jcptbr.Resultado result = sessao.getLeitura().pesquisar(usuario);
            ImpServico serv = null;
            if(result.proxResultado() && ((classes.Usuario)usuario).getLogin().length()!=0 && ((classes.Usuario)usuario).getSenha().length()!=0){
                //if(servico == null)
                //    servico = new ImpServico(conexao);
                
                System.out.println("Usuario conectou: "+((classes.Usuario)usuario).getLogin());
                serv = new ImpServico(conexao);
            }
            result.fechar();
            return serv;
        }
        catch(Exception e){}
        return null;
    }
    
    public static void main(String[] args) {
        // TODO code application logic here

        if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
        
        try{
            Main myM = new Main();
            //interfaces.Servidor stub = (interfaces.Servidor) UnicastRemoteObject.exportObject (myM, 0);           
            //Registry registry = LocateRegistry.createRegistry(1098);
            String serviceName = "rmi://localhost:1099/autoescola";
            //registry.bind (serviceName, stub);
            /*nomeia a instancia do objeto como MensagemServer*/
            //LocateRegistry.createRegistry(27016);
            Naming.rebind(serviceName, myM);
            System.out.println(serviceName+" rodando.");
        }
        catch(Exception e){
            javax.swing.JOptionPane.showMessageDialog(null,e.getMessage());
            e.printStackTrace();
            System.exit(0);
        }

    }
    
    protected void finalize(){
        System.out.println("Main Terminou...");
    }
}
package remoteautoescola;

import interfaces.*;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ImpServico extends java.rmi.server.UnicastRemoteObject implements interfaces.Servico{
    
    private brandao.jcptbr.conexao.Conexao conexao;

    /** Creates a new instance of ImpServico */
    public ImpServico(brandao.jcptbr.conexao.Conexao conexao) throws java.rmi.RemoteException{
        super(1098);
        this.conexao = conexao;
    }
    
    public Formulario getAluno() throws java.rmi.RemoteException{
        return new remoteautoescola.aluno.Aluno(conexao);
    }

    public Formulario getAula() throws java.rmi.RemoteException{
        return new remoteautoescola.aula.Aula(conexao);
    }

}

do cliente vou passar somente o metodo da conexão, pois o codigo é muito grande!!!

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        classes.Usuario usuario = new classes.Usuario();
        usuario.setLogin(jTextField1.getText());
        usuario.setSenha(jPasswordField1.getText());
        try{
            jTextArea1.setText(""); //log
            endereco = jTextField2.getText(); //endereco do servidor
            jTextArea1.append("conectando...");
            autoescola.Main.servidor = (interfaces.Servidor)Naming.lookup(endereco+"/autoescola"); // consigo obter esse objeto
            if(autoescola.Main.servidor != null){
                jTextArea1.append("OK.\n");
                jTextArea1.append(autoescola.Main.servidor+"\n");
                jTextArea1.append("Obtendo serviço...");
                Main.servico = Main.servidor.getServico(usuario); // nao consigo obter esse objeto

                if(Main.servico!=null){
                    jTextArea1.append("OK\n");
                    jTextArea1.append(Main.servico+"\n");
                    this.dispose();
                    (new autoescola.modulos.principal.Tela()).setVisible(true);
                }
                else{
                    jTextArea1.append("ERRO\n");
                }
            }
            
        }
        catch(Exception e){
            javax.swing.JOptionPane.showMessageDialog(null,e.getMessage());
        }
    }

Ocorre o seguinte erro:
Connection refused to host: 10.0.0.2; nested exception is:
java.net.ConnectException: Connection refused: connect

ja desliguei o firewall e nada!!!

Obrigado ate o momento!!!

Criado 28 de setembro de 2006
Ultima resposta 7 de out. de 2006
Respostas 2
Participantes 2