ActiveMQ 出现FixedBackOff 异常一直出现无限制的连接导致资源占用异常

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



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值