📄 hellobean.java
字号:
package examples;
import javax.ejb.SessionContext;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import java.util.*;
/**
* Demonstration stateless session bean.
*
* @ejb.bean
* name="HelloWorld"
* type="Stateless"
* jndi-name="HelloHome"
*
* @ejb.interface
* remote-class="examples.Hello"
* local-class="examples.HelloLocal"
*
* @ejb.home
* remote-class="examples.HelloHome"
* local-class="examples.HelloLocalHome"
*
*/
public class HelloBean implements javax.ejb.SessionBean
{
//
// EJB-required methods
//
public void ejbCreate()
{
System.out.println("ejbCreate()");
}
public void ejbRemove()
{
System.out.println("ejbRemove()");
}
public void ejbActivate()
{
System.out.println("ejbActivate()");
}
public void ejbPassivate()
{
System.out.println("ejbPassivate()");
}
public void setSessionContext(SessionContext ctx)
{
System.out.println("setSessionContext()");
}
//
// Business methods
//
public String hello()
{
System.out.println("hello()");
AccountLocal account;
try
{
Context ctx = new InitialContext();
Object obj = ctx.lookup("java:comp/env/ejb/AccountHome");
AccountLocalHome home =(AccountLocalHome)javax.rmi.PortableRemoteObject.narrow(obj, AccountLocalHome.class);
System.err.println("Total of all accounts in bank initially = " + home.getTotalBankValue());
/*
* Use the factory to create the Account EJB Object
*/
home.create("123-456-7890", "John Smith");
/*
* Find an account
*/
Iterator i = home.findByOwnerName("John Smith").iterator();
if (i.hasNext())
{
account = (AccountLocal)javax.rmi.PortableRemoteObject.narrow(i.next(), AccountLocal.class);
}
else
{
throw new Exception("Could not find account");
}
/*
* Call the balance() method, and print it
*/
System.out.println("Initial Balance = " + account.getBalance());
/*
* Deposit $100 into the account
*/
account.deposit(100);
/*
* Retrieve the resulting balance.
*/
System.out.println("After depositing 100, account balance = " + account.getBalance());
System.out.println("Total of all accounts in bank now = " + home.getTotalBankValue());
/*
* Retrieve the Primary Key from the EJB Object
*/
AccountPK pk = (AccountPK) account.getPrimaryKey();
/*
* Release our old EJB Object reference. Now call
* find() again, this time querying on Account ID
* (i.e. the Primary Key).
*/
account = null;
account = home.findByPrimaryKey(pk);
/*
* Print out current balance
*/
System.out.println("Found account with ID " + pk +". Balance = " + account.getBalance());
/*
* Try to withdraw $150
*/
System.out.println("Now trying to withdraw $150, which is more than is currently available. This should generate an exception..");
account.withdraw(150);
return "Hello, World!";
}
catch (Exception e)
{
e.printStackTrace();
return "Exception";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -