ActiveMQ Producer 发送消息有同步异步两种方式。
mq发送消息的多种方式.png
ActiveMQ 事务
client-broker事务交互.png优点: 保证消息不丢失.
缺点: 性能损失。客户端多了事务创建,提交,回滚的IO消耗。Broker需要额外维护事务信息。
Sync Send Msg With TX
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Topic topic = session.createTopic("Test.Transactions");
MessageProducer producer = session.createProducer(topic);
producer.send(session.createTextMessage("message"));
// 没有显示commit,不会把消息添加到pending队列中.
session.commit();
Sync Send Msg Without TX
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createQueue("studyQueue"));
Message message = session.createTextMessage("hello everybody!");
// 消息会立刻发送到broker
producer.send(message);
session.close();
网友评论