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

📄 helloagentimpl.java

📁 分别介绍RMI,分布式对象与CORBA,动态接口,CORBA客户端开发的课件,以及相关的例子
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.agent.agents;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.beancontext.BeanContextChild;
import java.beans.beancontext.BeanContextChildSupport;
import java.beans.beancontext.BeanContextProxy;
import java.io.IOException;
import java.net.InetAddress;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.RemoteObject;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *   <description> 
 *      
 *   @see <related>
 *   @author Rickard 謆erg
 *   @version $Revision:$
 */
public class HelloAgentImpl
   extends BeanContextChildSupport
   implements HelloAgent, Runnable
{
   // Constants -----------------------------------------------------
    
   // Attributes ----------------------------------------------------
   String localhost;
   boolean running = true;
   Thread runner;
   
   int runCounter = 0;
   int queryCounter = 0;
   
   // Static --------------------------------------------------------
   
   // Constructors --------------------------------------------------
   
   // Public --------------------------------------------------------
   
   // HelloAgent implementation -------------------------------------
   public synchronized String hello(String name)
   {
      queryCounter++;
      return "Hello " + name + "! I'm an agent running at "+localhost+".";
   }
   
   public java.awt.Component getComponent()
      throws RemoteException
   {
      return new HelloViewer(this);
   }
   
   public String getName()
   {
      return "Hello Agent";
   }
   
   public synchronized int getQueryCounter()
   {
      return queryCounter;
   }
      
   public synchronized int getRunCounter()
   {
      return runCounter;
   }
      
   // Runnable implementation ---------------------------------------
   public void run()
   {
      while (isRunning())
      {      
         // Do some dummy code
         System.out.println("Date is now " + new java.util.Date());
         runCounter++;
         
         // Wait for awhile
         try { Thread.sleep(5000); } catch (InterruptedException e) {}
      }
      
      // The agent was removed
      System.out.println("Ok, I'm done");
   }
      
   // Package protected ---------------------------------------------
    
   // Protected -----------------------------------------------------
   synchronized protected void initializeBeanContextResources()
   {
      // Start the execution thread
      running = true;
      runner = new Thread(this);
      runner.start();
      
      // Get the name of this host
      try
      {
         localhost = InetAddress.getLocalHost().toString();
      } catch (Exception e)
      {
         localhost = "some nice agenthost";
      }
   }
   
   synchronized protected void releaseBeanContextResources()
   {
      // Stop the execution thread
      running = false;
      runner.interrupt();
      runner = null;
   }
   
   synchronized protected boolean isRunning()
   {
      return running;
   }
   
   synchronized void increaseRunCounter()
   {
      runCounter++;
   }
   
   // Private -------------------------------------------------------

   // Inner classes -------------------------------------------------
   static class HelloViewer
      extends JPanel
      implements Runnable, ActionListener
   {   
      JLabel info = new JLabel("", JLabel.CENTER);
      JButton helloButton = new JButton("Say hello");
      
      HelloAgent agent;
      boolean running;
   
      HelloViewer(HelloAgent agent)
      {
         super(new BorderLayout());
      
         this.agent = agent;
      
         add("Center", info);
         add("South", helloButton);
      }
      
      public synchronized void addNotify()
      {
         super.addNotify();
         
         // Start agent query thread
         running = true;
         new Thread(this).start();
         
         // Add action listener to button
         helloButton.addActionListener(this);
      }
      
      public synchronized void removeNotify()
      {
         super.removeNotify();

         // Stop agent query thread
         running = false;
         
         // Remove action listener from button
         helloButton.removeActionListener(this);
      }
      
      public void run()
      {
         while (isRunning())
         {
            // Get info from agent
            try
            {
               info.setText("Hello Agent. Run:"+agent.getRunCounter()+" Query:"+agent.getQueryCounter());
            } catch (RemoteException e)
            {
               e.printStackTrace();
               info.setText("ERROR: Could not access agent");
            }
            
            // Wait some time
            try
            {
               Thread.sleep(5000); // Sleep 5 seconds
            } catch (InterruptedException e)
            {
               // Ignore
            }
         }
      }
      
      public void actionPerformed(ActionEvent evt)
      {
         String name = JOptionPane.showInputDialog(SwingUtilities.getRoot(HelloViewer.this), "What is your name?");
      
         try
         {
            String greeting = HelloViewer.this.agent.hello(name);
            JOptionPane.showMessageDialog(SwingUtilities.getRoot(HelloViewer.this), "HelloAgent says:\""+greeting+"\"", "HelloAgent says", JOptionPane.PLAIN_MESSAGE); 
         } catch (RemoteException e)
         {
            e.printStackTrace();
            JOptionPane.showMessageDialog(SwingUtilities.getRoot(HelloViewer.this), "Could not contact agent:"+e.getMessage(), "HelloAgent error", JOptionPane.ERROR_MESSAGE); 
         }
      }
      
      synchronized protected boolean isRunning()
      {
         return running;
      }
   }
}

⌨️ 快捷键说明

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