据我所知RabbitMQ 不支持支持XA风格的交易。
是的,你可以在Java中做到这一点:
TestConfiguration.java发送的
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.SingleConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration extends AbstractRabbitConfiguration {
private String routingKey = "test.queue";
private String testQueueName = "test.queue";
public ConnectionFactory getConnectionFactory() {
SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}
@Override
public RabbitTemplate rabbitTemplate() {
RabbitTemplate rabbitTemplate = new RabbitTemplate(getConnectionFactory());
rabbitTemplate.setRoutingKey(routingKey);
rabbitTemplate.setQueue(testQueueName);
return rabbitTemplate;
简单的例子:
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
public class MessageSender {
@Autowired
private AmqpTemplate template;
public void send(String text) {
template.convertAndSend(text);
}
}
..和接收:
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class MessageHandler implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("Received message: " + message);
}
}
阅读一些文档,似乎rabbitmq不支持XA。现在我真的很好奇什么是'完美'解决方案...... – 2012-04-12 00:41:56
对于同步层,你考虑过[Change Data Capture](https://en.wikipedia.org/wiki/Change_data_capture)吗? [This thread](http://stackoverflow.com/questions/9918519/generating-events-from-sql-server)在散文中进行了一些更详细的介绍,但它并未深入或在代码中涵盖您的解决方案。如果这是你的想象,我很乐意在这里回答这个问题。 :) – MrGomez 2012-04-13 23:49:14