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

📄 helloclient.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.hellojini.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.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import net.jini.core.lookup.ServiceTemplate;
import net.jini.core.lookup.ServiceItem;
import net.jini.lookup.LookupCache;
import net.jini.lookup.ServiceDiscoveryManager;
import net.jini.lookup.ServiceDiscoveryListener;
import net.jini.lookup.ServiceDiscoveryEvent;

import masteringrmi.hellojini.interfaces.HelloWorld;

import com.dreambean.dynaserver.DynaServer;

/**
 *   This is an application that will talk to the Jini service.
 *
 *   When one or more HelloWorld service(s) are available it will
 *   show a button that when clicked will communicate with the service.
 *
 *   If there are no services the button will be greyed out.
 *
 *   This client uses the ServiceDiscoveryManager to keep track of any
 *   available instances of the HelloWorld Jini service.
 *      
 *   @see HelloWorld
 *   @author Rickard 謆erg (rickard@dreambean.com)
 */
public class HelloClient
	extends Frame
{
   // Constants -----------------------------------------------------
   
   // Attributes ----------------------------------------------------
   /**
    *   The service lookup cache managed by the ServiceDiscoveryManager
    */
   LookupCache serviceCache;
   
   /**
    *   This label will show whether the service is available or not
    */
   Label serviceAvailability;
   
   /**
    *   The service usage response will shown in this label
    */
   Label response;
	
   /**
    *   Press this button to say hello to the service
    */
   Button helloButton;
	
   // Static  -------------------------------------------------------
	public static void main(String[] args)
      throws IOException
   {
      new HelloClient();
   }
	
   // Constructors --------------------------------------------------
   public HelloClient()
      throws IOException
   {
      super("HelloJini client");
   
      // Init security
      System.setProperty("java.security.policy", getClass().getResource("/client.policy").toString());
      System.setSecurityManager(new SecurityManager());
      
      // Start client
      startWebserver();
      startGui();
      startJini();
   }

   // Protected  ----------------------------------------------------
   protected void startWebserver()
      throws IOException
   {
      // Create webserver for dynamic class downloading
      DynaServer srv = new DynaServer();
      
      // Add the classloader for this application
      // This will allow any client to download classes/resources that
      // are in the classpath for this application
      srv.addClassLoader(Thread.currentThread().getContextClassLoader());
      
      // Start the webserver
      srv.start();
   }
   
   protected void startGui()
   {
      // Create GUI
      serviceAvailability = new Label("Service is not available");
      response = new Label()
      {
         public java.awt.Dimension getPreferredSize()
         {
            return new java.awt.Dimension(200,20);
         }
      };
      helloButton = new Button("Say hello!");
      helloButton.setEnabled(false);
      
      // Add listeners
      helloButton.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent evt)
         {
      	   // Call server again and show response
      	   sayHello();
         }
      
      });
   
      addWindowListener(new WindowAdapter()
      {
         public void windowClosing(WindowEvent evt)
         {
            serviceCache.terminate();
      	   System.exit(0);
         }
      });
      
      // Layout GUI
      add("North",serviceAvailability);
      add("Center",response);
      add("South",helloButton);
   
      pack();
      show();
   }
   
   protected void startJini()
      throws IOException
   {
      // Create Jini service discovery manager
      ServiceDiscoveryManager sdm = new ServiceDiscoveryManager(null, null);
      
      // Create service template
      Class[] types = new Class[] { HelloWorld.class };
      ServiceTemplate template = new ServiceTemplate(null, types, null);
      
      // Initialize service lookup
      serviceCache = sdm.createLookupCache(template, null, new ServiceDiscoveryListenerHelper());
   }
   
   protected void sayHello()
   {
      ServiceItem si = serviceCache.lookup(null);
      HelloWorld service = (HelloWorld)si.service;
      try
      {
         String greeting = service.helloWorld("World");
         response.setText(greeting);
      } catch (RemoteException e)
      {
         serviceCache.discard(service);
      }
   }
   
   // Inner classes -------------------------------------------------
   class ServiceDiscoveryListenerHelper
      implements ServiceDiscoveryListener 
   {
      public void serviceAdded(ServiceDiscoveryEvent event)
      {
         System.out.println("Service added");
      
         // Service is now available
         helloButton.setEnabled(true);
         serviceAvailability.setText("Service is available");
      }
      
      public void serviceRemoved(ServiceDiscoveryEvent event)
      {
         System.out.println("Service removed");
         
         if (serviceCache.lookup(null) == null)
         {
            // Service is now not available
            helloButton.setEnabled(false);
            serviceAvailability.setText("Service is not available");
         }
      }
      
      public void serviceChanged(ServiceDiscoveryEvent event)
      {
         // Ignore
         System.out.println("Service changed");
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -