ActiveMQ高并发发送消息异常解决方法

现象,使用10个线程每100ms发送一条消息,大约3000多条后,出现异常,所有线程停止:

javax.jms.JMSException: Could not connect to broker URL: tcp://localhost:61616. Reason:
 java.net.BindException: Address already in use: connect; nested exception is ja
va.net.BindException: Address already in use: connect

原因:创建了太多jms连接没有来得及回收

解决方法:使用jms连接池

原来的配置:

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
		<property name="environment">
			<props>
				<prop key="java.naming.factory.initial">
					org.apache.activemq.jndi.ActiveMQInitialContextFactory
			</prop>
				<prop key="java.naming.provider.url">
					tcp://huzq-linux:61616
			</prop>
			</props>
		</property>
	</bean>

	<bean id="jndiConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>ConnectionFactory</value>
		</property>
		<property name="jndiTemplate">
			<ref local="jndiTemplate"></ref>
		</property>
	</bean>

修改为:

<bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
		<property name="connectionFactory">
			<bean class="org.apache.activemq.ActiveMQConnectionFactory">
				<property name="brokerURL" value="tcp://huzq-linux:61616" />
			</bean>
		</property>
	</bean>

相关推荐