【问题标题】:My server file in Java RMI stops after 2-4 sec from running it on NetBeans with Win8我在 Java RMI 中的服务器文件在使用 Win8 在 NetBeans 上运行 2-4 秒后停止
【发布时间】:2015-06-26 07:34:50
【问题描述】:

我正在尝试使用 Java RMI 构建分布式应用程序,并且我在这些领域做了很多实践和研究。这么多的例子工作得很好。 这里的代码是用于服务器端的,当我通过 cmd 运行应用程序时它没有任何问题,但是它会在 2-4 秒后关闭,然后才能像这样绑定服务,因此客户端给出“NOTBOUND Exception” .疯狂的部分是相同的代码在 anth 应用程序中运行非常顺利。

这是我正在使用的所有代码

RInterface.java

package rmi;
import java.rmi.*;
public interface RInterface extends Remote {
public boolean log(String Uname, String code) throws RemoteException;
}

RClass.java

package rmi;

import java.rmi.*;
import java.rmi.server.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class RClass extends UnicastRemoteObject implements RInterface {

private Connection connect = null;
private Statement statement = null;
private ResultSet resultSet = null;
private boolean stat = false;
public String state = "Waiting Confirmation ...";

public RClass() throws RemoteException {
    super();
    connect();
}


public boolean log(String Uname, String code) throws RemoteException {
    try {
            String sql = "select name,code from logusers where name='"+Uname+"' and code ='"+code+"'";
            resultSet = statement.executeQuery(sql);

            int count=0;

            while (resultSet.next()) {
                count+=1;
            }

            if (count==0) {
                 stat = false;
                 state = "NO User Found!! Access Denied";
            }else
                if (count>1) {
                    stat = false;
                    state = "duplicate User!! NOT allowed";
                }else
                    if (count == 1){
                        stat = true;
                        state = "WelCome "+ Uname +"!! Access Granted";
                    }
    } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,e);
        }
    return stat;
}

public final void connect(){
    try{ 
     Class.forName("com.mysql.jdbc.Driver");
     connect = DriverManager.getConnection("jdbc:mysql://localhost/eysa?"+"user=root&password=");
     statement = connect.createStatement();

    } catch(ClassNotFoundException | SQLException e){
        System.exit(0);
    }
}
}

myServer.java(我的痛点)

package rmi;

import java.net.MalformedURLException;
import java.rmi.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class myServer {
myServer() {
    new Thread(){
        public void run(){
            try{
                RInterface stub = new RClass();  
                Naming.rebind("rmi://localhost/LOGIN",stub);
            } catch(RemoteException e){
                JOptionPane.showMessageDialog(null, e);
            } catch (MalformedURLException ex) {
                Logger.getLogger(myServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }.start();
}  

public static void main(String[] args) throws InterruptedException {
    myServer myS = new myServer();
    JOptionPane.showMessageDialog(null, "Server is Ready...");
}
}

myClient.java 是很简单的代码,我觉得没必要附上。

【问题讨论】:

    标签: java netbeans server rmi


    【解决方案1】:

    显然,您正在退出 vi 中的 System.exit() 调用,否则为空异常处理程序。

    不要写这样的代码。当您捕获异常时,打印或记录异常,或者更好的是堆栈跟踪。那么调试就不仅仅是猜谜游戏了。

    NB 一个 JDBC 连接和一个语句对于 RMI 服务器来说是不够的。 RMI 是自动多线程的。每个远程方法调用都需要一个 Connection 和 Statement。

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 1970-01-01
      • 2020-02-19
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多