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

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

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

    csusky

    常用鏈接

    統(tǒng)計(jì)

    最新評(pí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ù)名稱得到隊(duì)列連接工廠   隊(duì)列       

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



    三、根據(jù)隊(duì)列連接工廠和隊(duì)列創(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 曉宇 閱讀(371) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲综合图片小说区热久久| 国产亚洲成av人片在线观看| 亚洲成年人啊啊aa在线观看| 亚洲精品成a人在线观看| 国产成人A亚洲精V品无码| 亚洲第一AAAAA片| 亚洲国产日韩在线人成下载| 亚洲欧美日韩中文字幕在线一区| 青青视频免费在线| 国产色无码精品视频免费| 国产一卡二卡3卡四卡免费| 国产禁女女网站免费看| 亚洲午夜久久久久久久久久| 亚洲成a人片7777| 国产亚洲欧美在线观看| 99re6在线视频精品免费| 99无码人妻一区二区三区免费 | 国产午夜无码片免费| 久久www免费人成看片| 日本不卡免费新一二三区| 亚洲中文字幕在线第六区| 亚洲综合激情视频| 免费无码AV一区二区| 久99久精品免费视频热77| 成人免费777777| 亚洲无线码一区二区三区| 亚洲国产日韩女人aaaaaa毛片在线 | 亚洲中文字幕久久精品无码2021| 国产成人综合亚洲绿色| 久热免费在线视频| 日韩在线天堂免费观看| 久久精品亚洲视频| 亚洲av成人中文无码专区| 嫩草在线视频www免费观看| 精品免费国产一区二区| 日本亚洲欧洲免费天堂午夜看片女人员| ass亚洲**毛茸茸pics| 国产精品免费久久久久电影网| 99免费在线观看视频| 亚洲第一区在线观看| 亚洲国产成人手机在线电影bd |