理解JMS

13 篇文章 0 订阅
1 篇文章 0 订阅

首先JMS存在的理由:

RPC(Remote procedure call )系统以及JAVA的RMI都是采用基于服务端、客服端协同完成数据通信的,然而他们有一个共同点就是同步的,调用者必须阻止和等待,直到被调用的方法完成执行;而且两者之间的通信在同一时间必须都是处于活动状态。这样在服务端和客服端就死死的紧密的耦合在了一起,在企业应用中很不方便。应运而生的就是JMS,然后应运而生的就是基于JMS的面向消息中间件(Message-Oriented Middleware:MOM)。因为MOM能提供这种解决方案,它们是基于异步交互模式,并提供一个消息队列的抽象的,可以在网络上传输和访问。

 

JMS的概念:

JMS,Java Message Service,是一个在 Java标准化组织(JCP)内开发的标准(代号JSR 914),是Java平台上有关面向消息中间件(MOM)的技术规范,它便于消息系统中的Java应用程序进行消息交换,并且通过提供标准的产生、发送、接收消息的接口简化企业应用的开发,翻译为Java消息服务。

可以把它理解为是一套实现某类特定需求的一套接口,或者说是一套API,或者说是一套标准,或者说是一套规范,它的存在类似于JDBC一样。许多厂商针对标准就开发了自己的不同产品,JMS则提供与厂商无关的访问方法,以访问消息收发服务。许多厂商目前都支持 JMS,包括 IBM 的 MQSeries、BEA的 Weblogic JMS service和 Progress 的 SonicMQ以及Apache的ActiveMQ,这些MOM产品就跟针对JDBC的不同数据库的驱动包类似的效果,类似的概念。

Java消息服务(JMS)的设计,可以很容易地开发业务应用程序异步发送和接收的业务数据和事件,它可以很容易地和有效地支持广泛的企业消息传递产品。

 

JMS应用程序的成员:

一个JMS实现者:一个实现了JMS规范的消息管理系统

多个JMS客服端:发送和接收消息的JAVA应用程序
消息体: 指的是在客服端之间传输的对象
管理消息的对象: 使用JMS的管理员预先配置的JMS对象

 

JMS的消息传递模型

1.Point-to-Point (Queue destination): In this model, a message is delivered from a producer to one consumer. The messages are delivered to the destination, which is a queue, and then delivered to one of the consumers registered for the queue. While any number of producers can send messages to the queue, each message is guaranteed to be delivered, and consumed by one consumer. If no consumers are registered to consume the messages, the queue holds them until a consumer registers to consume them.

点对点的基于队列的模型:在这个模型中,一个消息主体从一个生产这传输到一个消费者。生产者将生产的消息传递到队列(目的地),然后该队列将消息传输到一个在该队列上注册过的一个消费者。多个生产者都可以将消息传递到队列,每一个消息都将保证与一个消费者之间进行传输并消费。如果没有消费者的注册消费的消息,队列将保留,直到消费者请求消费它。

2.Publish/Subscribe (Topic destination): In this model, a message is delivered from a producer to any number of consumers. Messages are delivered to the topic destination, and then to all active consumers who have subscribed to the topic. In addition, any number of producers can send messages to a topic destination, and each message can be delivered to any number of subscribers. If there are no consumers registered, the topic destination doesn't hold messages unless it has durable subscription for inactive consumers. A durable subscription represents a consumer registered with the topic destination that can be inactive at the time the messages are sent to the topic.

发布订阅模型:在这个模型中,一个由生产者(发布者)生产的消息可以传递给多个消费者。发布的消息首先被传输到Topic(目的地),可以将它看成是一个主题,接着将消息传输到所有订阅了这个主题的并且处于活动状态的消费者。此外,任何数量的生产者可以向主题topic发送消息,每个消息可以发送到任意数量的订阅者。如果没有用户订阅该消息,主题topic不保留消息,除非它有持久订阅者。一个持久订阅者指的是一个订阅了某个主题的一个消费者,在消息被发送到主题的这段时间里该订阅者是处于非活动状态的,此时topic才持久化该消息

 

JMS编程模型:

 

一个JMS应用程序由业务定义的消息和一组消费这些消息的消费者构成。通过使用JMS的提高的API来发送和接受这些消息进行交互。一个消息由三部分组成:消息头,属性和主体:

  • The header, which is required for every message, contains information that is used for routing and identifying messages. Some of these fields are set automatically, by the JMS provider, during producing and delivering a message, and others are set by the client on a message by message basis.

   消息头,每个消息都必须有消息头,包含该消息的指向和定义的一些信息,消息头里面的部分字段是被JSM产品提供商在生产和传递过程中自动设值的,另一部分字段是在基本消息体的基础上被客服端设值的。

  • Properties, which are optional, provide values that clients can use to filter messages. They provide additional information about the data, such as which process created it, the time it was created. Properties can be considered as an extension to the header, and consist of property name/value pairs. Using properties, clients can fine-tune their selection of messages by specifying certain values that act as selection criteria.

    属性信息,是可选的,是以键值对出现的,可以方便消费者用来过滤消息。他是用来记录一些额外的一些数据,比如该消息是哪个进程创建的,什么时间创建的。属 性可以被认为是作为消息头的一个扩展,由键值对组成。使用属性,客服端可以根据他们的选择标准选择他们需要的消息。

  • The body, which is also optional, contains the actual data to be exchanged. The JMS specification defined six type or classes of messages that a JMS provider must support:

    消息体,也是可选的,包含在交换过程中的实际数据。JMS规范定义了6个类型或类别的消息, JMS产品提供商必须支持这些类型:

  • Message : This represents a message without a message body.消息不包含消息主体的消息
  • StreamMessage : A message whose body contains a stream of Java primitive types. It is written and read sequentially.消息体包含能被顺序读写的Java基本数据类型流的消息
  • MapMessage : A message whose body contains a set of name/value pairs. The order of entries is not defined.消息体包含没有先后顺序的一组键值对的消息
  • TextMessage : A message whose body contains a Java string...such as an XML message.消息体包含类似于XML格式的Java字符串的消息
  • ObjectMessage : A message whose body contains a serialized Java object.消息体包含Java序列化对象的消息
  • BytesMessage : A message whose body contains a stream of uninterpreted bytes.消息体包含未经过解析的字节流的消息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值