main.java

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

JAVA
63
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package queueproducer;import javax.annotation.Resource;import javax.jms.Queue;import javax.jms.QueueConnection;import javax.jms.QueueConnectionFactory;import javax.jms.QueueSender;import javax.jms.QueueSession;import javax.jms.TextMessage;import javax.naming.Context;import javax.naming.InitialContext;/** * * @author user */public class Main {    @Resource(mappedName="jms/qcf")    private static QueueConnectionFactory qcf;    @Resource(mappedName="jms/q")    private static Queue q;    /**     * @param args the command line arguments     */    public static void main(String[] args) {        // TODO code application logic here        QueueConnection con = null;        QueueSession ses = null;        QueueSender sender = null;        try{            Context ctx = new InitialContext();            //step 1. locate QueueConnectionFactory           // QueueConnectionFactory qcf = (QueueConnectionFactory)            //        ctx.lookup("jms/qcf");            //step 2. create QueueConnection from the factory            con = qcf.createQueueConnection();            //step 3. create QueueSession from the connection            ses = con.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);            //step 4. locate Queue            //Queue q = (Queue)ctx.lookup("jms/q");            //step 5. create QueueSender            sender = ses.createSender(q);            //step 6. create Message from the session            TextMessage tmsg = ses.createTextMessage();            tmsg.setText("My first JMS Queue programming.");            //step 7. send message            sender.send(tmsg);        }catch(Exception e){            e.printStackTrace();        }finally{            if(sender != null) try{ sender.close(); }catch(Exception e){}            if(ses != null) try{ ses.close(); }catch(Exception e){}            if(con != null) try{ con.close(); }catch(Exception e){}        }    }}

⌨️ 快捷键说明

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