0

I have set the timeout property for a activemq connection when I send message to broker.

But I couldn't get any exception or return when it send timeout.

I couldn't gain the status of sending succeed or timeout.

This also happened when I use receive(long long timeout);

Is there any way to distinguish these two states?

Version Activemq 5.4.2 activemq-cpp 3.2.5

URI:

failover:(tcp://192.168.32.11:61617) without any option, all use default.

Connection code:

bool CActiveMqProducer::Initial()
{
    try
    {
        activemq::library::ActiveMQCPP::initializeLibrary();

        //sure has been cleaned up before initial
        if (!m_bCleanUp)
            CleanUp();

        if(m_strBrokerURI == "" || m_strDestURI == "")
        {
            printf("MQ initial failed for m_strBrokerURI == \"\" || m_strDestURI == \"\"\n");
            return false;
        }

        //create a connection factory
        auto_ptr<ActiveMQConnectionFactory> ConnFactoryPtr(new ActiveMQConnectionFactory(m_strBrokerURI, m_strAccount, m_strPsw));

        // Create a Connection
        try
        {
            m_pConnObj = ConnFactoryPtr->createConnection();
            if(m_pConnObj != NULL)
            {

                ActiveMQConnection* amqConnection = dynamic_cast<ActiveMQConnection*>(m_pConnObj);
                amqConnection->setSendTimeout(m_unSendTimeout);
                //here set send timeout option
            }
            else
            {
                return false;
            }

            m_pConnObj->start();
        }
        catch (CMSException& e)
        {
            e.printStackTrace();
            throw e;
        }

        // Create a Session
        if (m_bClientAck)
        {
            m_pSession = m_pConnObj->createSession(Session::CLIENT_ACKNOWLEDGE);
            if(m_pSession == NULL)
                return false;
        }
        else
        {
            m_pSession = m_pConnObj->createSession(Session::AUTO_ACKNOWLEDGE);
            if(m_pSession == NULL)
                return false;
        }

        // Create the destination (Topic or Queue)
        if (m_bUseTopic)
        {
            m_pMsgDest = m_pSession->createTopic(m_strDestURI);
            if(m_pMsgDest == NULL)
                return false;
        }
        else
        {
            m_pMsgDest = m_pSession->createQueue(m_strDestURI);
            if(m_pMsgDest == NULL)
                return false;
        }

        // Create a MessageProducer from the Session to the Topic or Queue
        m_pMsgProducer = m_pSession->createProducer(m_pMsgDest);
        if(m_pMsgProducer == NULL)
            return false;
        if(m_bPresistent)
        {
            m_pMsgProducer->setDeliveryMode(DeliveryMode::PERSISTENT);
        }
        else
        {
            m_pMsgProducer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);
        }

        //control the logic
        m_bInitialized = true;
        m_bCleanUp = false;

    }
    catch (CMSException& e)
    {
        e.printStackTrace();
        return false;
    }
    return true;
}

Send code:

bool CActiveMqProducer::SendTextMessage(const char* msg, int deliveryMode, int priority, long long timeToLive, std::map<std::string,std::string> property)
{
    try
    {
        if(!m_bInitialized)
        {
            printf("MQ client has not been initialized!\n");
            return false;
        }
        TextMessage * tmsg = m_pSession->createTextMessage();
        tmsg->setText(msg);
        std::map<std::string, std::string>::iterator it = property.begin();
        for(; it != property.end(); it++)
        {
            tmsg->setStringProperty(it->first,it->second);
        }

        m_pMsgProducer->send(tmsg, deliveryMode, priority, timeToLive);

        delete tmsg;
    }
    catch(MessageFormatException &e)
    {
        e.printStackTrace();
        return false;
    }
    catch(InvalidDestinationException &e)
    {
        e.printStackTrace();
        return false;
    }
    catch(UnsupportedOperationException &e)
    {
        e.printStackTrace();
        return false;
    }
    catch(CMSException &e)
    {
        //if an internal error occurs while sending the message.
        e.printStackTrace();
        return false;
    }
    return true;

}
jaylong35
  • 3
  • 2

1 Answers1

0

First off I recommend using the latest release v3.4.1 there are many fixes that could affect this. Next you should get a NULL returned from receive when you pass it a timeout value.

As for the timeout option you say you are setting you should be more clear on what option you are setting as there are a couple. There is a requestTimeout that is used to timeout when a message is sent synchronously, and there is a timeout option on the failover transport that also comes into play when the broker connection is down.

Since you have shown any code or the URI its hard to say what all is going on here but for a send if you haven't set the AlwaysSyncSend option then its possible the message is getting sent async.

Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • I'll be using release v3.4.1 in new version for V3.2.5 cannot using PrefetchSize option. The code and URI I show after question.I'm not setting any option in uri. The default value of AlwaysSyncSend is false. – jaylong35 Jan 09 '12 at 03:20
  • @Tim Bish Please look this question.http://stackoverflow.com/questions/19706788/jersey-rest-web-service-with-activemq-middleware-integration Thanks for your time. – Kumar Nov 12 '13 at 07:43