Book
import java.rmi.*;
import javax.ejb.*;
public interface Book extends EJBObject {
public String getIsbn() throws RemoteException;
public String getAuthor() throws RemoteException;
public void setAuthor(String author) throws RemoteException;
public String getBooktitle() throws RemoteException;
public void setBooktitle(String booktitle) throws RemoteException;
public Double getPrice() throws RemoteException;
public void setPrice(Double price) throws RemoteException;
public Integer getInstock() throws RemoteException;
public void setInstock(Integer instock) throws RemoteException;
}
import java.rmi.*;
import javax.ejb.*;
BookBean
public class BookBean implements EntityBean {
EntityContext entityContext;
public String isbn;
public String author;
public String booktitle;
public Double price;
public Integer instock;
public String ejbCreate(String isbn, String author, String booktitle, Double price, Integer instock) throws CreateException, RemoteException {
this.isbn = isbn;
this.author = author;
this.booktitle = booktitle;
this.price = price;
this.instock = instock;
return null;
}
public String ejbCreate(String isbn) throws RemoteException, CreateException, RemoteException {
return ejbCreate(isbn, null, null, null, null);
}
public void ejbPostCreate(String isbn, String author, String booktitle, Double price, Integer instock) throws CreateException, RemoteException {
}
public void ejbPostCreate(String isbn) throws CreateException, RemoteException {
ejbPostCreate(isbn, null, null, null, null);
}
public void ejbLoad() throws RemoteException {
}
public void ejbStore() throws RemoteException {
}
public void ejbRemove() throws RemoveException, RemoteException {
}
public void ejbActivate() throws RemoteException {
}
public void ejbPassivate() throws RemoteException {
}
public void setEntityContext(EntityContext entityContext) throws RemoteException {
this.entityContext = entityContext;
}
public void unsetEntityContext() throws RemoteException {
entityContext = null;
}
public String getIsbn() {
return isbn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBooktitle() {
return booktitle;
}
public void setBooktitle(String booktitle) {
this.booktitle = booktitle;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getInstock() {
return instock;
}
public void setInstock(Integer instock) {
this.instock = instock;
}
}
BookHome
import java.rmi.*;
import javax.ejb.*;
import java.util.Collection;
public interface BookHome extends EJBHome {
public Book create(String isbn, String author, String booktitle, Double price, Integer instock) throws CreateException, RemoteException;
public Book create(String isbn) throws RemoteException, CreateException;
public Book findByPrimaryKey(String primaryKey) throws RemoteException, FinderException;
public Collection findAll() throws RemoteException, FinderException;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BookSearch
import java.rmi.*;
import javax.ejb.*;
public interface BookSearch extends EJBObject {
public Book findBookByISBN(String book_ISBN) throws RemoteException;
}
BookSearchBean
import javax.rmi.*;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
import javax.naming.*;
public class BookSearchBean implements SessionBean {
private SessionContext sessionContext;
public Context jndiContext;
public BookHome bookHome;
public void ejbCreate() {
}
public void ejbRemove() throws RemoteException {
}
public void ejbActivate() throws RemoteException {
}
public void ejbPassivate() throws RemoteException {
}
public void setSessionContext(SessionContext sessionContext) throws RemoteException {
this.sessionContext = sessionContext;
try {
jndiContext = new InitialContext();
Object object1=jndiContext.lookup("Book");
bookHome=(BookHome)PortableRemoteObject.narrow(object1, BookHome.class);
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex);
}
}
public Book findBookByISBN(String book_ISBN){
try{
String bookISBN=book_ISBN;
Book b = bookHome.findByPrimaryKey(bookISBN);
return b;
}
catch (Exception ex) {
throw new EJBException(ex.getMessage());
}
}
}
import java.rmi.*;
import javax.ejb.*;
public interface BookSearchHome extends EJBHome {
public BookSearch create() throws RemoteException, CreateException;
}
BookSearchClient
import com.borland.samples.micro.mobilebookstore.bookstore.ejb.*;
import javax.naming.*;
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
public class BookSearchClient {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private BookSearchHome bookSearchHome = null;
private BookSearch bookSearch = null;
private Book book = null;
//Construct the EJB test client
public BookSearchClient() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
try {
//get naming context
Context ctx = new InitialContext();
//look up jndi name
Object ref = ctx.lookup("BookSearch");
//cast to Home interface
bookSearchHome = (BookSearchHome) PortableRemoteObject.narrow(ref, BookSearchHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public BookSearch create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
bookSearch = bookSearchHome.create();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: create()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: create()");
}
e.printStackTrace();
}
if (logging) {
log("Return value from create(): " + bookSearch + ".");
}
return bookSearch;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public Book findBookByISBN(String book_ISBN) throws RemoteException{
long startTime = 0;
if (logging) {
log("Calling findBookByISBN(" + book_ISBN + ")");
startTime = System.currentTimeMillis();
}
try {
book = bookSearch.findBookByISBN(book_ISBN);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: findBookByISBN(" + book_ISBN + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
e.printStackTrace();
}
if (logging) {
try {
log("Return value from findBookByISBN(" + book_ISBN + "): " + book + ".");
log("Author: " + book.getAuthor() + ".");
log("Title: " + book.getBooktitle() + ".");
log("Price: " + book.getPrice() + ".");
log("In Stock: " + book.getInstock() + ".");
}
catch (Exception ex) {
}
}
return book;
}
public void updateInStock() {
try {
Integer instock = book.getInstock();
int is = instock.intValue();
int updatedInstock = is -1;
Integer newInstock = new Integer(updatedInstock);
book.setInstock(newInstock);
}
catch(Exception e) {
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
private void log(String message) {
if (message == null) {
System.out.println("-- null");
return ;
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
}
else {
System.out.println("-- " + message);
}
}
//Main method
public static void main(String[] args) {
BookSearchClient client = new BookSearchClient();
}
}
BookSearchHome
BookSearch create()
BookSearch
public Book findBookByISBN(String book_ISBN) throws RemoteException;
BookSearchBean
public Book findBookByISBN(String book_ISBN){
try{
jndiContext = new InitialContext();
Object object1=jndiContext.lookup("Book");
bookHome=(BookHome)PortableRemoteObject.narrow(object1, BookHome.class);
String bookISBN=book_ISBN;
Book b = bookHome.findByPrimaryKey(bookISBN);
return b;
}
catch (Exception ex) {
throw new EJBException(ex.getMessage());
}
}
具体用法:
//look up jndi name
Object ref = ctx.lookup("BookSearch");
//getHome
//cast to Home interface
bookSearchHome = (BookSearchHome) PortableRemoteObject.narrow(ref, BookSearchHome.class);
//get interface
bookSearch = bookSearchHome.create();
//调用inteface的方法—方法的实现在BookSearchBean
book = bookSearch.findBookByISBN(book_ISBN);
posted on 2009-01-07 17:35
波 阅读(421)
评论(0) 编辑 收藏