我正在使用Alpakka
及其JMS
连接器将Oracle AQ
的数据从队列中取列。我可以按照 this 指南提出下面的基本实现。
我的问题是如何使其成为事务性的,以便我可以保证如果抛出异常,我的消息就不会丢失。
object ConsumerApp extends App {
implicit val system: ActorSystem = ActorSystem("actor-system")
implicit val materializer: ActorMaterializer = ActorMaterializer()
val connectionFactory = AQjmsFactory.getConnectionFactory(getOracleDataSource())
val out = JmsSource.textSource(
JmsSourceSettings(connectionFactory).withQueue("My_Queue")
)
val sink = Sink.foreach { message: String =>
println("in sink: " + message)
throw new Exception("") // !!! MESSAGE IS LOST !!!
}
out.runWith(sink, materializer)
}
如果是PL/SQL
,解决方案就是这样:
DECLARE
dequeue_options DBMS_AQ.DEQUEUE_OPTIONS_T;
message_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
message_handle RAW (44);
msg SYS.AQ$_JMS_TEXT_MESSAGE;
BEGIN
DBMS_AQ.dequeue (
queue_name => 'My_Queue',
dequeue_options => dequeue_options,
message_properties => message_properties,
payload => msg,
msgid => message_handle
);
-- do something with the message
COMMIT;
END;
答案 0 :(得分:2)
流阶段失败时的默认行为是关闭整个流。您必须决定流中的handle errors。例如,一种方法是使用退避策略restart流。
此外,由于您正在使用Alpakka JMS连接器,请将acknowledgement mode设置为ClientAcknowledge
(自Alpakka 0.15起可用)。使用此配置,可以通过JMS源再次传递未确认的消息。例如:
val jmsSource: Source[Message, NotUsed] = JmsSource(
JmsSourceSettings(connectionFactory)
.withQueue("My_Queue")
.withAcknowledgeMode(AcknowledgeMode.ClientAcknowledge)
)
val result = jmsSource
.map {
case textMessage: TextMessage =>
val text = textMessage.getText
textMessage.acknowledge()
text
}
.runForeach(println)