📄 studentmdbbean.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package ejb3.day5;import java.text.SimpleDateFormat;import javax.jms.Queue;import java.util.logging.Level;import java.util.logging.Logger;import javax.annotation.Resource;import javax.ejb.ActivationConfigProperty;import javax.ejb.MessageDriven;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.ObjectMessage;import javax.jms.QueueConnection;import javax.jms.QueueConnectionFactory;import javax.jms.QueueSender;import javax.jms.QueueSession;/** * * @author user */@MessageDriven(mappedName = "jms/t", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"), @ActivationConfigProperty(propertyName = "clientId", propertyValue = "StudentMDBBean"), @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "StudentMDBBean") })public class StudentMDBBean implements MessageListener { @Resource(mappedName="jms/qcf") private QueueConnectionFactory qcf; @Resource(mappedName="jms/q") private Queue q; public StudentMDBBean() { } public void onMessage(Message message) { try { ObjectMessage om = (ObjectMessage) message; Student stu = (Student) om.getObject(); sendToQueue(stu); } catch (JMSException ex) { Logger.getLogger(StudentMDBBean.class.getName()).log(Level.SEVERE, null, ex); } } private void sendToQueue(Student stu) { StringBuilder sb = new StringBuilder(); sb.append(stu.getName()).append(",") .append(stu.getSex()).append(","); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sb.append(sdf.format(stu.getBirthday())); QueueConnection con = null; QueueSession ses = null; QueueSender sender = null; try{ con = qcf.createQueueConnection(); ses = con.createQueueSession(true, QueueSession.AUTO_ACKNOWLEDGE); sender = ses.createSender(q); sender.send(ses.createTextMessage(sb.toString())); }catch(JMSException e){ e.printStackTrace(); }finally{ if(sender != null) try{ sender.close(); } catch(JMSException e){} if(ses != null) try{ ses.close(); } catch(JMSException e){} if(con != null) try{ con.close(); } catch(JMSException e){} } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -