📄 newcustomerreceiverbean.java
字号:
package com.learnweblogic.examples.ch10.customer;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class NewCustomerReceiverBean
implements MessageDrivenBean, MessageListener {
private static final int BAD_MESSAGE_TYPE = 1;
private static final int CUSTOMER_ALREADY_EXISTS = 2;
private MessageDrivenContext ctx;
private NewCustomer newCustomer;
public void setMessageDrivenContext(MessageDrivenContext c) {
ctx = c;
try {
Context ic = new InitialContext();
Object h = ic.lookup("java:/comp/env/ejb/NewCustomerHome");
NewCustomerHome home =
(NewCustomerHome) PortableRemoteObject.narrow(
h,
NewCustomerHome.class);
newCustomer = home.create();
} catch (Exception e) {
e.printStackTrace();
throw new EJBException(e);
}
}
public void ejbCreate() {
}
public void ejbRemove() {
}
private void publishError(int errorType, Throwable th) {
// This method handles application errors in the incoming
// messages. One common design published to an error topic here.
// For simplicity, we only print an error message and continue
switch (errorType) {
case BAD_MESSAGE_TYPE :
System.err.println("Bad Message Type received: " + th);
break;
case CUSTOMER_ALREADY_EXISTS :
System.err.println("Customer already exists: " + th);
break;
default :
throw new EJBException("Unexpected error type: " + errorType);
}
}
public void onMessage(Message m) {
MapMessage msg = (MapMessage) m;
try {
int id = msg.getInt("ID");
String firstName = msg.getString("FIRST_NAME");
String lastName = msg.getString("LAST_NAME");
String emailAddress = msg.getString("EMAIL_ADDRESS");
newCustomer.enterNewCustomer(id, firstName, lastName, emailAddress);
} catch (JMSException e) {
// message was mal-formed
publishError(BAD_MESSAGE_TYPE, e);
} catch (InvalidCustomerException ice) {
// customer id already existsed
publishError(CUSTOMER_ALREADY_EXISTS, ice);
} catch (RemoteException re) {
re.printStackTrace();
ctx.setRollbackOnly();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -