⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bidreceiverbean.java

📁 J2EE开发与Weblogic一书中的源代码
💻 JAVA
字号:
package webauction.ejb;

import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.mail.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class BidReceiverBean implements MessageDrivenBean, MessageListener {
    private MessageDrivenContext ctx;
	private UserHome userHome;
	private BidHome bidHome;
	private ItemHome itemHome;
	private IDGeneratorHome idGeneratorHome;

    private IDGenerator idGenerator;
    private MailSender mailSender;

	private Object narrow(Object o, Class c) {
		return PortableRemoteObject.narrow(o, c);
	}

    public void setMessageDrivenContext(MessageDrivenContext c) {
        ctx = c;
        try {
            Context ic = new InitialContext();

			userHome =
				(UserHome) narrow(ic.lookup(EjbConstants.LOOKUP_USER_HOME),
					UserHome.class);
			bidHome =
				(BidHome) narrow(ic.lookup(EjbConstants.LOOKUP_BID_HOME),
					BidHome.class);
			itemHome =
				(ItemHome) narrow(ic.lookup(EjbConstants.LOOKUP_ITEM_HOME),
					ItemHome.class);
			idGeneratorHome =
				(IDGeneratorHome) narrow(ic.lookup(EjbConstants.LOOKUP_IDGEN_HOME),
					IDGeneratorHome.class);

			idGenerator = idGeneratorHome.create();

            Session mailSession =
                (Session) ic.lookup(EjbConstants.LOOKUP_MAIL_SESSION);
            mailSession.setDebug(true);
            mailSender = new MailSender(mailSession);
        } catch (NamingException ne) {
            ne.printStackTrace();
            throw new EJBException(ne);
        } catch (Exception e) {
            // Unexpected error while creating idGenerator
            e.printStackTrace();
            throw new EJBException(e);
        }
    }

    public void ejbCreate() {}
    public void ejbRemove() {}
    public void ejbActivate() {}
    public void ejbPassivate() {}

    public void onMessage(Message m) {
        MapMessage bidMsg = (MapMessage) m;
        Item item = null;
        User user = null;

        try {
            int itemId = bidMsg.getInt("Item_ID");
            String userName = bidMsg.getString("User_Name");
            double bidAmount = bidMsg.getDouble("Amount");
            item = itemHome.findByPrimaryKey(new Integer(itemId));
            user = userHome.findByPrimaryKey(userName);
            int id = idGenerator.getNextValue();
            Bid bid = bidHome.create(new Integer(id), item, user, bidAmount);

            String userEmail = user.getEmail();
            String description = item.getDescription();
            // TODO uncomment
            //            mailSender.sendMailMessage(
            //                userEmail,
            //                "wlauction@learnweblogic.com",
            //                "Your bid on item: " + description,
            //                "Your bid in the amount of : "
            //                    + bidAmount
            //                    + " was accepted on item: "
            //                    + description);
        } catch (Exception e) {
            ctx.setRollbackOnly();
            e.printStackTrace();
            return;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -