对于ActiveMQ Artemis主备切换的分裂脑问题,可通过在broker.xml中配置static-connectors来解决。具体实现方法如下:
master-connector
slave-connector
slave-connector
master-connector
master-connector
slave-connector
public class FailoverListener extends ActiveMQFailoverListener {
@Override
public void failoverEvent(FailoverEventType eventType) throws ActiveMQException {
if (eventType == FailoverEventType.FAILURE_DETECTED) {
// 主broker宕机,将备broker转换为主broker
String newMasterBrokerUrl = "tcp://127.0.0.1:61617";
ServerLocator serverLocator = ActiveMQClient.createServerLocator(newMasterBrokerUrl);
ClientSessionFactory sessionFactory = serverLocator.createSessionFactory();
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(sessionFactory);
// 改变static-connectors的connector-ref
cf.getConnection().createSession().createQueue("artemis.dev.queue");
System.err.println("Switched to the new master: " + new