---
title: "ActiveMQ使用总结 "
author: "ssxxjjii"
date: "2014-06-13T11:40:13Z"
tags: []
categories: ["互联网"]
slug: "activemq"
draft: false
lastmod: "2014-06-13T11:40:13Z"
id: 2079115
url: "https://tdcq.iteye.com/blog/2079115"
---

1、ActiveMQ支持消息过滤设置规则和用法

selector支持下列几种方式：

(1) String literals: "color ='blue'"

(2) Byte strings: “myBytes &lt;&gt; "0X0AFC23"”

(3) Numeric values: "NoltemsInStock &gt; 20"

(4) Boolean literals TRUE or FALSE: "AcctDetails=TRUE"

(5) Composite expressions: "Type='car' AND (color = 'blue' OR color ='green') AND weight&gt; 2500"

 

使用方式:

（1）创建生产者在send函数之前添加过滤属性：

  message-&gt;setStringProperty("Color",pColor);

  producer-&gt;send(message);

（2）在创建消费者createConsumer时进行如下设置：

  std::string MessageFilter="color ='blue'"

  m\_pConsumer = m\_pSession-&gt;createConsumer(m\_pDestination, m\_MessageFilter);

  m\_pConsumer-&gt;setMessageListener(this);

 

 

2、错误：Channel was inactive for too long （服务器消息较长时间没有消息发送时，客户端会报这个错误）

解决方法：在建立连接的Uri中加入： wireFormat.maxInactivityDuration=0

 

3、采用failover方式连接导致卡死

解决方法：不采用failover连接

 

4、ActiveMQ发送模式

（1）ActiveMQ异步发送，只需新增参数transport.useAsyncSend=true，具体如下:

      BrokerUri = "tcp://127.0.0.1:61616?transport.useAsyncSend=true"

（2）ActiveMQ同步发送，只需新增参数transport.useAsyncSend=false，具体如下:

      BrokerUri = "tcp://127.0.0.1:61616?transport.useAsyncSend=false"

      其实activeMQ在默认情况下就是同步发送，所以在同步发送时可以简写为：

      BrokerUri = "tcp://127.0.0.1:61616"

 

 

5、错误：The Session is closed（网络异常时客户端会报出这个错误）

解决办法：在建立连接的Uri中加入： maxReconnectDelay=10000

maxReconnectDelay 最大重连间隔

 

 

6、ActiveMQ负责均衡

对broker采取了负载均衡和基于共享文件系统的主备配置，这个时候，客户端生产者和消费者的URI中用

failover:(tcp://192.168.1.117:61616,tcp://192.168.1.118:61616,tcp://broker3:61616)

 

 

6、ActiveMQ的ACK设置

根据不同的需要可以将ACK设置为Session::CLIENT\_ACKNOWLEDGE 或  Session::AUTO\_ACKNOWLEDGE 默认为Session::AUTO\_ACKNOWLEDGE 如：

if( clientAck )

{

session = connection-&gt;createSession( Session::CLIENT\_ACKNOWLEDGE);

}

else

{

session = connection-&gt;createSession( Session::AUTO\_ACKNOWLEDGE);

}

 

7、ActiveMQ的Topic设置

根据不同的需要可以将Topic设置为Topic或  Queue默认为Queue如：

if(useTopic)

{

   destination = session-&gt;createTopic(destURI);

}

else

{

   destination = session-&gt;createQueue(destURI);

}

 

8、ActiveMQ的DeliveryMode设置（生产者时设置）

根据不同的需要可以将DeliveryMode设置为DeliveryMode::NON\_PERSISTENT或  DeliveryMode::PERSISTENT默认为DeliveryMode::NON\_PERSISTENT如：

if(usePersistent)

{

   producer-&gt;setDeliveryMode( DeliveryMode::PERSISTENT);

}

else

{

   producer-&gt;setDeliveryMode( DeliveryMode::NON\_PERSISTENT);

}