当前位置: 技术问答>java相关
EJB资源问题,高手请进
来源: 互联网 发布时间:2015-01-10
本文导语: 在一个ejb中使用另一个ejb时,可以使用资源这种东西,也叫reference,据我的理解他的好处就是可以不用预先知道他要使用的jeb的jndi名字,这样可以增强ejb的独立性 比如可以在deploy的时候预先分配资源的名字 怎样在deplo...
在一个ejb中使用另一个ejb时,可以使用资源这种东西,也叫reference,据我的理解他的好处就是可以不用预先知道他要使用的jeb的jndi名字,这样可以增强ejb的独立性
比如可以在deploy的时候预先分配资源的名字
怎样在deploy的时候分配资源的名字我知道,不过怎样在ejb中使用资源那?请高手指点
比如可以在deploy的时候预先分配资源的名字
怎样在deploy的时候分配资源的名字我知道,不过怎样在ejb中使用资源那?请高手指点
|
在一个ejb中使用别的ejb跟在客户端使用区别不大。
travelagentbean使用了四个entity bean和一个无状态session bean,代码如下:
package com.titan.travelagent;
import com.titan.cabin.*;
import com.titan.cruise.*;
import com.titan.customer.*;
import com.titan.processpayment.*;
import com.titan.reservation.*;
import java.sql.*;
import javax.sql.DataSource;
import java.util.Vector;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.ejb.EJBException;
public class TravelAgentBean implements javax.ejb.SessionBean {
public Customer customer;
public Cruise cruise;
public Cabin cabin;
public javax.ejb.SessionContext ejbContext;
public javax.naming.Context jndiContext;
public void ejbCreate(Customer cust){
customer = cust;
}
public int getCustomerID( )
throws IncompleteConversationalState{
try{
if(customer == null)
throw new IncompleteConversationalState();
return ((CustomerPK)customer.getPrimaryKey()).id;
}catch(RemoteException re){
throw new EJBException(re);
}
}
public int getCruiseID( )
throws IncompleteConversationalState{
try{
if(cruise == null)
throw new IncompleteConversationalState();
return ((CruisePK)cruise.getPrimaryKey()).id;
}catch(RemoteException re){
throw new EJBException(re);
}
}
public int getCabinID( )
throws IncompleteConversationalState{
try{
if(cabin==null)
throw new IncompleteConversationalState();
return ((CabinPK)cabin.getPrimaryKey()).id;
}catch(RemoteException re){
throw new EJBException(re);
}
}
public void setCabinID(int cabinID)
throws javax.ejb.FinderException{
try{
CabinHome home = (CabinHome)getHome("CabinBean",CabinHome.class);
CabinPK pk = new CabinPK();
pk.id=cabinID;
cabin = home.findByPrimaryKey(pk);
}catch(RemoteException re){
throw new EJBException(re);
}
}
public void setCruiseID(int cruiseID)
throws javax.ejb.FinderException{
try{
CruiseHome home = (CruiseHome)getHome("CruiseBean", CruiseHome.class);
cruise = home.findByPrimaryKey(new CruisePK(cruiseID));
}catch(RemoteException re){
throw new EJBException(re);
}
}
public Ticket bookPassage(CreditCard card, double price)
throws IncompleteConversationalState{
if(customer == null || cruise == null || cabin == null){
throw new IncompleteConversationalState();
}
try{
ReservationHome resHome =
(ReservationHome) getHome("ReservationBean",ReservationHome.class);
Reservation reservation =
resHome.create(customer, cruise, cabin,price);
ProcessPaymentHome ppHome =
(ProcessPaymentHome) getHome("ProcessPaymentBean",ProcessPaymentHome.class);
ProcessPayment process = ppHome.create();
process.byCredit(customer, card, price);
Ticket ticket = new Ticket(customer,cruise,cabin,price);
return ticket;
}catch(Exception e){
throw new EJBException(e);
}
}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void setSessionContext(javax.ejb.SessionContext cntx){
ejbContext = cntx;
try{
jndiContext = new javax.naming.InitialContext();
}catch(NamingException ne){
throw new EJBException(ne);
}
}
protected Object getHome(String name,Class type){
try{
Object ref = jndiContext.lookup("java:comp/env/ejb/"+name);
return javax.rmi.PortableRemoteObject.narrow(ref, type);
}catch(NamingException ne){
throw new EJBException(ne);
}
}
private Connection getConnection() throws SQLException{
try{
DataSource ds = (DataSource)jndiContext.lookup("java:comp/env/jdbc/mssqlPool");
return ds.getConnection( );
}catch(NamingException ne){throw new EJBException(ne);}
}
public String [] listAvailableCabins(int bedCount)
throws IncompleteConversationalState{
if(cruise == null) throw new IncompleteConversationalState();
Connection con = null;
PreparedStatement ps = null;;
ResultSet result = null;
try {
int cruiseID = ((CruisePK)cruise.getPrimaryKey()).id;
int shipID = cruise.getShipID();
con = getConnection();
ps = con.prepareStatement(
"select ID, NAME, DECK_LEVEL from CABIN "+
"where SHIP_ID = ? and ID NOT IN "+
"(SELECT CABIN_ID FROM RESERVATION WHERE CRUISE_ID = ?)");
ps.setInt(1,shipID);
ps.setInt(2,cruiseID);
result = ps.executeQuery();
Vector vect = new Vector();
while(result.next()){
StringBuffer buf = new StringBuffer();
buf.append(result.getString(1));
buf.append(',');
buf.append(result.getString(2));
buf.append(',');
buf.append(result.getString(3));
vect.addElement(buf.toString());
}
String [] returnArray = new String[vect.size()];
vect.copyInto(returnArray);
return returnArray;
}
catch (Exception e) {
throw new EJBException(e);
}
finally {
try {
if (result != null) result.close();
if (ps != null) ps.close();
if (con!= null) con.close();
}catch(SQLException se){se.printStackTrace();}
}
}
}
travelagentbean使用了四个entity bean和一个无状态session bean,代码如下:
package com.titan.travelagent;
import com.titan.cabin.*;
import com.titan.cruise.*;
import com.titan.customer.*;
import com.titan.processpayment.*;
import com.titan.reservation.*;
import java.sql.*;
import javax.sql.DataSource;
import java.util.Vector;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.ejb.EJBException;
public class TravelAgentBean implements javax.ejb.SessionBean {
public Customer customer;
public Cruise cruise;
public Cabin cabin;
public javax.ejb.SessionContext ejbContext;
public javax.naming.Context jndiContext;
public void ejbCreate(Customer cust){
customer = cust;
}
public int getCustomerID( )
throws IncompleteConversationalState{
try{
if(customer == null)
throw new IncompleteConversationalState();
return ((CustomerPK)customer.getPrimaryKey()).id;
}catch(RemoteException re){
throw new EJBException(re);
}
}
public int getCruiseID( )
throws IncompleteConversationalState{
try{
if(cruise == null)
throw new IncompleteConversationalState();
return ((CruisePK)cruise.getPrimaryKey()).id;
}catch(RemoteException re){
throw new EJBException(re);
}
}
public int getCabinID( )
throws IncompleteConversationalState{
try{
if(cabin==null)
throw new IncompleteConversationalState();
return ((CabinPK)cabin.getPrimaryKey()).id;
}catch(RemoteException re){
throw new EJBException(re);
}
}
public void setCabinID(int cabinID)
throws javax.ejb.FinderException{
try{
CabinHome home = (CabinHome)getHome("CabinBean",CabinHome.class);
CabinPK pk = new CabinPK();
pk.id=cabinID;
cabin = home.findByPrimaryKey(pk);
}catch(RemoteException re){
throw new EJBException(re);
}
}
public void setCruiseID(int cruiseID)
throws javax.ejb.FinderException{
try{
CruiseHome home = (CruiseHome)getHome("CruiseBean", CruiseHome.class);
cruise = home.findByPrimaryKey(new CruisePK(cruiseID));
}catch(RemoteException re){
throw new EJBException(re);
}
}
public Ticket bookPassage(CreditCard card, double price)
throws IncompleteConversationalState{
if(customer == null || cruise == null || cabin == null){
throw new IncompleteConversationalState();
}
try{
ReservationHome resHome =
(ReservationHome) getHome("ReservationBean",ReservationHome.class);
Reservation reservation =
resHome.create(customer, cruise, cabin,price);
ProcessPaymentHome ppHome =
(ProcessPaymentHome) getHome("ProcessPaymentBean",ProcessPaymentHome.class);
ProcessPayment process = ppHome.create();
process.byCredit(customer, card, price);
Ticket ticket = new Ticket(customer,cruise,cabin,price);
return ticket;
}catch(Exception e){
throw new EJBException(e);
}
}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void setSessionContext(javax.ejb.SessionContext cntx){
ejbContext = cntx;
try{
jndiContext = new javax.naming.InitialContext();
}catch(NamingException ne){
throw new EJBException(ne);
}
}
protected Object getHome(String name,Class type){
try{
Object ref = jndiContext.lookup("java:comp/env/ejb/"+name);
return javax.rmi.PortableRemoteObject.narrow(ref, type);
}catch(NamingException ne){
throw new EJBException(ne);
}
}
private Connection getConnection() throws SQLException{
try{
DataSource ds = (DataSource)jndiContext.lookup("java:comp/env/jdbc/mssqlPool");
return ds.getConnection( );
}catch(NamingException ne){throw new EJBException(ne);}
}
public String [] listAvailableCabins(int bedCount)
throws IncompleteConversationalState{
if(cruise == null) throw new IncompleteConversationalState();
Connection con = null;
PreparedStatement ps = null;;
ResultSet result = null;
try {
int cruiseID = ((CruisePK)cruise.getPrimaryKey()).id;
int shipID = cruise.getShipID();
con = getConnection();
ps = con.prepareStatement(
"select ID, NAME, DECK_LEVEL from CABIN "+
"where SHIP_ID = ? and ID NOT IN "+
"(SELECT CABIN_ID FROM RESERVATION WHERE CRUISE_ID = ?)");
ps.setInt(1,shipID);
ps.setInt(2,cruiseID);
result = ps.executeQuery();
Vector vect = new Vector();
while(result.next()){
StringBuffer buf = new StringBuffer();
buf.append(result.getString(1));
buf.append(',');
buf.append(result.getString(2));
buf.append(',');
buf.append(result.getString(3));
vect.addElement(buf.toString());
}
String [] returnArray = new String[vect.size()];
vect.copyInto(returnArray);
return returnArray;
}
catch (Exception e) {
throw new EJBException(e);
}
finally {
try {
if (result != null) result.close();
if (ps != null) ps.close();
if (con!= null) con.close();
}catch(SQLException se){se.printStackTrace();}
}
}
}
|
对,
java:comp/env/是所有环境变量的前缀
后面跟上在部署工具中设置的reference名 ,如ejb/xxxx
合起来就是java:comp/env/ejb/xxxx
java:comp/env/是所有环境变量的前缀
后面跟上在部署工具中设置的reference名 ,如ejb/xxxx
合起来就是java:comp/env/ejb/xxxx
|
to acool
可以在客户端直接使用.
可以在客户端直接使用.