JMS和ActiveMQ学习笔记一

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方法,这个方法会发送一个请求消息,然后等待一个响应消息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值