The following example shows how to invoke a EJB3 session bean from a JSF managed bean.

There are some points I need to mention:

1) The JNDI name of session bean may vary according to different Application Server and the way you deploy your application.
2) Depending your situation,choose the right interface: remote or local.
3) Exception handling is up to your own strategy and choice.

Last but not least,this is not a complete example,there is something missing.

引用
-----------Web Layer--------------------------------------------------

/*
* XXX managed bean
*/
...

public class XXXBean extends MyManagedBean
{
// Session bean JNDI name
private static final String EJB_PRODUCT_SESSION= "ProductSession";

// Session bean remote interface
ProductSessionRemote productSession;

// Session bean local interface
//ProductSessionLocal productSession;

public XXXBean
{
try
{
productSession=(ProductSessionRemote)ServiceLocator.getInstance().getEJB(EJB_PRODUCT_SESSION);
}
catch(Exception) {
...
}

}

public String findProduct(…)
{

try {

product=productSession.findProductById(productId);

}
catch(Exception e)
{
processException(e,productId);
return null; // go current page
}

return “yyyy”;
}

public String createProduct(…)
{

try {

product=productSession.createProduct(product);

}
catch(Exception e)
{
int ret=processException(e);

// process nivagation according to your exception handling strategy
if ( ret== APPLICATION_EXCEPTION )
return “xxxx”; // go error page
else

}

return “yyyy”;
}

}


/*
* MyManagedBean, which is extended by all the managed beans.
*/
...

public class MyManaedBean
{
…….

/*
* Process exception for invocation session bean
*/
protected int processException(Exception e,Object... args)
{
if( e==null )
return -1;

if (e instanceof AppException) // application exception
{
addMessage(FacesMessage.SEVERITY_ERROR,e.getMessage(),args);
return APPLICATION_EXCEPTION;
}
else
if (e instanceof EJBAccessException) // no permission exception
{
addMessage(FacesMessage.SEVERITY_ERROR,CmmwebKeys.NO_PERMISSION_FAILURE);
return NO_PERMISSION_EXCEPTION;
}
else // exception
{
addMessage(FacesMessage.SEVERITY_ERROR,CmmwebKeys.GENERAL_FAILURE);
return OTHER_EXCEPTION;
}
}

// handle message
protected void addMessage(FacesMessage.Severity severity,String bundleMessageName,Object... args)
{
String message = getBundleMessage(bundleMessageName,args);
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(severity,message,message));
}

...
}


/*
* ServiceLocator.java
*
* Created on 19 October 2006, 09:45
* @author xfang
*
*/
...

public class ServiceLocator {

private InitialContext ic;
private Map cache;

private static ServiceLocator me;

static {
try {
me = new ServiceLocator();
} catch(NamingException se) {
throw new RuntimeException(se);
}
}

private ServiceLocator() throws NamingException {
ic = new InitialContext();
cache = Collections.synchronizedMap(new HashMap());
}

public static ServiceLocator getInstance() {
return me;
}

private Object lookup(String jndiName) throws NamingException {
Object cachedObj = cache.get(jndiName);
if (cachedObj == null) {
cachedObj = ic.lookup(jndiName);
cache.put(jndiName, cachedObj);
}
return cachedObj;
}

/**
* will get the ejb3. If this ejb home factory has already been
* clients need to cast to the type of EJBHome they desire
*
* @return the EJB3 corresponding to the homeName
*/
public Object getEJB(String jndiEJBName) throws NamingException {
Object objref = lookup(jndiEJBName);
return objref;
}
}

-------------EJB Layer------------------------------------------------

/*
* Product Session Bean
*/
...

@Stateless
@TransactionManagement(value = TransactionManagementType.CONTAINER)

public class ProductSession implements ProductSessionLocal,ProductSessionRemote
{
@PersistenceContext(unitName="mydb")
private EntityManager em;

/**
* Find a product entity by its ID
*/
public Product findProductById(long id) throws AppException,Exception
{
Product product = null;

try
{
product=(Product)em.find(Product.class,id); // find from database

initProduct(product);// initialize lazy-loaded property
}
catch(Exception e) {
throw e;
}

return product;
}

/**
* create a new product
*/
public Product createProduct(Product p) throws AppException,Exception
{
Product product = p;

try {

if ( !validateXXX(product) ) // validate failed!
{
throw new AppException("AppException.xxx.0001");
}
...

em.persist(product); // save to database

em.refresh(product); // re-initialize entity

initProduct(product); // initialize lazy-loaded property
}
catch (Exception e) {
throw e;
}

return product;
}
}


/*
* Product Session Bean Local interface
*/
...

import javax.ejb.Local;

@Local
public interface ProductSessionLocal
{
Product findProductById(long id) throws AppException,Exception;
Product createProduct(Product product) throws AppException,Exception;
}

/*
* Product Session Bean Remote interface
*/
...

import javax.ejb.Remote;

@Remote
public interface ProductSessionRemote
{
Product findProductById(long id) throws AppException,Exception;
Product createProduct(Product product) throws AppException,Exception;
}
------------------------------------------------------------------------
评论
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

fxy1949
搜索本博客
博客分类
最近加入圈子
最新评论