ActiveMQ C++ SDK編譯記錄

1烤蜕、 下載源碼

 wget https://mirror.bit.edu.cn/apache/activemq/activemq-cpp/3.9.5/activemq-cpp-library-3.9.5-src.tar.gz

2、解壓

tar -xzvf activemq-cpp-library-3.9.5-src.tar.gz 

3迹冤、按照依賴工具

 yum install autoconf  libtool

4讽营、編譯

cd activemq-cpp-library-3.9.5
./autogen.sh
./configure

發(fā)現(xiàn)錯誤:

configure: Apache Portable Runtime (APR) library configuration
checking for APR... no
configure: WARNING: APR not found
The Apache Portable Runtime (APR) library cannot be found.
Please install APR on this system and supply the appropriate
--with-apr option to 'configure'
configure: error: no suitable APR found

5、安裝apr

  • 下載
wget https://mirror.bit.edu.cn/apache//apr/apr-1.7.0.tar.gz
  • 解壓
tar -xzvf apr-1.7.0.tar.gz
  • 編譯安裝
./configure && make -j4 && make install
  • 編譯安裝apr后泡徙,日志中會說明安裝頭文件和庫文件的路徑:
 /usr/local/apr/include/apr-1
 /usr/local/apr/lib

6橱鹏、繼續(xù)編譯

  • 回到之前的步驟4, 我們繼續(xù)編譯activemq-cpp-library:
cd activemq-cpp-library-3.9.5
./configure
  • 發(fā)現(xiàn)已經(jīng)能找到apr:
configure: Apache Portable Runtime (APR) library configuration
checking for APR... yes
checking APR version... 1.7.0
  • 繼續(xù)編譯安裝:
 make -j4 && make install 
  • 發(fā)現(xiàn)鏈接OpenSSL失斂懊辍:
libtool: link: g++ -ansi -pedantic -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/local/apr/include/apr-1 -W -Wall -Wextra -Wconversion -fPIC -fstrict-aliasing -Wstrict-aliasing=2 -Wno-long-long -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/local/apr/include/apr-1 -Wno-non-virtual-dtor -Wno-unused-parameter -Wno-uninitialized -I./../main -g -O2 -pthread -o .libs/example example-main.o  ../main/.libs/libactivemq-cpp.so /usr/local/apr/lib/libapr-1.so -lrt -lcrypt -ldl -lpthread -pthread -Wl,-rpath -Wl,/usr/local/lib -Wl,-rpath -Wl,/usr/local/apr/lib
mv -f stress-test/.deps/stress_test-ConnectionFactoryMgr.Tpo stress-test/.deps/stress_test-ConnectionFactoryMgr.Po
/bin/sh ../../libtool  --tag=CXX   --mode=link g++ -ansi -pedantic   -DLINUX -D_REENTRANT -D_GNU_SOURCE  -I/usr/local/apr/include/apr-1    -W -Wall -Wextra -Wconversion -fPIC -fstrict-aliasing -Wstrict-aliasing=2 -Wno-long-long   -DLINUX -D_REENTRANT -D_GNU_SOURCE  -I/usr/local/apr/include/apr-1    -Wno-non-virtual-dtor -Wno-unused-parameter -Wno-uninitialized -I./../main -g -O2 -pthread   -o simple_producer ./producers/simple_producer-SimpleProducer.o ../main/libactivemq-cpp.la  /usr/local/apr/lib/libapr-1.la -lrt -lcrypt  -lpthread -ldl       -lpthread 
../main/.libs/libactivemq-cpp.so: undefined reference to `ERR_error_string_n'
../main/.libs/libactivemq-cpp.so: undefined reference to `ASN1_STRING_length'
../main/.libs/libactivemq-cpp.so: undefined reference to `X509V3_EXT_get'
../main/.libs/libactivemq-cpp.so: undefined reference to `BIO_new'
../main/.libs/libactivemq-cpp.so: undefined reference to `OBJ_obj2nid'
../main/.libs/libactivemq-cpp.so: undefined reference to `SSL_CTX_set_default_passwd_cb'
../main/.libs/libactivemq-cpp.so: undefined reference to `OPENSSL_init_crypto'
../main/.libs/libactivemq-cpp.so: undefined reference to `SSL_CTX_new'
  • 以上未定義的函數(shù)都是OpenSSL libcrypto的莉兰,我們搜索下libcrypto庫路徑:
find / -name "libcrypto.so"
/data/soft/openssl-master/libcrypto.so
/usr/local/lib64/libcrypto.so
  • 懷疑是configure時,檢查到的OpenSSL庫路徑不對礁竞,我們重新指定下:
./configure --with-openssl=/usr/local
make -j4
  • 發(fā)現(xiàn)編譯成功了糖荒,然后安裝,發(fā)現(xiàn)安裝路徑為:
 /usr/local/lib/
/usr/local/include/activemq-cpp-3.9.5/

7、編譯簡單測試程序:

  • 編寫CMakeList.txt
cmake_minimum_required(VERSION 3.17)
project(ActiveMQ)

set(CMAKE_CXX_STANDARD 11)
include_directories(/usr/local/include/activemq-cpp-3.9.5/ /usr/local/apr/include/apr-1)
link_directories(/usr/local/apr/lib /usr/local/lib/)

add_executable(ActiveMQ main.cpp)
target_link_libraries(ActiveMQ activemq-cpp apr-1)
  • 拷貝測試用例到main.cpp
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/core/ActiveMQConnection.h>
#include <activemq/transport/DefaultTransportListener.h>
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Integer.h>
#include <activemq/util/Config.h>
#include <decaf/util/Date.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace activemq;
using namespace activemq::core;
using namespace activemq::transport;
using namespace decaf::lang;
using namespace decaf::util;
using namespace decaf::util::concurrent;
using namespace cms;
using namespace std;

////////////////////////////////////////////////////////////////////////////////
class SimpleAsyncConsumer : public ExceptionListener,
                            public MessageListener,
                            public DefaultTransportListener {
private:

    Connection* connection;
    Session* session;
    Destination* destination;
    MessageConsumer* consumer;
    bool useTopic;
    std::string brokerURI;
    std::string destURI;
    bool clientAck;

private:

    SimpleAsyncConsumer(const SimpleAsyncConsumer&);
    SimpleAsyncConsumer& operator=(const SimpleAsyncConsumer&);

public:

    SimpleAsyncConsumer(const std::string& brokerURI,
                        const std::string& destURI,
                        bool useTopic = false,
                        bool clientAck = false) :
            connection(NULL),
            session(NULL),
            destination(NULL),
            consumer(NULL),
            useTopic(useTopic),
            brokerURI(brokerURI),
            destURI(destURI),
            clientAck(clientAck) {
    }

    virtual ~SimpleAsyncConsumer() {
        this->cleanup();
    }

    void close() {
        this->cleanup();
    }

    void runConsumer() {

        try {

            // Create a ConnectionFactory
            ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory(brokerURI);

            // Create a Connection
            connection = connectionFactory->createConnection();
            delete connectionFactory;

            ActiveMQConnection* amqConnection = dynamic_cast<ActiveMQConnection*>(connection);
            if (amqConnection != NULL) {
                amqConnection->addTransportListener(this);
            }

            connection->start();

            connection->setExceptionListener(this);

            // Create a Session
            if (clientAck) {
                session = connection->createSession(Session::CLIENT_ACKNOWLEDGE);
            } else {
                session = connection->createSession(Session::AUTO_ACKNOWLEDGE);
            }

            // Create the destination (Topic or Queue)
            if (useTopic) {
                destination = session->createTopic(destURI);
            } else {
                destination = session->createQueue(destURI);
            }

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

        } catch (CMSException& e) {
            e.printStackTrace();
        }
    }

    // Called from the consumer since this class is a registered MessageListener.
    virtual void onMessage(const Message* message) {

        static int count = 0;

        try {
            count++;
            const TextMessage* textMessage = dynamic_cast<const TextMessage*>(message);
            string text = "";

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

            if (clientAck) {
                message->acknowledge();
            }

            printf("Message #%d Received: %s\n", count, text.c_str());
        } catch (CMSException& e) {
            e.printStackTrace();
        }
    }

    // If something bad happens you see it here as this class is also been
    // registered as an ExceptionListener with the connection.
    virtual void onException(const CMSException& ex AMQCPP_UNUSED) {
        printf("CMS Exception occurred.  Shutting down client.\n");
        exit(1);
    }

    virtual void onException(const decaf::lang::Exception& ex) {
        printf("Transport Exception occurred: %s \n", ex.getMessage().c_str());
    }

    virtual void transportInterrupted() {
        std::cout << "The Connection's Transport has been Interrupted." << std::endl;
    }

    virtual void transportResumed() {
        std::cout << "The Connection's Transport has been Restored." << std::endl;
    }

private:

    void cleanup(){

        //*************************************************
        // Always close destination, consumers and producers before
        // you destroy their sessions and connection.
        //*************************************************

        // Destroy resources.
        try{
            if( destination != NULL ) delete destination;
        }catch (CMSException& e) {}
        destination = NULL;

        try{
            if( consumer != NULL ) delete consumer;
        }catch (CMSException& e) {}
        consumer = NULL;

        // Close open resources.
        try{
            if( session != NULL ) session->close();
            if( connection != NULL ) connection->close();
        }catch (CMSException& e) {}

        // Now Destroy them
        try{
            if( session != NULL ) delete session;
        }catch (CMSException& e) {}
        session = NULL;

        try{
            if( connection != NULL ) delete connection;
        }catch (CMSException& e) {}
        connection = NULL;
    }
};

////////////////////////////////////////////////////////////////////////////////
int main(int argc AMQCPP_UNUSED, char* argv[] AMQCPP_UNUSED) {

    activemq::library::ActiveMQCPP::initializeLibrary();

    std::cout << "=====================================================\n";
    std::cout << "Starting the example:" << std::endl;
    std::cout << "-----------------------------------------------------\n";

    // Set the URI to point to the IPAddress of your broker.
    // add any optional params to the url to enable things like
    // tightMarshalling or tcp logging etc.  See the CMS web site for
    // a full list of configuration options.
    //
    //  http://activemq.apache.org/cms/
    //
    // Wire Format Options:
    // =====================
    // Use either stomp or openwire, the default ports are different for each
    //
    // Examples:
    //    tcp://127.0.0.1:61616                      default to openwire
    //    tcp://127.0.0.1:61616?wireFormat=openwire  same as above
    //    tcp://127.0.0.1:61613?wireFormat=stomp     use stomp instead
    //
    std::string brokerURI =
            "failover:(tcp://127.0.0.1:61616"
            //        "?wireFormat=openwire"
            //        "&connection.useAsyncSend=true"
            //        "&transport.commandTracingEnabled=true"
            //        "&transport.tcpTracingEnabled=true"
            //        "&wireFormat.tightEncodingEnabled=true"
            ")";

    //============================================================
    // This is the Destination Name and URI options.  Use this to
    // customize where the consumer listens, to have the consumer
    // use a topic or queue set the 'useTopics' flag.
    //============================================================
    std::string destURI = "TEST.FOO"; //?consumer.prefetchSize=1";

    //============================================================
    // set to true to use topics instead of queues
    // Note in the code above that this causes createTopic or
    // createQueue to be used in the consumer.
    //============================================================
    bool useTopics = false;

    //============================================================
    // set to true if you want the consumer to use client ack mode
    // instead of the default auto ack mode.
    //============================================================
    bool clientAck = false;

    // Create the consumer
    SimpleAsyncConsumer consumer( brokerURI, destURI, useTopics, clientAck );

    // Start it up and it will listen forever.
    consumer.runConsumer();

    // Wait to exit.
    std::cout << "Press 'q' to quit" << std::endl;
    while( std::cin.get() != 'q') {}

    // All CMS resources should be closed before the library is shutdown.
    consumer.close();

    std::cout << "-----------------------------------------------------\n";
    std::cout << "Finished with the example." << std::endl;
    std::cout << "=====================================================\n";

    activemq::library::ActiveMQCPP::shutdownLibrary();
}
  • 編譯運行:
mkdir build
cd build 
cmake ..
make -j4
./ActiveMQ 
=====================================================
Starting the example:
-----------------------------------------------------
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末苏章,一起剝皮案震驚了整個濱河市寂嘉,隨后出現(xiàn)的幾起案子奏瞬,更是在濱河造成了極大的恐慌枫绅,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件硼端,死亡現(xiàn)場離奇詭異并淋,居然都是意外死亡,警方通過查閱死者的電腦和手機珍昨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門县耽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人镣典,你說我怎么就攤上這事兔毙。” “怎么了兄春?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵澎剥,是天一觀的道長。 經(jīng)常有香客問我赶舆,道長哑姚,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任芜茵,我火速辦了婚禮叙量,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘九串。我一直安慰自己绞佩,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著品山,像睡著了一般析既。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上谆奥,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天眼坏,我揣著相機與錄音,去河邊找鬼酸些。 笑死宰译,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的魄懂。 我是一名探鬼主播沿侈,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼市栗!你這毒婦竟也來了缀拭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤填帽,失蹤者是張志新(化名)和其女友劉穎蛛淋,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體篡腌,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡褐荷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了嘹悼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片叛甫。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖杨伙,靈堂內(nèi)的尸體忽然破棺而出其监,到底是詐尸還是另有隱情,我是刑警寧澤限匣,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布抖苦,位于F島的核電站,受9級特大地震影響膛腐,放射性物質(zhì)發(fā)生泄漏睛约。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一哲身、第九天 我趴在偏房一處隱蔽的房頂上張望辩涝。 院中可真熱鬧,春花似錦勘天、人聲如沸怔揩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽商膊。三九已至伏伐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間晕拆,已是汗流浹背藐翎。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留实幕,地道東北人吝镣。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像昆庇,于是被迫代替她去往敵國和親末贾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,955評論 2 355