All Products
Search
Document Center

ApsaraMQ for RocketMQ:Environment preparation (V1.x.x)

Last Updated:Feb 01, 2024

Before you use the SDK for C++ to connect to ApsaraMQ for RocketMQ to send and receive messages, you must prepare the environment. This topic describes the preparations, instructions, and precautions for using the SDK for C++ V1.x.x.

Before you start, take note of the following items:

  • This topic describes only the environment preparation for the SDK for C++ V1.x.x. If you want to use the SDK for C++ V2.0.0, you can refer to the Environment preparation (V2.X.X) topic or download the SDK for C++ V2.0.0. For information about precautions for using the SDK for C++ V2.0.0, see Environment preparation (V2.X.X).

  • You must go to the ApsaraMQ for RocketMQ console to create the topics and groups that are involved in the code. You can specify custom message tags in your application. For information about how to create a message tag, see Create resources.

  • Applications that use ApsaraMQ for RocketMQ must be deployed on Alibaba Cloud Elastic Compute Service (ECS) instances.

Download Tablestore SDK for C++

The C++ SDK supports Windows and Linux operating systems with identical interfaces. The CentOS 6(RHEL 6) and CentOS 7(RHEL 7) series are supported in Linux. For more information about how to download the C++ SDK, see Release notes.

After the download is complete, decompress the package. The directory structure is as follows:

  • demo/(for Windows only)

    Contains a created Windows C++ demo.

  • example/

    Examples include normal message sending, one-way message sending, ordered message sending, normal message consumption, and ordered message consumption. Linux also includes Makefile for example compilation and management.

  • include/

    This directory contains header files required by your own programs.

  • lib/

    • The following subdirectories of the Linux SDK are the static and dynamic libraries of 64.

      lib-boost-share/
        libonsclient4cpp.so
      lib-boost-static/
        libonsclient4cpp.a                        
    • The Windows SDK subdirectory is as follows, which is the dll library of the SDK in a 64-bit system. If no Visual Studio 2015 environment is installed, copy the installation vc_redist.x64. This is the run time environment for Visual C++ 2015.

      64/
      vc_redist.x64                        
  • SDK_GUIDE.pdf

    Contains the documentation and frequently asked questions (FAQ) about how to prepare the SDK environment.

  • changelog

    Contains bug fixes and new features in the new releases.

Linux C++ SDK

Since December 02, 2016, the Linux CPP version relies on the high-performance boost library (version 1.62.0), which not only reduces the CPU resource utilization rate, but also improves the operation efficiency. Currently, it mainly relies on four libraries: boost_system, boost_thread, boost_chrono, and boost_filesystem. There are two solutions: static library and dynamic library.

Static Solution

The ApsaraMQ for RocketMQ library file is in the lib/lib-boost-static directory. The boost library is statically linked to the libonsclient4cpp.a. For business parties that do not rely on the boost library, you can directly select the static library solution. In the static library solution, the corresponding boost library has been linked to the libonsclient4cpp.a. You only need to link the libonsclient4cpp.a during compilation, and no other operations are required. The following code provides an example on how to set the fields:

cd aliyun-mq-linux-cpp-sdk // The path where the downloaded SDK is decompressed.
cd example // Go to the example directory, modify the example file, and enter the information about the topic and key that you created.
make static=1            
Important

For full static links, make sure that libstdc++, pthread and other related static libraries are installed on the machine. By default, libstdc++ does not have static libraries installed, so you need to install related static libraries through yum or apt-get.

In addition, some warning messages will appear as follows when using the above method.

warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking            

It is recommended that the best way is not to use complete static links, but only to statically link onsclient4cpp, and other libraries can be dynamically linked. The following code provides an example on how to set the fields:

g++ -ggdb -Wall -O3 -I../include    ../example/ProducerExampleForEx.cpp  -Wl,-static -lonsclient4cpp -L../lib/lib-boost-static/ -Wl,-Bdynamic  -lpthread -ldl -lrt -o ../example/ProducerExampleForEx        

In addition, because GCC 5.x introduces Dual ABI, when compiling links, add -D_GLIBCXX_USE_CXX11_ABI=0 compiler options.

Dynamic solutions

The ApsaraMQ for RocketMQ library file is in the lib/lib-boost-share directory, and the business side needs to link the boost dynamic library and libonsclient4cpp.so when generating the executable file. if the business side has already relied on the boost library and needs to select a dynamic library solution, the following work needs to be done to rely on the boost library:

  1. Download Boost 1.62.0.

    boost 1.62.0

  2. Decompress boost 1.62.0.

    tar --bzip2 -xf /path/to/boost_1_62_0.tar.bz2

  3. Install boost version 1.62.0:

    1. Go to the path after boost 1.62.0 decompression: cd path/to/boost_1_62_0

    2. Configure boost: ./bootstrap.sh

    3. Compile boost: ./b2 link=shared runtime-link=shared

    4. Install boost: ./b2 install

  4. The execution ldconfig -v|grep libboost. If there is relevant output, it indicates that the boost dynamic library is in the dynamic library search path.

  5. When generating an executable file, you need to link the boost dynamic library and the ApsaraMQ for RocketMQ dynamic library. The method is as follows.

    cd aliyun-mq-linux-cpp-sdk // The path where the downloaded SDK is decompressed.
    cd example // Go to the example directory, modify the example file, and enter the topics and keys that you created in the Message Queue for Apache RocketMQ console.
    g++ -Wall -Wno-deprecated -L ../lib/lib-boost-share/ -I ../include/ ProducerExampleForEx.cpp  -lonsclient4cpp -lboost_system -lboost_thread -lboost_chrono -lboost_filesystem -lpthread
    export LD_LIBRARY_PATH="../lib/lib-boost-share/"  //Add dynamically loaded search paths.
    ./a.out  //Run the program.

Windows C++ SDK

Use the SDK for C++ in the Visual Studio 2015 environment

  1. Use Visual Studio 2015 to create your project.createprojectnextfinish

  2. Right-click the project and choose Properties > Configuration Manager. Set the Active solution configuration parameter to release and the Active solution platform parameter to x64.x64CM

  3. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > General, click Output Directory, and then select /A. Copy all files in the 64-bit lib directory to the /A directory based on the setting of the Active solution platform parameter.path

  4. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > C/C++ > General, click Additional Include Directories, and then select /B. Copy the header file in the include directory to the /B directory.otherdirectoryadditional

  5. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > Linker > General, click Additional Library Directories, and then select /A.additionalA

  6. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > Linker > Input, click Additional Dependencies, and then select ONSClient4CPP.lib.ONSClient4CPP.lib

  7. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > C/C++ > Preprocessor, and then click Preprocessor Definitions to add the WIN32 macro.WIN32

Use the SDK for C++ in an environment other than Visual Studio 2015

  1. Follow the preceding steps to configure your project based on the Visual Studio 2015 environment.

  2. Install the runtime environment for Visual C++ 2015: vc_redist.x64.

    Important

    To prevent complex settings, you can use the preconfigured SDK for C++ demo. You can download and decompress the SDK for C++ package, go to the demo directory, and then open the project in Visual Studio 2015.

    Sample code:

    demopropertiessetfilepath

    After you perform the preceding operations, the environment is compiled. Then, you can click Build to compile an executable program and copy the DLL file to the directory in which the executable program resides or to the system directory to run the program.

Upgrade SDK for v1.x.x to v2.0.0

Currently, the latest version of SDK v2.0.0 and SDK v1.x.x maintain API forward compatibility (that is, the new version is compatible with the old version). ABI is incompatible due to different implementation methods.

Perform the following steps:

  1. Replace the header file of the new version of the SDK with the header file of the old version.

  2. Copy all the dynamic libraries in the new version of the lib directory to the directory where the original version of the dynamic library is stored.

  3. If the static link scheme was used before, remove the -static parameter and change it to the default dynamic link scheme.

  4. Modify the compilation script, add -lrocketmq_client_core parameters, and link the new kernel dynamic library.

  5. Recompile the project.

  6. If the upgrade fails, you can roll back the project to the pre-upgrade status and recompile it.

References