zoukankan      html  css  js  c++  java
  • [转]JMS编程模式

    消息形式:
    点对点为两个客户之间建立消息队列,使两个客户端之间通过队列实现点对点的消息传递
    注:类似数据结构里的队列,先进先出
    主题消息,在消息中间件上建立一个主题,没个客户端都可已向这个主题发送消息,接受消息

    注:类似广播的方式

    开发流程:


    使用JMS步骤

    1.创建一个JNDI上下文

    Context  init initCtx =new InitialContext(env);

    2.查找创建JMS连接使用的工厂类(Connnect Factory)

    对于主题消息:

    Object tmp=initCtx.lookup(“Connnect Factory”);

    TopicConnectionFactory tcf=( TopicConnectionFactory) tmp;

    对于点对点消息:

    Object tmp=initCtx.lookup(“Connnect Factory”);

    QueueConnectionFactory tcf=( QueueConnectionFactory) tmp;

    3.查找JMS的目标对象(Destination)

    对于主题消息:

    Topic topic=(Topic)iniCtx.lookup(“topic/testTopic”);

    对于点对点消息:

    Queue queue (Queue)iniCtx.lookup(“queue /A”);

    4.创建JMS连接(Connection)

    对于主题消息:

    TopicConnection conn=tcp.createTopicConnection();

    对于点对点消息:

    QueueConnection conn=tcp.createQueueConnection ();

    5.创建JMS会话(session)

    对于主题消息:

    TopicSession session=conn.createTopicSession(fasle,Session.AUTO_ACKNOWLEDGE);

    对于点对点消息:

    QueueSession session= conn.createQueueSession(true,0);

    6.创建消息的生产和消费者

    生产者:

    对于主题消息:

    TopicPublisher publisher=session.createPublisher(topic);

    对于点对点消息:

    QueueSender sender = session.createSender (queue);

    消费者:

    对于主题消息:

    TopicSubscriber subscriber=session.createSubscriber (topic);

    对于点对点消息:

    QueueReceiver receiver= session.createReceiver (queue);

    7.注册消息的监听者

    对于主题消息:

    TextListener listener=new TextListener();

    subscriber.setMessageListener(listener);

    对于点对点消息:

    TextListener listener=new TextListener();

    receiver.setMessageListener(listener);

    8.开始JMS的连接

       conn.start();

    9.发送和接受消息

    发送消息:

    对于主题消息:

    publisher.publish(message);

    对于点对点消息:

    sender.send(message);

    接受消息:

    对于主题消息:

    Message msg= subscriber.receive();

    对于点对点消息:

    Message msg= receiver.receive(1000);

    10.停止和关闭JMS连接

    conn.stop();

    session.close();

    conn.close();

    转自:http://www.mldn.cn/articleview/2007-2-6/article_view_1377.htm

  • 相关阅读:
    电商工具 谷歌插件 版本 2021-03-04
    PowerDesigner 自定义脚本
    MapReduce案例之寻找共同好友
    Hadoop之MapReduce开发总结
    python之文件操作
    python字典、集合
    python元组
    python列表练习
    python之列表
    python之编码解码、字符串常用方法
  • 原文地址:https://www.cnblogs.com/monica/p/1788739.html
Copyright © 2011-2022 走看看