client_2.java

来自「This book shows you how to use JBoss to 」· Java 代码 · 共 66 行

JAVA
66
字号
package com.titan.clients;

import com.titan.travelagent.TravelAgentRemote;
import com.titan.travelagent.ExtendedPersistenceContextRemote;
import com.titan.travelagent.TransactionPersistenceContextRemote;
import com.titan.domain.Cabin;

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

import javax.rmi.PortableRemoteObject;

public class Client_2
{
    public static void main(String [] args) 
    {
        try 
	{
            Context jndiContext = getInitialContext();
            Object ref = jndiContext.lookup("TravelAgentBean/remote");
            TravelAgentRemote dao = (TravelAgentRemote)ref;


            ref = jndiContext.lookup("TransactionPersistenceContextBean/remote");
            TransactionPersistenceContextRemote txBean = (TransactionPersistenceContextRemote)ref;

	    Cabin fetchedCabin = dao.findCabin(1);
            int oldBedCount = fetchedCabin.getBedCount();

	    System.out.println("Set up transaction persistence context stateful bean");
	    txBean.setCabin(1);
	    txBean.updateBedCount(5);

	    fetchedCabin = dao.findCabin(1);
	    System.out.println("Cabin bed count will still be " + oldBedCount + ": " + fetchedCabin.getBedCount());

	    System.out.println("Set up extended persistence context stateful bean");

            ref = jndiContext.lookup("ExtendedPersistenceContextBean/remote");
            ExtendedPersistenceContextRemote extendedBean = (ExtendedPersistenceContextRemote)ref;


	    extendedBean.setCabin(1);
	    extendedBean.updateBedCount(5);

	    fetchedCabin = dao.findCabin(1);
	    System.out.println("Cabin bed count will be 5: " + fetchedCabin.getBedCount());

	    // cleanup
	    txBean.remove();
	    extendedBean.remove();
        } 
        catch (javax.naming.NamingException ne)
        {
	    ne.printStackTrace();
	}
    }

    public static Context getInitialContext() 
        throws javax.naming.NamingException 
    {
        return new javax.naming.InitialContext();
    }
}

⌨️ 快捷键说明

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