JMS test

本文介绍了一个简单的Java消息服务(JMS)示例,包括如何使用JMS发送和接收消息。通过创建发送者和接收者类,演示了基本的消息传递流程。发送者将消息发送到指定队列,接收者则从同一队列中读取消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
        }


    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值