📄 facadebean.java
字号:
package bookstoreejb;
import javax.jms.*;
import javax.ejb.*;
import java.util.*;
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
public class facadeBean implements SessionBean {
SessionContext sessionContext;
private BookItem bookItem=null;
private BookItemHome bookItemHome=null;
private Orderinfo orderInfo=null;
private OrderinfoHome orderInfoHome=null;
public void ejbCreate() throws CreateException {
try{
Context context = new InitialContext();
Object ref = context.lookup("java:/comp/env/BookItem");
bookItemHome= (BookItemHome) ref;
ref=context.lookup("java:/comp/env/Orderinfo");
orderInfoHome= (OrderinfoHome) ref;
}catch(Exception e){e.printStackTrace();}
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
try{
Context context = new InitialContext();
Object ref = context.lookup("java:/comp/env/BookItem");
bookItemHome= (BookItemHome) ref;
ref=context.lookup("java:/comp/env/Orderinfo");
orderInfoHome= (OrderinfoHome) ref;
}catch(Exception e){e.printStackTrace();}
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public java.util.Collection getAllBook() {
Collection result=new Vector();
try{
Collection c1 = bookItemHome.findAllBook();
System.out.println(c1.size());
Object o1[]=c1.toArray();
for(int i=0;i<o1.length;i++)
{
bookItem=(BookItem)o1[i];
result.add(new Book(bookItem.getBookname(),bookItem.getAuthor(),bookItem.getPrice().floatValue()));
}
}catch(Exception e){e.printStackTrace();}
return result;
}
public Object getBookByName(String bookName) {
try{
bookItem = (BookItem) bookItemHome.findByPrimaryKey(bookName);
}catch(Exception e){e.printStackTrace();}
return new Book(bookItem.getBookname(),bookItem.getAuthor(),bookItem.getPrice().floatValue());
}
public void sendMessage(Order order) {
try{
QueueSender queueSender = null;
Context ctx=getInitialContext();
QueueConnectionFactory qConFactory =
(QueueConnectionFactory) ctx.lookup("weblogic.jms.ConnectionFactory");
Queue messageQueue = (Queue)ctx.lookup("bookstorequeue");
QueueConnection qCon=qConFactory.createQueueConnection();
QueueSession session = qCon.createQueueSession(false,Session.AUTO_ACKNOWLEDGE );
queueSender=session.createSender(messageQueue);
Message objMessage=session.createObjectMessage(order);
queueSender.send(objMessage);
if (queueSender != null) {
queueSender.close();
}
if(qCon!=null) {
qCon.close();
}
}catch(Exception e){e.printStackTrace();}
/**@todo Complete this method*/
}
public java.util.Collection getAllOrder() {
Collection result=new Vector();
try{
Collection c1 = orderInfoHome.findAllOrder();
Object o1[]=c1.toArray();
for(int i=0;i<o1.length;i++)
{
orderInfo=(Orderinfo)o1[i];
result.add(new Order(orderInfo.getOrderid(),orderInfo.getCustname(),orderInfo.getAddress(),orderInfo.getEMail(),orderInfo.getBooklist(),orderInfo.getPrice().floatValue()));
}
}catch(Exception e){e.printStackTrace();}
return result;
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = "system";
String password = "security";
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
return new InitialContext(properties);
}
catch(Exception e) {
throw e;
}
}
public void deleteOrder(String orderid) {
try{
orderInfoHome.remove(orderid);
}catch(Exception e){e.printStackTrace();}
/**@todo Complete this method*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -