<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    csusky

    常用鏈接

    統(tǒng)計

    最新評論

    JMS

    一、初始化         

              String CTX_FACTORY="com.sun.fscontext.RefContextFactory";          
              String INIT_URL 
    = TAppServerInit.getString("bindingURL","file:/d:/IBM/WMQ/java/bin");   //從配置文件中取得
              
              java.util.Hashtable env
    =new java.util.Hashtable();
       
             
    // 初始化JNDI TREE 
             env.put(Context.INITAL_CONTEXT_FACTORY,CTX_FACTORY);    
             env.put(Context.PROVIDE_URL,INIT_URL);
        
             Context ctx
    =new InitiaContext(env);



    二、根據(jù)名稱得到隊列連接工廠   隊列       

            QueueConnectionFactory qcf=(QueueConnectionFactory)ctx.lookup("qcf_name");
           
            Queue queue
    =(Queue)ctx.lookup("q_name");



    三、根據(jù)隊列連接工廠和隊列創(chuàng)建連接    再用連接創(chuàng)建session      

           QueneConnection qc=qcf.createQueneConnection();

           QueueSession   session
    =qc.createQueueSession(false,Session.AUTO_AKNOWLEDGE);



     四、根據(jù)session創(chuàng)建發(fā)送者、接收者、消息    

           Queue sender = ctx.loopup("sender");
           session.createSender(sender);

           Queue receiver 
    = ctx.loopup("receiver");
           session.createReceiver(
    "receiver");
          
          Message message
    =session.createMessage();
          


    五、發(fā)送消息  接收消息       

          sender.send(message,javax.jms.DeliveryMode.PERSISTENT,0,(long)24*60*60*days); 

           Message message
    =receiver.receiverNoWait();


    六、完整的消息收發(fā)程序

    package jmssample; 
    import java.util.Hashtable;
    import javax.jms.*;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    /** This example shows how to establish a connection
    * and send messages to the JMS queue. The classes in this
    * package operate on the same JMS queue. Run the classes together to
    * witness messages being sent and received, and to browse the queue
    * for messages. The class is used to send messages to the queue.
    *
    @author Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved.
    */

    public class QueueSend
    {
    // Defines the JNDI context factory.
    public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

    // Defines the JNDI provider url.
    public final static String PROVIDER_URL=" t3://localhost:7001";

    // Defines the JMS connection factory for the queue.
    public final static String JMS_FACTORY="SendJMSFactory";

    // Defines the queue.
    public final static String QUEUE="SendJMSQueue";


    private QueueConnectionFactory qconFactory;
    private QueueConnection qcon;
    private QueueSession qsession;
    private QueueSender qsender;
    private Queue queue;
    private TextMessage msg;

    /**
    * Creates all the necessary objects for sending
    * messages to a JMS queue.
    *
    @param ctx JNDI initial context
    @param queueName name of queue
    @exception NamingException if operation cannot be performed
    @exception JMSException if JMS fails to initialize due to internal error
    */

    public void init(Context ctx, String queueName)
    throws NamingException, JMSException
    {
    qconFactory 
    = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
    qcon 
    = qconFactory.createQueueConnection();
    qsession 
    = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queue 
    = (Queue) ctx.lookup(queueName);
    qsender 
    = qsession.createSender(queue);
    msg 
    = qsession.createTextMessage();
    qcon.start();
    }


    /**
    * Sends a message to a JMS queue.
    *
    @param message message to be sent
    @exception JMSException if JMS fails to send message due to internal error
    */

    public void send(String message) throws JMSException {
    msg.setText(message);
    qsender.send(msg);
    }


    /**
    * Closes JMS objects.
    @exception JMSException if JMS fails to close objects due to internal error
    */

    public void close() throws JMSException {
    qsender.close();
    qsession.close();
    qcon.close();
    }

    /** main() method.
    *
    @param args WebLogic Server URL
    @exception Exception if operation fails
    */

    public static void main(String[] args) throws Exception {
    InitialContext ic 
    = getInitialContext();
    QueueSend qs 
    = new QueueSend();
    qs.init(ic, QUEUE);
    readAndSend(qs);
    qs.close();
    }


    private static void readAndSend(QueueSend qs)
    throws IOException, JMSException
    {
    BufferedReader msgStream 
    = new BufferedReader(new InputStreamReader(System.in));
    String line
    =null;
    boolean quitNow = false;
    do {
    System.out.print(
    "Enter message (\"quit\" to quit): ");
    line 
    = msgStream.readLine();
    if (line != null && line.trim().length() != 0{
    qs.send(line);
    System.out.println(
    "JMS Message Sent: "+line+"\n");
    quitNow 
    = line.equalsIgnoreCase("quit");
    }

    }
     while (! quitNow);

    }


    private static InitialContext getInitialContext()
    throws NamingException
    {
    Hashtable env 
    = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    env.put(Context.PROVIDER_URL, PROVIDER_URL);
    return new InitialContext(env);
    }


    }



     

      1package jmssample;
      2
      3import java.util.Hashtable;
      4import javax.jms.*;
      5import javax.naming.Context;
      6import javax.naming.InitialContext;
      7import javax.naming.NamingException;
      8
      9/**
     10* This example shows how to establish a connection to
     11* and receive messages from a JMS queue. The classes in this
     12* package operate on the same JMS queue. Run the classes together to
     13* witness messages being sent and received, and to browse the queue
     14* for messages. This class is used to receive and remove messages
     15* from the queue.
     16*
     17@author Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved.
     18*/

     19public class QueueReceive implements MessageListener
     20{
     21// Defines the JNDI context factory.
     22public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
     23
     24// Defines the JNDI provider url.
     25public final static String PROVIDER_URL=" t3://localhost:7001";
     26
     27// Defines the JMS connection factory for the queue.
     28public final static String JMS_FACTORY="SendJMSFactory";
     29
     30// Defines the queue.
     31public final static String QUEUE="SendJMSQueue";
     32
     33private QueueConnectionFactory qconFactory;
     34private QueueConnection qcon;
     35private QueueSession qsession;
     36private QueueReceiver qreceiver;
     37private Queue queue;
     38private boolean quit = false;
     39
     40/**
     41* Message listener interface.
     42@param msg message
     43*/

     44public void onMessage(Message msg)
     45{
     46try {
     47String msgText;
     48if (msg instanceof TextMessage) {
     49msgText = ((TextMessage)msg).getText();
     50}
     else {
     51msgText = msg.toString();
     52}

     53
     54System.out.println("Message Received: "+ msgText );
     55
     56if (msgText.equalsIgnoreCase("quit")) {
     57synchronized(this{
     58quit = true;
     59this.notifyAll(); // Notify main thread to quit
     60}

     61}

     62}
     catch (JMSException jmse) {
     63jmse.printStackTrace();
     64}

     65}

     66
     67/**
     68* Creates all the necessary objects for receiving
     69* messages from a JMS queue.
     70*
     71@param ctx JNDI initial context
     72@param queueName name of queue
     73@exception NamingException if operation cannot be performed
     74@exception JMSException if JMS fails to initialize due to internal error
     75*/

     76public void init(Context ctx, String queueName)
     77throws NamingException, JMSException
     78{
     79qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
     80qcon = qconFactory.createQueueConnection();
     81qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
     82queue = (Queue) ctx.lookup(queueName);
     83qreceiver = qsession.createReceiver(queue);
     84qreceiver.setMessageListener(this);
     85qcon.start();
     86}

     87
     88/**
     89* Closes JMS objects.
     90@exception JMSException if JMS fails to close objects due to internal error
     91*/

     92public void close()throws JMSException
     93{
     94qreceiver.close();
     95qsession.close();
     96qcon.close();
     97}

     98/**
     99* main() method.
    100*
    101@param args WebLogic Server URL
    102@exception Exception if execution fails
    103*/

    104
    105public static void main(String[] args) throws Exception {
    106
    107InitialContext ic = getInitialContext();
    108QueueReceive qr = new QueueReceive();
    109qr.init(ic, QUEUE);
    110
    111System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
    112
    113// Wait until a "quit" message has been received.
    114synchronized(qr) {
    115while (! qr.quit) {
    116try {
    117qr.wait();
    118}
     catch (InterruptedException ie) {}
    119}

    120}

    121qr.close();
    122}

    123
    124private static InitialContext getInitialContext()
    125throws NamingException
    126{
    127Hashtable env = new Hashtable();
    128env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    129env.put(Context.PROVIDER_URL, PROVIDER_URL);
    130return new InitialContext(env);
    131}

    132
    133
    134}

    135



          




    posted on 2008-02-18 11:35 曉宇 閱讀(370) 評論(0)  編輯  收藏 所屬分類: J2EE


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久久亚洲精品国产| 日韩精品福利片午夜免费观着| 国产免费高清69式视频在线观看| 亚洲AV无码精品国产成人| 亚洲最大中文字幕无码网站| 亚洲福利视频网站| 亚洲精品午夜在线观看| 久久亚洲精品成人无码网站 | 中文字幕免费在线播放| 特级毛片爽www免费版| 色屁屁www影院免费观看视频| 日韩成人精品日本亚洲| 黄色网址免费在线| 五月天婷婷精品免费视频| caoporn成人免费公开| 岛国岛国免费V片在线观看 | 亚洲无限乱码一二三四区| 1区1区3区4区产品亚洲| 亚洲成AV人片久久| 亚洲国产日韩精品| 久久精品国产亚洲AV天海翼| 特级aaaaaaaaa毛片免费视频| 人人鲁免费播放视频人人香蕉| 国产大片免费天天看| 秋霞人成在线观看免费视频| 日韩精品人妻系列无码专区免费| 最近中文字幕完整免费视频ww | 中文无码日韩欧免费视频| 野花香在线视频免费观看大全 | 久久毛片免费看一区二区三区| 鲁丝片一区二区三区免费| 最近新韩国日本免费观看| 成人免费的性色视频| 免费看无码自慰一区二区| 亚洲精品成人久久久| 久久精品国产亚洲夜色AV网站| 亚洲精品成人久久| 亚洲精品色在线网站| 免费看无码特级毛片| 日本视频一区在线观看免费| 永久在线毛片免费观看|