我使用ActiveMQ cpp 3.7.0和VS2010来构建一个客户端,服务器是ActiveMQ 5.8。我已经根据here中提到的内容管理系统配置,使用类似于以下代码的代码创建了一个消息消费者。ConnClass既是ExceptionListener又是MessageListener。在调用cms::Session::commit()之前,我只想消费一条消息。
void ConnClass::setup()
{
// Create a ConnectionFactory
std::tr1::shared_ptr<ConnectionFactory> connectionFactory(
ConnectionFactory::createCMSConnectionFactory(
"tcp://localhost:61616?cms.PrefetchPolicy.queuePrefetch=1");
// Create a Connection
m_connection = std::tr1::shared_ptr<cms::Connection>(
connectionFactory->createConnection());
m_connection->start();
m_connection->setExceptionListener(this);
// Create a Session
m_session = std::tr1::shared_ptr<cms::Session>(
m_connection->createSession(Session::SESSION_TRANSACTED));
// Create the destination (Queue)
m_destination = std::tr1::shared_ptr<cms::Destination>(
m_session->createQueue("myqueue?consumer.prefetchSize=1"));
// Create a MessageConsumer from the Session to the Queue
m_consumer = std::tr1::shared_ptr<cms::MessageConsumer>(
m_session->createConsumer( m_destination.get() ));
m_consumer->setMessageListener( this );
}
void ConnClass::onMessage( const Message* message )
{
// read message code ...
// schedule a processing event for
// another thread that calls m_session->commit() when done
}问题是我在调用m_session->commit()之前收到了多条消息,而不是一条消息--我知道这一点,因为commit()调用是由用户输入触发的。如何确保在每次调用commit()之前只调用一次onMessage()
发布于 2013-07-20 04:02:45
它不是这样工作的。当使用异步消费者时,消息会在onMessage方法完成后以最快的速度传递。如果您想使用且仅使用一条消息,则使用同步接收调用。
对于异步消费者,预取允许代理缓冲客户端上的工作,而不是一次触发一个,因此您通常可以获得更好的性能,在您的情况下,当异步onMessage调用完成时,ack被发送回代理,下一条消息被发送到客户端。
发布于 2017-12-08 16:49:39
是的,我也发现了这个。但是,当我为异步消费者使用目标URI选项( "consumer.prefetchSize=15“,http://activemq.apache.org/cms/configuring.html#Configuring-DestinationURIParameters )时,它工作得很好。
顺便说一句,我只是在CentOS 7上使用Tim最新的ActiveMQ-CPP v3.9.4和ActiveMQ v5.12.1。
谢谢!
https://stackoverflow.com/questions/17750579
复制相似问题