JMS和ActiveMQ学习笔记一
JMS和ActiveMQ学习笔记一
猪刚烈 发表于3年前
JMS和ActiveMQ学习笔记一
  • 发表于 3年前
  • 阅读 45
  • 收藏 1
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

1.为什么异步调用是高效的?

  Systems that rely upon synchronous requests typically have a limited ability to scale because eventually requests will begin to back up, thereby slowing the whole system.
《ActiveMQ in Action》一书在解释同步调用的系统为什么会存在性能问题,或者反过来说为什么异步调用的系统性能要好的时候是这样解释的:同步调用总会有可能出现请求因为没有及时响应而导致阻塞的情况发生!

2.JMS的消息格式

  和所有其他的通信规范一样,JMS也会定义消息的格式,如同http的request和response格式一样,jms的消息也分成header和body两部分:

header部分主要是由预定义属性和一些自定义属性组成,一些重要的预定义属性有:

JMSDestination:消息的目的地

JMSDeliveryMode:消息的投递模式,有两种Persistent和Nonpersistent

JMSExpiration:消息的期限

JMSMessageID:消息的ID

JMSPriority:消息的优先级

....

body部分,JMS定义了六种java类型,它们是:
  1 Message —The base message type. Used to send a message with no payload, only
headers and properties. Typically used for simple event notification.
  2 TextMessage —A message whose payload is a String. Commonly used to send simple
textual and XML data.
  3 MapMessage —Uses a set of name/value pairs as its payload. The names are of
type String and the values are a Java primitive type.
  4 BytesMessage —Used to contain an array of uninterpreted bytes as the payload.
  5 StreamMessage—A message with a payload containing a stream of primitive Java
types that’s filled and read sequentially.
  6 ObjectMessage—Used to hold a serializable Java object as its payload. Usually
used for complex Java objects. Also supports Java collections.

3.消息的发布机制

  所有MOM现在都支持两种消息发布机制:point-to-point和publish-subscribe。

点对点通信机制如下图:


订阅发布通信机制如下图:

补充:关于Request/Response风格的消息通信.


      Although the JMS spec doesn’t define request/reply messaging as a formal messaging domain, it does provide some message headers and a couple of convenience classes for handling basic request/reply messaging.

  JMS没有定义这种风格的通信机制,但是通过一些消息header的属性和工具类是很容易实现这种通信模型的。这里所需要的header主要是JMSReplyTo和JMSCorrelationID两个属性.
The JMSReplyTo specifies the destination where a reply should be sent, and the JMSCorrelationID in the reply message specifies the JMSMessageID of the request message. These headers are used to link the reply message(s) to the original request message.
      JMSReplyTo指明了响应应该送交的地址,很显然这是请求消息要提供的。JMSCorrelationID则用来指明这则消息是回复给那个请求的,因此它应该是响应消息里要提供的。
  The convenience classes for handling basic request/reply are the QueueRequestor and the TopicRequestor. These classes provide a request() method that sends a request message and waits for a reply message through the creation of a temporary destination where only one reply per request is expected.
      而工具类,则是指QueueRequestor和TopicRequestor的request方法,这个方法会发送一个请求消息,然后等待一个响应消息。

共有 人打赏支持
粉丝 22
博文 708
码字总数 110
作品 1
×
猪刚烈
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: