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

发现错误:

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、继续编译

  • 回到之前的步骤4, 我们继续编译activemq-cpp-library:
cd activemq-cpp-library-3.9.5
./configure
  • 发现已经能找到apr:
configure: Apache Portable Runtime (APR) library configuration
checking for APR... yes
checking APR version... 1.7.0
  • 继续编译安装:
 make -j4 && make install 
  • 发现链接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'
  • 以上未定义的函数都是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
  • 发现编译成功了,然后安装,发现安装路径为:
 /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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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(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(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:
-----------------------------------------------------

你可能感兴趣的:(ActiveMQ C++ SDK编译记录)