⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 helloclient.java

📁 分别介绍RMI,分布式对象与CORBA,动态接口,CORBA客户端开发的课件,以及相关的例子
💻 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 + -