JMS test

8199 ワード

package depeng.test;

import javax.naming.*;
import javax.jms.*;


/**
 * User: depeng
 * Date: 2010-1-11
 * Time: 11:01:17
 */
public class SimpleQSender {
    public static void main(String[] args) {
        String queueName = null;
        Context jndiContext = null;
        QueueConnectionFactory queueConnectionFactory = null;
        QueueConnection queueConnection = null;
        QueueSession queueSession = null;
        Queue queue = null;
        QueueSender queueSender = null;
        TextMessage message = null;
        int NUM_MSGS = 5;

        if (args.length != 1) {
            System.out.println("");
            System.exit(1);
        }
        queueName = args[0];

        // create JNDI
        try {
            jndiContext =new InitialContext();
        } catch (NamingException e) {
            e.printStackTrace();
            System.exit(1);
        }

        //look up a connection factory and queue
        try {
            //[1]  
            queueConnectionFactory =(QueueConnectionFactory)jndiContext.lookup("QueueConnectionFactory");
            //[2]  
            queue=(Queue)jndiContext.lookup(queueName);
        } catch (NamingException e) {
            e.printStackTrace();  
        }


        try {
            if (queueConnectionFactory != null) {
                queueConnection=queueConnectionFactory.createQueueConnection();
            }
            if (queueConnection != null) {
                queueSession=queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
            }
            if (queueSession != null) {
                queueSender=queueSession.createSender(queue);
            }
            if (queueSession != null) {
                message=queueSession.createTextMessage();
            }


            for (int i = 0; i < NUM_MSGS; i++) {
                if (message != null) {
                    message.setText("This is msg"+(i+1));
                    System.out.println("Sending msg"+message.getText());
                    queueSender.send(message);
                }

            }

            //send a non-text control msg indicating end of message
            queueSender.send(queueSession.createMessage());
            

        } catch (JMSException e) {
            e.printStackTrace();
        }


    }
}

 
package depeng.test;

import javax.naming.*;
import javax.jms.*;

/**
 * User: depeng
 * Date: 2010-1-11
 * Time: 12:24:01
 */
public class SimpleQReceiver {


    public static void main(String[] args) {

        String queueName = null;
        Context jndiContext = null;
        QueueConnectionFactory queueConnectionFactory = null;
        QueueConnection queueConnection = null;
        QueueSession queueSession = null;
        Queue queue = null;
        QueueSender queueSender = null;
        TextMessage message = null;
        int NUM_MSGS = 5;
        
       if (args.length != 1) {
            System.out.println("");
            System.exit(1);
        }
        queueName=args[0];

           // create JNDI
        try {
             jndiContext = new InitialContext();
        } catch (NamingException e) {
            e.printStackTrace();
            System.exit(1);
        }

          //look up a connection factory and queue

        try {
            //[1]

             queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
            //[2]
             queue = (Queue) jndiContext.lookup(queueName);

             if (queueConnectionFactory != null) {
                queueConnection=queueConnectionFactory.createQueueConnection();
            }
            if (queueConnection != null) {
                queueSession=queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
            }
            if (queueSession != null) {
                QueueReceiver queueReceiver = queueSession.createReceiver(queue);
                while(true){
                    Message m=queueReceiver.receive(1);
                    if(m!=null){
                        if(m instanceof TextMessage){
                            message=(TextMessage)m;
                            System.out.println(""+message.getText());
                        }
                    }
                }
            }
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (JMSException e) {
            e.printStackTrace();
        }


    }
}

 
*message-driven beansモデル化非同期情報の受信者
 
package depeng.test;

import javax.naming.*;
import javax.jms.*;

/**
 * User: depeng
 * Date: 2010-1-11
 * Time: 12:35:56
 */
public class SimpleMessageListener implements MessageListener {
    public void onMessage(Message m) {
        TextMessage msg = null;
        if (m != null) {
            if (m instanceof TextMessage) {
                msg = (TextMessage) m;
                try {
                    System.out.println("" + msg.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {
        String queueName = null;
        Context jndiContext = null;
        QueueConnectionFactory queueConnectionFactory = null;
        QueueConnection queueConnection = null;
        QueueSession queueSession = null;
        Queue queue = null;
        QueueSender queueSender = null;
        TextMessage message = null;
        int NUM_MSGS = 5;
        SimpleMessageListener listener = new SimpleMessageListener();

        if (args.length != 1) {
            System.out.println("");
            System.exit(1);
        }
        queueName = args[0];

        // create JNDI
        try {
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            e.printStackTrace();
            System.exit(1);
        }

        //look up a connection factory and queue
        try {
            //[1]
            queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
            //[2]
            queue = (Queue) jndiContext.lookup(queueName);
        } catch (NamingException e) {
            e.printStackTrace();
        }


        try {
            if (queueConnectionFactory != null) {
                queueConnection = queueConnectionFactory.createQueueConnection();
            }
            if (queueConnection != null) {
                queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            }

            QueueReceiver receiver = queueSession.createReceiver(queue);
            receiver.setMessageListener(listener);
            queueConnection.start();


        } catch (JMSException e) {
            e.printStackTrace();
        }


    }

}