studentmdbbean.java

来自「想学习EJB的同学」· Java 代码 · 共 81 行

JAVA
81
字号
/* * 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 + =
减小字号Ctrl + -
显示快捷键?