我们有两个运行的ActiveMQ(版本5.10.0)实例,我正在使用共享存储来实现HA。 但是,我无法看到生产者和消费者发生故障转移。
ActiveMQ broker-1在IP2上运行,在IP2上运行broker-2 在配置的activemq.xml下,我修改了持久性适配器以使用IP1上的共享目录。
<persistenceAdapter>
<kahaDB directory="\\IP1\shared-directory\for activemq\data"/>
</persistenceAdapter>
在生产者和消费者方面,我都使用以下JNDI配置来获取连接和构建会话等。
jndi.properties
java.naming.factory.initial = ..........ActiveMQInitialContextFactory
java.naming.provider.url = failover:(tcp://IP1:61616,tcp://IP2:61616)?randomize=false
connectionFactoryNames = myConnectionFactory
queue.requestQ = my.RequestQ
有趣的部分是:
当我开始这个经纪人对时,我看到其中一个经纪人变成了主人。 当我启动生产者时,它将消息放在Q上(假设生产者在Q上放了100条消息)。我的制作人还在运行;我关闭了主代理,因此奴隶代理获得文件锁并成为主。当我打开webconsole时,我看到Q上仍然有100条消息。即使生产商正在运行,它也不再在此Q上放置任何消息。 / p>
对于消费者也类似。 消费者正在从Q中选择消息,这个Q已经说主消息时未消耗100条消息,现在主机关闭,从机成为主机,我看到100条消息仍未消耗,但消费者没有从Q中选择任何消息。
我等他们故障转移了很长时间。(&gt; 10分钟。) 任何人都可以建议我缺少什么配置吗?
我是复制粘贴生产者和消费者的原因(我已经在动作簿中通过微小的修改从ActiveMQ复制了这个)。
生产者
public class Producer {
private static String brokerURL = "failover:(tcp://IP1:3389,tcp://IP2:3389)";
private static transient ConnectionFactory factory;
private transient Connection connection;
private transient Session session;
private transient MessageProducer producer;
private static int count = 10;
private static int total;
private static int id = 1000000;
private String jobs[] = new String[] { "suspend", "delete" };
public Producer() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(null);
}
public void close() throws JMSException {
if (connection != null) {
connection.close();
}
}
public static void main(String[] args) throws JMSException {
Producer producer = new Producer();
while (total < 1000) {
for (int i = 0; i < count; i++) {
producer.sendMessage();
}
total += count;
System.out.println("Sent '" + count + "' of '" + total
+ "' job messages");
try {
Thread.sleep(1000);
} catch (InterruptedException x) {
}
}
producer.close();
}
public void sendMessage() throws JMSException {
int idx = 0;
while (true) {
idx = (int) Math.round(jobs.length * Math.random());
if (idx < jobs.length) {
break;
}
}
String job = jobs[idx];
Destination destination = session.createQueue("JOBS." + job);
Message message = session.createObjectMessage(id++);
System.out.println("Sending: id: "
+ ((ObjectMessage) message).getObject() + " on queue: "
+ destination);
producer.send(destination, message);
}
}
消费
public class Consumer {
private static String brokerURL = "failover:(tcp://IP1:3389,tcp://IP2:3389)";
private static transient ConnectionFactory factory;
private transient Connection connection;
private transient Session session;
private String jobs[] = new String[] { "suspend", "delete" };
public Consumer() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
public void close() throws JMSException {
if (connection != null) {
connection.close();
}
}
public static void main(String[] args) throws JMSException {
Consumer consumer = new Consumer();
for (String job : consumer.jobs) {
Destination destination = consumer.getSession().createQueue(
"JOBS." + job);
MessageConsumer messageConsumer = consumer.getSession()
.createConsumer(destination);
messageConsumer.setMessageListener(new Listener(job));
}
}
public Session getSession() {
return session;
}
}
还有一件事: 我对消费者故障转移比生产者更感兴趣。 还有一个观察结果是:消费者停止并突然进入命令提示符。
谢谢。 -JE