0

I am using activemq-cpp 3.7.0 with VS 2010 to build a client, the server is ActiveMQ 5.8. I have created a message consumer using code similar to the following, based on the CMS configurations mentioned here. ConnClass is a ExceptionListener and a MessageListener. I only want to consume a single message before calling 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
}

The problem is I am receiving multiple messages instead of one message before calling m_session->commit() -- I know this because the commit() call is triggered by user input. How can I ensure onMessage() is only called once before each call to commit()?

4

2 回答 2

0

它不是那样工作的。使用异步消费者时,消息的传递速度与 onMessage 方法完成的速度一样快。如果您只想使用一条消息,请使用同步接收调用。

对于异步消费者,预取允许代理缓冲客户端上的工作,而不是一次触发一个,因此您通常可以获得更好的性能,在您的情况下,当异步 onMessage 调用完成时,会将一个 ack 发送回代理,然后下一条消息被发送到客户端。

于 2013-07-19T20:02:44.807 回答
0

是的,我也发现了这个。但是,当我为异步消费者使用目标 URI 选项(“consumer.prefetchSize=15”,http://activemq.apache.org/cms/configuring.html#Configuring-DestinationURIParameters )时,它运行良好。

顺便说一句,我只使用 Tim 最新的 ActiveMQ-CPP v3.9.4 和 CentOS 7 上的 ActiveMQ v5.12.1。

谢谢!

于 2017-12-08T08:49:39.003 回答