ActiveMQ使用总结

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

selector支持下列几种方式:

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

(2) Byte strings: “myBytes <> "0X0AFC23"”

(3) Numeric values: "NoltemsInStock > 20"

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

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

使用方式:

(1)创建生产者在send函数之前添加过滤属性:

  message->setStringProperty("Color",pColor);

  producer->send(message);

(2)在创建消费者createConsumer时进行如下设置:

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

  m_pConsumer = m_pSession->createConsumer(m_pDestination, m_MessageFilter);

  m_pConsumer->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->createSession( Session::CLIENT_ACKNOWLEDGE);

}

else

{

session = connection->createSession( Session::AUTO_ACKNOWLEDGE);

}

7、ActiveMQ的Topic设置

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

if(useTopic)

{

   destination = session->createTopic(destURI);

}

else

{

   destination = session->createQueue(destURI);

}

8、ActiveMQ的DeliveryMode设置(生产者时设置)

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

if(usePersistent)

{

   producer->setDeliveryMode( DeliveryMode::PERSISTENT);

}

else

{

   producer->setDeliveryMode( DeliveryMode::NON_PERSISTENT);

}

猜你喜欢

转载自tdcq.iteye.com/blog/2079115