gpt4 book ai didi

Java RMI 服务器端线程

转载 作者:行者123 更新时间:2023-11-30 06:26:04 26 4
gpt4 key购买 nike

我刚刚开始使用 RMI,我正在尝试编写一个简单的程序来模拟火车预订系统。我已经设置了基础 - 服务器、客户端和导出的远程对象。它适用于一个客户端连接。但是,当超过 1 个客户端连接时,客户端似乎在同一个线程中执行。当我在同一台机器上运行多个客户端或从另一台笔记本电脑连接客户端时就是这种情况。

我的印象是 RMI 在服务器端处理线程?如果没有,我如何根据下面的代码处理多个客户端连接?

这里是感兴趣的类。

服务器.....

public class Server {

public Server() {
try {
Booking stub = (Booking) UnicastRemoteObject.exportObject(new BookingProcess(), 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("Booking", stub);
System.err.println("Server Ready");
} catch (RemoteException e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
} catch (AlreadyBoundException e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}

public static void main(String[] args) {
Server server = new Server();
}

}

BookingProcess.....(我省略了 processInput(String input) 使用的私有(private)方法)

public class BookingProcess implements Booking {

private static Journey dublinGalway = new Journey("Dublin to Galway");
private static Journey dublinLimerick = new Journey("Dublin to Limerick");
private static Journey dublinCork = new Journey("Dublin to Cork");
private Journey currentJourney;

private enum State {
INITIAL, JOURNEYS_DISPLAYED, JOURNEY_CHOSEN, ANOTHER_BOOKING_OFFERED, SOLD_OUT;
}

private State currentState = State.INITIAL;

public synchronized String processInput(String input) {
String output = "";

if(currentState == State.INITIAL) {
if(bookedOut()) {
output = "Sorry, there are no seats remaining on any route. Get the bus.";
currentState = State.SOLD_OUT;
}
else {
output = "Please choose a journey to book: " + "1: " + dublinGalway.getDescription() + ", 2: " + dublinLimerick.getDescription() + ", 3: " + dublinCork.getDescription();
currentState = State.JOURNEYS_DISPLAYED;
}
}

else if(currentState == State.JOURNEYS_DISPLAYED) {
output = this.processJourneyChoice(input);
}

else if(currentState == State.JOURNEY_CHOSEN) {
output = "Do you wish to confirm this booking? (y/n)";
if(input.equalsIgnoreCase("y")) {
if(bookingConfirmed()) {
output = "Thank you. Your journey from " + currentJourney.getDescription() + " is confirmed. Hit return to continue.";
//currentState = State.ANOTHER_BOOKING_OFFERED;
}
else {
output = "Sorry, but the last seat on the " + currentJourney.getDescription() + " route has just been booked by another user.";
//currentState = State.ANOTHER_BOOKING_OFFERED;
}
currentState = State.ANOTHER_BOOKING_OFFERED;
}
else if(input.equalsIgnoreCase("n")) {
output = "You have cancelled this booking. Hit return to continue.";
currentState = State.ANOTHER_BOOKING_OFFERED;
}
}

else if(currentState == State.ANOTHER_BOOKING_OFFERED) {
output = "Would you like to make another booking? (y/n)";
if(input.equalsIgnoreCase("y")) {
output = "Hit Return to continue.";
currentState = State.INITIAL;
}
else if(input.equalsIgnoreCase("n")){
output = "Goodbye.";
try {
Thread.currentThread().join(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
currentState = State.INITIAL;
}
}

else if(currentState == State.SOLD_OUT) {
output = "Goodbye.";
}

return output;
}

最后是客户......

public class Client {

public static void main(String[] args) {
Client client = new Client();
client.runClient();
}

public void runClient() {

try {
BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
Registry registry = LocateRegistry.getRegistry("localhost");
Booking stub = (Booking) registry.lookup("Booking");
String serverResponse = stub.processInput("begin");
System.out.println("Server: " + serverResponse);

while((serverResponse = stub.processInput(consoleInput.readLine())) != null) {
System.out.println(serverResponse);
if(serverResponse.equals("Goodbye.")) {
break;
}
}
} catch (Exception e) {
System.err.println("Client exception " + e.toString());
e.printStackTrace();
}
}


}

最佳答案

至于作为 RMI 服务器线程,答案是它可能会也可能不会在单独的线程中运行。请参阅此处的文档:

http://docs.oracle.com/javase/6/docs/platform/rmi/spec/rmi-arch3.html

3.2 Thread Usage in Remote Method Invocations

A method dispatched by the RMI runtime to a remote object implementation may or may not execute in a separate thread. The RMI runtime makes no guarantees with respect to mapping remote object invocations to threads. Since remote method invocation on the same remote object may execute concurrently, a remote object implementation needs to make sure its implementation is thread-safe.

您可以获取服务器端线程转储,您会看到 RMI TCP 连接线程 ID 不断变化,但是@jtahlborn 注意到服务器端方法是同步的,因此它会串行执行,但不一定在单个线程中执行。

关于Java RMI 服务器端线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14839955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com