package com.kuaff.ejb3.stateful;
import javax.ejb.EJBException;
import java.rmi.NoSuchObjectException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Client
{
public static void main(String[] args)
{
InitialContext ctx;
try
{
ctx = new InitialContext();
Counter counter = (Counter) ctx.lookup(Counter.class.getName());
counter.add(10);
System.out.println("当前的number:" + counter.getNumber());
counter.add(10);
System.out.println("当前的number:" + counter.getNumber());
Counter counter2 = (Counter) ctx.lookup(Counter.class.getName());
counter2.add(10);
System.out.println("当前的number:" + counter2.getNumber());
//删除
counter2.clean();
//下面如果再使用counter2,将出错
try
{
System.out.println("当前的number:" + counter2.getNumber());
}
catch(EJBException ex)
{
if (ex.getCausedByException() instanceof NoSuchObjectException)
{
System.out.println("我都被删除啦,还找我!");
}
else
{
throw ex;
}
}
}
catch (NamingException e)
{
e.printStackTrace();
}
}
}
|