2017-10-16 09:45:33,809 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] - Could not refresh JMS Connection for destination 'topic://SOMC_OKH_TOPIC' - retrying using FixedBackOff{interval=5000, currentAttempts=3, maxAttempts=unlimited}. Cause: Could not connect to broker URL: tcp://192.168.170.84:61616. Reason: java.net.ConnectException: Connection refused: connect
在做消息队列时出现连接服务器一直拒绝的情况,程序在后台出现了无限制的请求连接,导致资源不停的在访问,占用资源出现异常情况,从异常情况看 - retrying using FixedBackOff{interval=5000, currentAttempts=3, maxAttempts=unlimited}. 看出maxAttempts这个参数出现无限制的情况。根据FixedBackOff类去查看
package org.springframework.util.backoff;
/**
* A simple {@link BackOff} implementation that provides a fixed interval
* between two attempts and a maximum number of retries.
*
* @author Stephane Nicoll
* @since 4.1
*/
public class FixedBackOff implements BackOff {
/**
* The default recovery interval: 5000 ms = 5 seconds.
*/
public static final long DEFAULT_INTERVAL = 5000;
/**
* Constant value indicating an unlimited number of attempts.
*/
public static final long UNLIMITED_ATTEMPTS = Long.MAX_VALUE;
private long interval = DEFAULT_INTERVAL;
private long maxAttempts = UNLIMITED_ATTEMPTS;
/**
* Create an instance with an interval of {@value #DEFAULT_INTERVAL}
* ms and an unlimited number of attempts.
*/
public FixedBackOff() {
}
/**
* Create an instance.
* @param interval the interval between two attempts
* @param maxAttempts the maximum number of attempts
*/
public FixedBackOff(long interval, long maxAttempts) {
this.interval = interval;
this.maxAttempts = maxAttempts;
}
/**
* Set the interval between two attempts in milliseconds.
*/
public void setInterval(long interval) {
this.interval = interval;
}
/**
* Return the interval between two attempts in milliseconds.
*/
public long getInterval() {
return interval;
}
/**
* Set the maximum number of attempts in milliseconds.
*/
public void setMaxAttempts(long maxAttempts) {
this.maxAttempts = maxAttempts;
}
/**
* Return the maximum number of attempts in milliseconds.
*/
public long getMaxAttempts() {
return maxAttempts;
}
@Override
public BackOffExecution start() {
return new FixedBackOffExecution();
}
private class FixedBackOffExecution implements BackOffExecution {
private long currentAttempts = 0;
@Override
public long nextBackOff() {
this.currentAttempts++;
if (this.currentAttempts <= getMaxAttempts()) {
return getInterval();
}
else {
return STOP;
}
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("FixedBackOff{");
sb.append("interval=").append(FixedBackOff.this.interval);
String attemptValue = (FixedBackOff.this.maxAttempts == Long.MAX_VALUE ?
"unlimited" : String.valueOf(FixedBackOff.this.maxAttempts));
sb.append(", currentAttempts=").append(this.currentAttempts);
sb.append(", maxAttempts=").append(attemptValue);
sb.append('}');
return sb.toString();
}
}
}
需要设置maxAttempts的值,在spring 集成中配置
<bean id="backOff" class="org.springframework.util.backoff.FixedBackOff">
<property name="maxAttempts" value="3"/>
</bean>
在监听器中配置该属性backOff 即可
<bean id="queueListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="demoTopicDestination" />
<property name="messageListener" ref="topicMessageListen" />
<property name="backOff" ref="backOff"/>
<property name="sessionAcknowledgeMode" value="4"/>
</bean>
结果出现连接不上的只有尝试连接3次
2017-10-16 09:45:33,809 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] - Could not refresh JMS Connection for destination 'topic://SOMC_OKH_TOPIC' - retrying using FixedBackOff{interval=5000, currentAttempts=3, maxAttempts=3}. Cause: Could not connect to broker URL: tcp://192.168.170.84:61616. Reason: java.net.ConnectException: Connection refused: connect