JMS P2P方式的消息发送/接收的一个简单例子

来源:互联网 发布:都匀浪人网络 编辑:程序博客网 时间:2024/06/05 16:43
一、消息发送方程序开发 
  使用JMS的点对点方式发送消息基本上需要以下几个步骤:
  1、从JNDI查找队列连接工厂QueueConnectionFactory以及目标队列对象Queue;
  2、使用连接工厂建立队列连接:QueueConnection对象
  3、使用队列连接QueueConnection对象建立队列会话:QueueSession;
  4、使用会话对象建立消息发送者(或消息接收者)
  5、发送消息
  6、关闭连接等。
 
  程序源码如下:
    public static void main(String[] args) {
        //消息连接工厂
        javax.jms.QueueConnectionFactory queueConnetionFactory = null;
        //消息连接
        javax.jms.QueueConnection queueConnection = null;
        //消息对列
        javax.jms.Queue queue = null;
        //消息发送者
        javax.jms.QueueSender sender = null;
        //消息会话
        javax.jms.QueueSession queueSession = null;
        //消息对象
        javax.jms.TextMessage message = null;
        //上下文对象
        javax.naming.Context ctx = null;
        /*JNDI*/
        String jndiQueueConnectionName = "jms/MyQueueTest";
        String jndiQueueName = "jms/myqueue";
      
        try {
            ctx = new javax.naming.InitialContext();
        } catch (Exception ex) {
            System.out.println("create InitialContext Error:"+ex.toString());
            return;
        }
       
        try {
            //查找消息连接工厂及消息对列
            queueConnetionFactory = (QueueConnectionFactory)ctx.lookup(jndiQueueConnectionName);
            queue = (Queue)ctx.lookup(jndiQueueName);
           
            //建立消息连接
            queueConnection = queueConnetionFactory.createQueueConnection();
            //获得会话
            queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
            //消息发送者
            sender = queueSession.createSender(queue);
           
            //发送消息
            for (int i=0;i<5;i++){
                message = queueSession.createTextMessage();
                message.setText("This is Message:"+i);
                sender.send(message);
                System.out.println("Sender Text Message:"+i);
            }
            sender.send(queueSession.createMessage());
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.toString();
        }finally{
            if ( sender != null )
                try {
                    sender.close();
                } catch (Exception ex) {
                }
            if (queueSession != null)
                try {
                    queueSession.close();
                } catch (Exception ex) {
                }
            if (queueConnection != null)
                try {
                    queueConnection.close();
                } catch (Exception ex) {
                }
            sender = null;
            queueSession = null;
            queueConnection = null;
        }
    }
 
 
二、消息发送程序的发布
  1、进入DeployTool
  2、File--》New--》Appliction,指定应用程序路径及名称
  3、File--》New--》Application Client
    在导航中依次加入类;选择Main Class;
  4、在边的菜单树上选中刚才新建的Application Client,在可边选择Resource Ref's
    在Coded Name中输入jms/MyQueueTest,类型选为:javax.jms.QueueConnectionFactory;JNDI Name中输入:jms/MyQueueTest
  5、选中Msg Dest Ref's
    Coded Name中输入:jms/myqueue;Destination Name中输入:myqueue(Physical Destinations中配置的名称);类型:javax.jms.Queue;Usage:Produces
  6、Message Destinations
    输入Destination Name:myqueue;
    JNDI:jms/myqueue
  7、Tools--》Deploy..
    选中Return Client Jar
  发布完毕。
 
三、消息发送程序的运行
  在Dos下,输入:appclient -client jmsQueueSender.jar
  结果如下:
  Sender Text Message:0
  Sender Text Message:1
  Sender Text Message:2
  Sender Text Message:3
  Sender Text Message:4
 
四、消息接收端的开发
  消息接收与发送基本一样,只是连接建立后,需要建立消息接收对象,然后接收消息就行了。代码如下:
 
    public static void main(String[] args) {
        //消息连接工厂
        javax.jms.QueueConnectionFactory queueConnetionFactory = null;
        //消息连接
        javax.jms.QueueConnection queueConnection = null;
        //消息对列
        javax.jms.Queue queue = null;
        //消息发送者
        javax.jms.QueueReceiver queueReciver = null;
        //消息会话
        javax.jms.QueueSession queueSession = null;
        //消息对象
        javax.jms.Message message = null;
        //上下文对象
        javax.naming.Context ctx = null;
        /*JNDI*/
        String jndiQueueConnectionName = "jms/MyQueueTest";
        String jndiQueueName = "jms/myqueue";
        try {
            ctx = new javax.naming.InitialContext();
        } catch (Exception ex) {
            System.out.println("create InitialContext Error:"+ex.toString());
            return;
        }
        try {
            //查找消息连接工厂及消息对列
            queueConnetionFactory = (QueueConnectionFactory)ctx.lookup(jndiQueueConnectionName);
            queue = (Queue)ctx.lookup(jndiQueueName);
            //建立消息连接
            queueConnection = queueConnetionFactory.createQueueConnection();
            //获得会话
            queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
            //消息接收者
            queueReciver = queueSession.createReceiver(queue);
            //开始接收消息
            queueConnection.start();
            //接受消息
            while (true){
                message = queueReciver.receive();
                if (!(message instanceof javax.jms.TextMessage) || message == null)break;
                System.out.println( "Recieve a Message:"+((javax.jms.TextMessage)message).getText() );
            }
            System.out.println("End Recieve.");
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.toString();
        }finally{
            if ( queueReciver != null )
                try {
                    queueReciver.close();
                } catch (Exception ex) {
                }
            if (queueSession != null)
                try {
                    queueSession.close();
                } catch (Exception ex) {
                }
            if (queueConnection != null)
                try {
                    queueConnection.close();
                } catch (Exception ex) {
                }
            queueReciver = null;
            queueSession = null;
            queueConnection = null;
        }
    }
 
五、消息接收程序的发布及执行
  发布可参考消息接收程序的发布过程;
  在DOS下输入:appclient -client MyJmsRecieverClient.jar,即可看到结果:
  Recieve a Message:This is Message:0
  Recieve a Message:This is Message:1
  Recieve a Message:This is Message:2
  Recieve a Message:This is Message:3
  Recieve a Message:This is Message:4
  End Recieve.
 

0 0