- RabbitMQ Cookbook
- Sigismondo Boschi Gabriele Santomaggio
- 269字
- 2021-07-19 18:52:49
Messaging with transactions
In this example we will discuss how to use channel transactions. In the Producing messages recipe we have seen how to use a persistent message, but if the broker can't write the message to the disk, you can lose the message. With the AQMP transactions you can be sure that the message won't be lost.
You can find the source at Chapter01/Recipe12/Java_12/
.
Getting ready
To use this recipe you will need to set up the Java development environment as indicated in the Introduction section.
How to do it…
You can use transactional messages by performing the following steps:
- Create a persistent queue
channel.queueDeclare(myQueue, true, false, false, null);
- Set the channel to the transactional mode using:
channel.txSelect();
- Send the message to the queue and then commit the operation:
channel.basicPublish("", myQueue, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); channel.txCommit();
How it works…
After creating a persistent queue (step 1), we have set the channel in the transaction mode using the method txSelect()
(step 2). Using txCommit()
the message is stored in the queue and written to the disk; the message will then be delivered to the consumer(s).
The method txSelect()
must be called at least once before txCommit()
or txRollback()
.
As in a DBMS you can use a rollback method. In the following case the message isn't stored or delivered:
channel.basicPublish("",myQueue, MessageProperties.PERSISTENT_TEXT_PLAIN ,message.getBytes()); channel.txRollback();
There's more…
The transactions can reduce the application's performance, because the broker doesn't cache the messages and the tx
operations are synchronous.
See also
In the next chapter we will discuss the publish confirm plugin, which is a faster way to get the confirmation for the operations.
- Mastering Adobe Captivate 2017(Fourth Edition)
- 編程卓越之道(卷3):軟件工程化
- PHP+MySQL+Dreamweaver動態(tài)網(wǎng)站開發(fā)實例教程
- 鋒利的SQL(第2版)
- 單片機C語言程序設(shè)計實訓(xùn)100例
- SQL Server與JSP動態(tài)網(wǎng)站開發(fā)
- 執(zhí)劍而舞:用代碼創(chuàng)作藝術(shù)
- Python項目實戰(zhàn)從入門到精通
- Orchestrating Docker
- PrimeFaces Blueprints
- Arduino可穿戴設(shè)備開發(fā)
- Illustrator CS6設(shè)計與應(yīng)用任務(wù)教程
- Java EE 8 and Angular
- 高效使用Greenplum:入門、進階與數(shù)據(jù)中臺
- Web前端開發(fā)最佳實踐