通过slowConsumerStrategy启用的AMQ保持断开与请求/回复模式的连接。 SlowConsumerStrategy在广播模式下可以正常工作。但是,一旦我们为请求/回复模式创建了临时主题,我就会继续看到jms警告故障转移重新连接,只有在断开连接时才会发生。下面是我的配置和程序。 用于断开连接的JMS日志:
WARN | Transport (nio://localhost:13131?wireFormat.maxInactivityDuration=120000) failed , attempting to automatically reconnect: java.io.EOFException
我的AMQ目标策略已通过以下设置启用了慢速消费策略。
<slowConsumerStrategy>
<abortSlowAckConsumerStrategy maxTimeSinceLastAck="150000" abortConnection="true"/>
</slowConsumerStrategy>
程序:
public static void startProducer() throws JMSException {
try {
DataProvider.getProperties();
} catch (Exception e2) {
e2.printStackTrace();
}
String url = DataProvider.getActiveMQURL();
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
connectionFactory.setTrustAllPackages(true);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
String amqtopic = DataProvider.getActiveMQTopic();
Topic topic = session.createTopic(amqtopic);
long sleepTime = DataProvider.getActiveProducerSleepTime();
MessageProducer producer = session.createProducer(topic);
producer.setTimeToLive(0);
TemporaryTopic tempTopic = session.createTemporaryTopic();
MessageConsumer subscriber = session.createConsumer(tempTopic);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
TestObject testObj = null;
int i = 1;
Map<String, Double> average = new HashMap<String, Double>();
Map<String, Long> countCache = new HashMap<String, Long>();
while (true) {
ObjectMessage message = session.createObjectMessage();
testObj = new TestObject("TestKey_" + i, DataProvider.getData());
message.setObject(testObj);
String size = (ObjectSizeCalculator.getObjectSize(testObj) / 1000) + "."
+ ((ObjectSizeCalculator.getObjectSize(testObj)) % 1000);
System.out.println("TestObject Size :" + size + "kb");
long startTime = new Date().getTime();
message.setJMSReplyTo(tempTopic);
producer.send(message);
Message receive = subscriber.receive();
Date end = new Date();
long endTime = end.getTime();
long timetaken = endTime - startTime;
System.out.println("Message: " + testObj + ", Sent at " + formatDateYYYYMMddHHmmssSSSS(end)
+ ", Send Timespent : " + timetaken);
calculateAverageSendTime(size, timetaken,
countCache.containsKey(size) ? countCache.get(size) : 0, average);
calculateMessageCount(size, countCache);
System.out.println("Statistics : Average Time[" + average + "], Count[" + countCache + "]");
System.out.println();
System.out.println("####################################");
i++;
Thread.sleep(sleepTime);
}
} catch (JMSException | InterruptedException e) {
try {
e.printStackTrace();
connection.close();
} catch (JMSException e1) {
e1.printStackTrace();
}
} finally {
try {
connection.close();
} catch (JMSException e1) {
e1.printStackTrace();
}
}
}
});
t.start();
}