参考博文:http://www.cnblogs.com/SzeCheng/p/4792084.html
参考博文:http://activemq.apache.org/producer-flow-control.html
名词解释:
P:生产者
C:消费者
服务端:P 或者 ActiveMQ服务
客户端:ActiveMQ服务 或者 C
客户端成功接收一条消息的标志是这条消息被签收。成功接收一条消息一般包括如下三个阶段:
1.客户端接收消息;
2.客户端处理消息;
3.消息被签收。
session = connection.createSession(Boolean.false, Session.CLIENT_ACKNOWLEDGE);##第一个参数控制事务,第二个参数控制消息
另外ACKNOWLEDGE说明:
根据API说明
http://activemq.apache.org/maven/apidocs/org/apache/activemq/ActiveMQSession.html#acknowledge--
如果调用ack将会ack这个session的所有消息。
Acknowledges all consumed messages of the session of this consumed message.
All consumed JMS messages support the acknowledge
method for use when a client has specified that its JMS session's consumed messages are to be explicitly acknowledged. By invokingacknowledge
on a consumed message, a client acknowledges all messages consumed by the session that the message was delivered to.
Calls to acknowledge
are ignored for both transacted sessions and sessions specified to use implicit acknowledgement modes.
A client may individually acknowledge each message as it is consumed, or it may choose to acknowledge messages as an application-defined group (which is done by calling acknowledge on the last received message of the group, thereby acknowledging all messages consumed by the session.)Messages that have been received but not acknowledged may be redelivered.
还好AcitveMq提供了另外一种方法来避免一个消息一次ack 将会ack掉全部消息的问题。
session = connection.createSession(Boolean.FALSE,
ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
参考 Java Enterprise Edition : A Practical Approach - 第 228 页
在不带事务的 Session 中,一条消息何时和如何被签收取决于Session的设置。
1.Session.AUTO_ACKNOWLEDGE
当客户端从 receive 或 onMessage成功返回时,Session 自动签收客户端的这条消息的收条。
2.Session.CLIENT_ACKNOWLEDGE
客户端通过调用消息的 acknowledge 方法签收消息。
message.acknowledge();
在带事务的 Session 中,签收自动发生在事务提交时。如果事务回滚,所有已经接收的消息将会被再次传送。其实这里的Session.CLIENT_ACKNOWLEDGE 用处不大。
session = connection.createSession(Boolean.TRUE, Session.CLIENT_ACKNOWLEDGE);
session.commit();
总结:
1、对于生产者:服务端端为P,客户端为ActiveMQ服务。 Session设置为AUTO_ACKNOWLEDGE 和CLIENT_ACKNOWLEDGE ,相对来说区别不是很大,根据情况考虑。
2、对于消费者:服务端为ActiveMQ为服务,客户端为C。 Session设置为AUTO_ACKNOWLEDGE ,接收到消息(receive 或 onMessage成功返回时),即为消费成功,然后从队列里移除该数据。不关心该数据有没有正确被处理成我们想要的结果;Session设置为CLIENT_ACKNOWLEDGE 时,必须手动调用acknowledge 方法才为消费成功,然后从队列里移除该条数据。
3、P和C的Session设置成哪种模式,互不影响。