?? helloclient.java
字號:
/*
* Copyright 1999 by dreamBean Software,
* All rights reserved.
*/
package masteringrmi.helloactivate.client;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import masteringrmi.helloactivate.interfaces.HelloWorld;
/**
* This is an application that will talk to the remote object.
* After lookup of remote object, it will query it for a
* greeting, which will be shown in the GUI.
*
* Since the server is activatable the client will be able
* to call the server even though it might have failed at some
* earlier point. The client does not have to get a new stub
* to the server.
*
* @see HelloWorld
* @author Rickard 謆erg (rickard@dreambean.com)
*/
public class HelloClient
extends Frame
{
// Attributes ----------------------------------------------------
/**
* The response will shown in this label
*/
Label response = new Label();
/**
* Press this button to say hello again
*/
Button helloButton = new Button("Say hello again!");
/**
* Keep track of how many times we have called the server
*/
int iteration = 1; // Always call one time
/**
* A reference to the server
*/
HelloWorld server;
// Static -------------------------------------------------------
public static void main(String[] args)
{
new HelloClient();
}
// Constructors --------------------------------------------------
/**
* Initialize GUI
*
*/
public HelloClient()
{
System.setProperty("java.security.policy", getClass().getResource("/client.policy").toString());
System.setSecurityManager(new SecurityManager());
// Setup GUI
add("Center",response);
add("South",helloButton);
helloButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
// Call server again and show response
sayHello();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
pack();
show();
try
{
// Locate remote object
server = (HelloWorld)new InitialContext().lookup(HelloWorld.NAME);
// Call server and show response
sayHello();
} catch (NamingException e)
{
response.setText("The server could not be found");
e.printStackTrace(System.err);
} catch (Exception e)
{
response.setText(e.getMessage());
e.printStackTrace(System.err);
}
}
// Public --------------------------------------------------------
public void sayHello()
{
try
{
response.setText(server.helloWorld("World ("+iteration+")"));
iteration++;
} catch (Exception e)
{
response.setText("The object could not be called");
e.printStackTrace(System.err);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -