📄 messagedb.java
字号:
/** * * @author kiriashoo */package messageDrivenBeans;import BusinessSessionBeans.CarSessionBeanRemote;import BusinessSessionBeans.RentingSessionBeanRemote;import BusinessSessionBeans.UserSessionBeanRemote;import Entities.CarEntityBean;import Entities.UserEntityBean;import exception.*;import java.util.Properties;import javax.annotation.Resource;import javax.ejb.ActivationConfigProperty;import javax.ejb.EJB;import javax.ejb.MessageDriven;import javax.ejb.MessageDrivenContext;import javax.jms.JMSException;import javax.jms.MapMessage;import javax.jms.Message;import javax.jms.MessageListener;import javax.mail.*;import javax.mail.internet.*;@MessageDriven(mappedName = "jms/MessageDB", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")})public class MessageDB implements MessageListener { public MessageDB() {} @Resource private MessageDrivenContext mdc; @EJB private RentingSessionBeanRemote rentingBean; public void onMessage(Message message) { MapMessage msg = null; Integer carId = null; Integer userId=null; String startDate=null; String endDate=null; try { if (message instanceof MapMessage) { msg = (MapMessage) message; userId=(Integer) msg.getObject("uid"); carId = (Integer) msg.getObject("carid"); startDate=msg.getString("startDate"); endDate=msg.getString("endDate"); } else { return; } } catch (JMSException e) { e.printStackTrace(); mdc.setRollbackOnly(); } catch (Throwable te) { te.printStackTrace(); } CarSessionBeanRemote apSBean; CarEntityBean carBean; try { javax.naming.Context c = new javax.naming.InitialContext(); apSBean=(CarSessionBeanRemote)c.lookup(CarSessionBeanRemote.class.getName()); } catch(javax.naming.NamingException ne) { java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne); throw new RuntimeException(ne); } carBean=apSBean.find(carId); UserSessionBeanRemote userSBean; UserEntityBean userBean; try { javax.naming.Context c = new javax.naming.InitialContext(); userSBean=(UserSessionBeanRemote)c.lookup(UserSessionBeanRemote.class.getName()); } catch(javax.naming.NamingException ne) { java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne); throw new RuntimeException(ne); } userBean=userSBean.find(userId); RentingSessionBeanRemote bookSBean; try { javax.naming.Context c = new javax.naming.InitialContext(); bookSBean=(RentingSessionBeanRemote)c.lookup(RentingSessionBeanRemote.class.getName()); } catch(javax.naming.NamingException ne) { java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne); throw new RuntimeException(ne); } String smtpServer="smtp.gmail.com"; String from="TravelAgenty@gmail.com"; String subject="Notification: Car reservation"; String bodyS="Mr/Mrs "+userBean.getName()+"\n"; bodyS=bodyS+"You have succesfully reserved a car\n"+"Car information: \n"+"Car Make: "+carBean.getMake()+"\n"; bodyS=bodyS+"\nCar Model: "+carBean.getModel()+"\n Car Year of Fabrication: "+carBean.getYearOfFabr()+"\n"; bodyS=bodyS+"\n BOOKING DETAILS: \n"+"Booking Start Date: "+startDate+"\n Booking End Date: "+endDate; String bodyF="Mr/Mrs "+userBean.getName()+" \n"; bodyF="You have failed to reserve a Car\n"+"Car information: \n"+"Car Make: "+carBean.getMake()+"\n"; bodyF=bodyF+"\nCar Model: "+carBean.getModel()+"\n Car Year of Fabrication: "+carBean.getYearOfFabr()+"\n"; bodyF=bodyF+"\n BOOKING DETAILS: FAILED BETWEEN \n"+" Start Date: "+startDate+"\n End Date: "+endDate; String mail=userBean.getEmail(); try { bookSBean.create(userId,carId,startDate,endDate); send(smtpServer,mail, from, subject, bodyS); } catch(CarNotFoundException e){ send(smtpServer, mail, from, "Notification: Apartment reservation Failed", bodyF); } catch(CarNotFreeException e){ send(smtpServer, mail, from, "Notification: Apartment reservation Failed", bodyF); } } public static void send(String smtpServer, String to, String from , String subject, String body){ try { Properties props =System.getProperties(); //props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(); javax.mail.Session session = javax.mail.Session.getInstance(props,auth); javax.mail.Message msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(new InternetAddress(from)); msg.setRecipients(javax.mail.Message.RecipientType.TO,InternetAddress.parse(to,false)); msg.setSubject(subject); msg.setContent(body,"text/plain"); Transport.send(msg); } catch (Exception ex){ ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -