helloclient.java

来自「最新的关于精通rmi这本书的源码」· Java 代码 · 共 79 行

JAVA
79
字号
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.helloejb.client;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

import masteringrmi.helloejb.interfaces.HelloHome;
import masteringrmi.helloejb.interfaces.HelloWorld;

/**
 *   This is an application that will talk to an EJB component.
 *
 *   It will use JNDI to find the component, then the home will be used instantiate it, and finally call it.
 *      
 *   @see HelloWorld
 *   @author Rickard 謆erg (rickard@dreambean.com)
 */
public class HelloClient
{
   // Static  -------------------------------------------------------
	public static void main(String[] args)
      throws Exception
   {
      new HelloClient();
   }
	
   // Constructors --------------------------------------------------
   public HelloClient()
      throws Exception
   {
      // Find home for HelloEJB
      HelloHome home;
      try
      {
         Context ctx = new InitialContext();
         home = (HelloHome)javax.rmi.PortableRemoteObject.narrow(ctx.lookup("HelloEJB"), HelloHome.class);
         ctx.close();
      } catch (NamingException e)
      {
         System.out.println("Could not lookup HelloEJB home");
         throw e;
      }
      
      // Create HelloEJB
      HelloWorld helloEJB;
      try
      {
         helloEJB = home.create();
      } catch (CreateException e)
      {
         System.out.println("Could not create HelloEJB");
         throw e;
      }
      
      // Get greeting
      String greeting;
      try
      {
         greeting = helloEJB.helloWorld("World");
      } catch (RemoteException e)
      {
         System.out.println("Could not get greeting from HelloEJB");
         throw e;
      } finally
      {
         helloEJB.remove();
      }
      
      // Print greeting
      System.out.println(greeting);
   }
}

⌨️ 快捷键说明

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