我在 Java RMI 中的服务器文件在 2-4 秒后从 运行 开始在 NetBeans 和 Win8 上停止
My server file in Java RMI stops after 2-4 sec from running it on NetBeans with Win8
我正在尝试使用 Java RMI 构建分布式应用程序,并且我在这些领域进行了大量实践和研究。这么多例子都很好用。
这里的代码是针对服务器端的,当我通过 cmd 运行 应用程序时它没有任何问题,但它在绑定服务之前 2-4 秒后关闭,因为这个客户给了一个"NOTBOUND Exception"。疯狂的是相同的代码在 anthor 应用程序中运行得非常顺利。
这是我使用的所有代码
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 代码很简单,我觉得没必要附上。
显然,您正在退出 vi System.exit()
调用,否则为空的异常处理程序。
不要这样写代码。当您捕获异常时,打印或记录异常,或者更好的是堆栈跟踪。那么调试就不再是猜谜游戏了
NB 一个 JDBC 连接和一个语句对于 RMI 服务器来说是不够的。 RMI 是自动多线程的。每个远程方法调用都需要一个连接和语句。
我正在尝试使用 Java RMI 构建分布式应用程序,并且我在这些领域进行了大量实践和研究。这么多例子都很好用。 这里的代码是针对服务器端的,当我通过 cmd 运行 应用程序时它没有任何问题,但它在绑定服务之前 2-4 秒后关闭,因为这个客户给了一个"NOTBOUND Exception"。疯狂的是相同的代码在 anthor 应用程序中运行得非常顺利。
这是我使用的所有代码
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 代码很简单,我觉得没必要附上。
显然,您正在退出 vi System.exit()
调用,否则为空的异常处理程序。
不要这样写代码。当您捕获异常时,打印或记录异常,或者更好的是堆栈跟踪。那么调试就不再是猜谜游戏了
NB 一个 JDBC 连接和一个语句对于 RMI 服务器来说是不够的。 RMI 是自动多线程的。每个远程方法调用都需要一个连接和语句。