virtual void run() {

      try {


        // Create a ConnectionFactory
        ConnectionFactory * connectionFactory(ConnectionFactory::createCMSConnectionFactory("failover:tcp://127.0.0.1:61616?jms.prefetchPolicy.queuePrefetch=1"));

        // Create a Connection
        connection = connectionFactory->createConnection();
        //connection->setDispatchAsync(false);
        connection->start();
        connection->setExceptionListener(this);

        // Create a Session
        session = connection->createSession(Session::SESSION_TRANSACTED);
        destination = session->createQueue("MHIVE.OUTPUT?consumer.prefetchSize=1");

        // Create a MessageConsumer from the Session to the Topic or Queue
        consumer = session->createConsumer(destination);

        consumer->setMessageListener(this);

        std::cout.flush();
        std::cerr.flush();

        // Indicate we are ready for messages.
        //latch.countDown();

        // Wait while asynchronous messages come in.
        //doneLatch.await(waitMillis);

      } catch (CMSException& e) {
        // Indicate we are ready for messages.
        // latch.countDown();
        e.printStackTrace();
      }
    }
예제 #2
0
    void subscribe(const vector<string>& messageTypes_) {
        try {
        // Create a ConnectionFactory
        auto_ptr<ConnectionFactory> connectionFactory(
            ConnectionFactory::createCMSConnectionFactory(_brokerURI));
        // Create a Connection
        _connection = connectionFactory->createConnection();

        _connection->setExceptionListener(this);

        // Create a Session
         _session = _connection->createSession(Session::AUTO_ACKNOWLEDGE);

         // Create consumers
         for (vector<string>::const_iterator iter = messageTypes_.begin();
             iter != messageTypes_.end(); ++iter) {
            Destination* destination = _session->createTopic(*iter);
            MessageConsumer* consumer = _session->createConsumer(destination);
            consumer->setMessageListener(this);

            destinations.push_back(destination);
            consumers.push_back(consumer);
         }
// start the connection after subscribe all topics
		         _connection->start();

        } catch (CMSException& e) {
            // Log the error
            cerr << "Opps! A exception happened: " << e.getMessage() << endl;
        }
    }
예제 #3
0
void Producter::sendMsg(const QString& aMsg)
{
    qDebug()<<">> TEST INFO:"<<__FUNCTION__<<__LINE__;

    // Create a Connection

    try {
        m_producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);

        m_tempDest = m_session->createTemporaryQueue();
        MessageConsumer* responseConsumer = m_session->createConsumer(m_tempDest);
        responseConsumer->setMessageListener(this);
        QString queueName = QString::fromStdString(m_tempDest->getQueueName());
        if (!m_responseConsumerMap.keys().contains(queueName))
        {
            m_responseConsumerMap.insert(queueName, responseConsumer);
        }
        //Now create the actual message you want to send
        m_txtMessage = m_session->createTextMessage();
        m_txtMessage->setText(aMsg.toStdString());

        m_txtMessage->setCMSReplyTo(m_tempDest);

        QString randStr;
        getRandString(randStr);
        m_txtMessage->setCMSCorrelationID(randStr.toUtf8().data());
        m_producer->send(m_txtMessage);
//        TextMessage* textMessage = dynamic_cast<TextMessage*>(m_responseConsumer->receive());
//        string text = "";

//        if (textMessage != NULL) {
//            text = textMessage->getText();
//        } else {
//            text = "NOT A TEXTMESSAGE!";
//            return;
//        }

//        qDebug()<<">> TEST INFO:"<<__FUNCTION__<<__LINE__ << QString::fromUtf8(text.c_str());
//        emit messageReceived(QString::fromUtf8(text.c_str()));

        delete m_txtMessage;
        m_txtMessage = NULL;
//        m_responseConsumer->close();
//        delete m_responseConsumer;
//        m_responseConsumer = NULL;
        delete m_tempDest;
        m_tempDest = NULL;

    } catch (CMSException e) {
        //Handle the exception appropriately
    }
}
예제 #4
0
void AmqClient::run(void *ptr)
{
	CountDownLatch latch(1);
	Connection *connection = NULL;
	Session *session = NULL;
	Destination *destination = NULL;
	MessageConsumer *consumer = NULL;

	ifstream ifs;
	string line;
	vector<string> fields;

	activemq::library::ActiveMQCPP::initializeLibrary();
	auto_ptr<ConnectionFactory> connectionFactory(ConnectionFactory::createCMSConnectionFactory(_brokerUrl.c_str()));

	do {
		try {
			connection = connectionFactory->createConnection();
		} catch (...) {
			connection = NULL;
			sleep(3);
		}
	} while (connection == NULL);
	connection->start();

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

	ifs.open(_topicFile.c_str());
	while(getline(ifs, line)) {
		if(line.empty() || line[0] == '#') {
			continue;
		}

		fields.clear();
		if(Utils::splitStr(line, fields, ':') != 2) {
			continue;
		}

		BaseTabListen *baseTab = new BaseTabListen(_pEnv, fields[0]);
		destination = session->createQueue(fields[1].c_str());
		consumer = session->createConsumer(destination);
		consumer->setMessageListener(baseTab);
	}

	latch.await();
	activemq::library::ActiveMQCPP::shutdownLibrary();
}