Linux 下的 ActiveMQ C++ 环境搭建与测试

一、下载安装jdk

jdk官网下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

1、解压缩到安装目录下

2、编辑配置文件,配置环境变量

终端执行命令:
vim /etc/profile
添加如下内容:JAVA_HOME根据实际目录来

export JAVA_HOME=/home/ss/software/jdk1.8.0_191
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

终端执行命令:
source /etc/profile

3、查看安装情况

java -version

二、下载安装ActiveMQ

ActiveMQ官网下载地址:http://activemq.apache.org/download.html

1、解压缩,进入根目录下,进入bin文件,在文件里面打开终端,输入如下命令启动ActiveMQ

./activemq start

停止则输入:

./activemq stop

2、查看是否启动:

ps -elf|grep active

3、监控

ActiveMQ默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQ的admin应用。

访问链接形式:http://127.0.0.1:8161/admin/

在浏览器中访问上面的链接(注意:ip修改成activeMq服务端的ip地址),出现登录界面,输入用户名和密码会进入activeMq管理网页页面(用户和密码都是admin),可以从这个网页监控端查看队列名称、生产的消息数目以及消费情况以及是否有消费者正在等待消息的到来。

三、下载安装依赖库及ActiveMQ CPP

参考:https://blog.csdn.net/lgh1700/article/details/51055784

打开http://activemq.apache.org/cms/building.html页面,这里介绍了cms build时用到的依赖库。

1、cppunit

cppunit下载页面:
https://sourceforge.net/projects/cppunit/files/cppunit/
tar解压后,进入目录,编译三部曲,configure、make、make install(install需要root权限):

./configure --prefix=/usr/local/cppunit/
make && make install

执行完后在/usr/local/cppunit/目录下可以看到头文件和库文件。
如果在编译过程出现如下错误:

../../src/cppunit/.libs/libcppunit.so  -Wl,--rpath -Wl,/usr/local/cppunit//lib
../../src/cppunit/.libs/libcppunit.so: undefined reference to `dlopen'
../../src/cppunit/.libs/libcppunit.so: undefined reference to `dlclose'
../../src/cppunit/.libs/libcppunit.so: undefined reference to `dlsym'

命令改为

./configure LDFLAGS='-ldl' --prefix=/usr/local/cppunit/
make && make install

或在Makefile中加入

DFLAGS=-lpthread -lrt -ldl

2、apr

apr的全称为Apache Portable Runtime(Apache可移植运行时),Apache旗下有很多开源软件。
apr介绍页面:
http://apr.apache.org/download.cgi
apr下载地址(与以下的apr-util和apr-iconv下载页面一致,下载时注意区分名称):
http://mirrors.hust.edu.cn/apache/apr/
同上,解压进入目录,三部曲:

./configure --prefix=/usr/local/apr/
make && make install

执行完后在/usr/local/apr/目录下可以看到头文件和库文件。

3、apr-util

apr-util下载地址:
http://mirrors.hust.edu.cn/apache/apr/
解压进入目录编译:

./configure --prefix=/usr/local/aprutil --with-apr=/usr/local/apr/
make && make install

4、apr-iconv

apr-iconv下载地址:
http://mirrors.hust.edu.cn/apache//apr/
解压编译:

./configure --prefix=/usr/local/apr-iconv/ --with-apr=/usr/local/apr/
make && make install

5、openssl

openssl下载地址:
https://www.openssl.org/source/

解压编译:

./config --prefix=/usr/local/openssl/
make && make install

若出现报错
cms.pod around line 457: Expected text after =item, not a number
在root权限下,执行

rm -f /usr/bin/pod2man

然后重新

make install

6、ActiveMQ-CPP

ActiveMQ-CPP下载地址:
http://activemq.apache.org/cms/download.html
解压编译:

./configure --prefix=/usr/local/ActiveMQ-CPP --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/aprutil --with-cppunit=/usr/local/cppunit --with-openssl=/usr/local/openssl
make && make install

至此编译工作完成,在/usr/local目录下生成了6个目录,分别为ActiveMQ-CPP、apr、apr-iconv、aprutil、cppunit、openssl。

四、测试

采用该博客的代码进行测试(代码已搬运到文末):
https://blog.csdn.net/chenxun_2010/article/details/52709277

1、编译生成可执行文件:

g++ producer.cpp -o send -I/usr/local/ActiveMQ-CPP/include/activemq-cpp-3.9.4 -I/usr/local/apr/include/apr-1 -L/usr/local/ActiveMQ-CPP/lib -lactivemq-cppg++ consumer.cpp -o receive -I/usr/local/ActiveMQ-CPP/include/activemq-cpp-3.9.4 -I/usr/local/apr/include/apr-1 -L/usr/local/ActiveMQ-CPP/lib -lactivemq-cpp

2、运行

./send
./receive

3、出错:找不到依赖库

把常用的依赖库添加到动态路径里面重新运行即可:

cat /etc/ld.so.conf
echo "/usr/local/lib" >> /etc/ld.so.conf
echo "/usr/local/apr/lib" >> /etc/ld.so.conf
echo "/usr/local/ActiveMQ-CPP/lib" >> /etc/ld.so.conf
ldconfig

代码如下:producer.cpp和consumer.cpp

producer.cpp:

#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Long.h>
#include <decaf/util/Date.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <activemq/library/ActiveMQCPP.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>
#include <memory>using namespace activemq;
using namespace activemq::core;
using namespace decaf;
using namespace decaf::lang;
using namespace decaf::util;
using namespace decaf::util::concurrent;
using namespace cms;
using namespace std;class SimpleProducer : public Runnable {
private:Connection* connection;Session* session;Destination* destination;MessageProducer* producer;bool useTopic;bool clientAck;unsigned int numMessages;std::string brokerURI;std::string destURI;private:SimpleProducer( const SimpleProducer& );SimpleProducer& operator= ( const SimpleProducer& );public:SimpleProducer( const std::string& brokerURI, unsigned int numMessages,const std::string& destURI, bool useTopic = false, bool clientAck = false ) :connection(NULL),session(NULL),destination(NULL),producer(NULL),useTopic(useTopic),clientAck(clientAck),numMessages(numMessages),brokerURI(brokerURI),destURI(destURI) {}virtual ~SimpleProducer(){cleanup();}void close() {this->cleanup();}virtual void run() {try {//1、创建工厂连接对象,需要制定ip和端口号auto_ptr<ActiveMQConnectionFactory> connectionFactory(new ActiveMQConnectionFactory( brokerURI ) );try{//2、使用连接工厂创建一个连接对象connection = connectionFactory->createConnection();//3、开启连接connection->start();} catch( CMSException& e ) {e.printStackTrace();throw e;}//4、使用连接对象创建会话(session)对象 if( clientAck ) {session = connection->createSession( Session::CLIENT_ACKNOWLEDGE );} else {session = connection->createSession( Session::AUTO_ACKNOWLEDGE );}//5、使用会话对象创建目标对象,包含queue和topic(一对一和一对多)if( useTopic ) {destination = session->createTopic( destURI );} else {destination = session->createQueue( destURI );}//6、使用会话对象创建生产者对象producer = session->createProducer( destination );producer->setDeliveryMode( DeliveryMode::NON_PERSISTENT );string threadIdStr = Long::toString( Thread::currentThread()->getId() );string text = (string)"Hello world! from thread " + threadIdStr;for( unsigned int ix=0; ix<numMessages; ++ix ){//7、使用会话对象创建一个消息对象TextMessage* message = session->createTextMessage( text );message->setIntProperty( "Integer", ix );printf( "Sent message #%d from thread %s\n", ix+1, threadIdStr.c_str() );//8、发送消息producer->send( message );delete message;}}catch ( CMSException& e ) {e.printStackTrace();}}private://9、关闭资源void cleanup(){try{if( destination != NULL ) delete destination;}catch ( CMSException& e ) { e.printStackTrace(); }destination = NULL;try{if( producer != NULL ) delete producer;}catch ( CMSException& e ) { e.printStackTrace(); }producer = NULL;try{if( session != NULL ) session->close();if( connection != NULL ) connection->close();}catch ( CMSException& e ) { e.printStackTrace(); }try{if( session != NULL ) delete session;}catch ( CMSException& e ) { e.printStackTrace(); }session = NULL;try{if( connection != NULL ) delete connection;}catch ( CMSException& e ) { e.printStackTrace(); }connection = NULL;}
};int main(int argc , char* argv[])
{activemq::library::ActiveMQCPP::initializeLibrary();std::cout << "=====================================================\n";std::cout << "Starting produce message:" << std::endl;std::cout << "-----------------------------------------------------\n";std::string brokerURI ="failover://(tcp://127.0.0.1:61616)";unsigned int numMessages = 2000;std::string destURI = "test.chen";bool useTopics = false;SimpleProducer producer( brokerURI, numMessages, destURI, useTopics );producer.run();producer.close();std::cout << "-----------------------------------------------------\n";std::cout << "Finished test" << std::endl;std::cout << "=====================================================\n";activemq::library::ActiveMQCPP::shutdownLibrary();
}

consumer.cpp:

#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 {//1、创建工厂连接对象,需要制定ip和端口号ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory( brokerURI );//2、使用连接工厂创建一个连接对象connection = connectionFactory->createConnection();delete connectionFactory;ActiveMQConnection* amqConnection = dynamic_cast<ActiveMQConnection*>( connection );if( amqConnection != NULL ) {amqConnection->addTransportListener( this );}//3、开启连接connection->start();connection->setExceptionListener(this);//4、使用连接对象创建会话(session)对象        if( clientAck ) {session = connection->createSession( Session::CLIENT_ACKNOWLEDGE );} else {session = connection->createSession( Session::AUTO_ACKNOWLEDGE );}//5、使用会话对象创建目标对象,包含queue和topic(一对一和一对多)if( useTopic ) {destination = session->createTopic( destURI );} else {destination = session->createQueue( destURI );}//6、使用会话对象创建生产者对象consumer = session->createConsumer( destination );//7、向consumer对象中设置一个messageListener对象,用来接收消息consumer->setMessageListener( this );} catch (CMSException& e) {e.printStackTrace();}}//8、程序等待接收用户消息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();}}virtual void onException( const CMSException& ex AMQCPP_UNUSED ) {printf("CMS Exception occurred.  Shutting down client.\n");exit(1);}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://9、关闭资源void cleanup(){try{if( destination != NULL ) delete destination;}catch (CMSException& e) {}destination = NULL;try{if( consumer != NULL ) delete consumer;}catch (CMSException& e) {}consumer = NULL;try{if( session != NULL ) session->close();if( connection != NULL ) connection->close();}catch (CMSException& e) {}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, char* argv[]) {activemq::library::ActiveMQCPP::initializeLibrary();std::cout << "=====================================================\n";std::cout << "Starting the example:" << std::endl;std::cout << "-----------------------------------------------------\n";std::string brokerURI = "failover:(tcp://127.0.0.1:61616)";std::string destURI = "test.chen"; bool useTopics = false;bool clientAck = false;SimpleAsyncConsumer consumer( brokerURI, destURI, useTopics, clientAck );consumer.runConsumer();std::cout << "Press 'q' to quit" << std::endl;while( std::cin.get() != 'q') {}consumer.close();std::cout << "-----------------------------------------------------\n";std::cout << "Finished with the example." << std::endl;std::cout << "=====================================================\n";activemq::library::ActiveMQCPP::shutdownLibrary();
}
}

Linux 下的 ActiveMQ C++ 环境搭建与测试相关推荐

  1. linux 下51单片机开发环境搭建

    linux 下51单片机开发环境搭建(arch linux) 编译sdcc 软件: sdcc 命令:sdcc file.c 得到一堆文件需要( .inx) 命令: packihx file.inx & ...

  2. 阿里云服务器搭建python web环境_《Python入门》Linux 下 Python Web开发环境搭建笔记-阿里云开发者社区...

    之前写过 Windows 7下Python Web开发环境搭建笔记,今天写一下在Linux系统下搭建Python Web的开发测试环境. 我使用的系统是:ubuntu 14.04 server,根据个 ...

  3. Python入门 Linux 下 Python Web开发环境搭建笔记

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 之前写过 ...

  4. 《Python入门》Linux 下 Python Web开发环境搭建笔记

    之前写过 Windows 7下Python Web开发环境搭建笔记,今天写一下在Linux系统下搭建Python Web的开发测试环境. 我使用的系统是:ubuntu 14.04 server,Cen ...

  5. selenium+linux+python,Linux下Python+selenium自动化环境搭建

    selenium介绍 Selenium自动化测试工具,主要是用于web应用程序的自动化UI测试,是目前主流的自动化测试工具,主要特点为开源,跨平台,支持主流的浏览器,支持多种编程语言并且支持支持分布式 ...

  6. linux skype 接口,ubuntu /linux下skype api开发环境搭建

    1.安装Skype: 下载skype for linux,地址:http://www.skype.com/download/skype/linux/choose/ 可以下载一个ubuntu 8.10 ...

  7. 【转载】]基于RedHatEnterpriseLinux V7(RHEL7)下SPEC CPU 2006环境搭建以及测试流程 介绍、安装准备、安装、config文件以及运行脚本介绍...

    https://www.codetd.com/article/1137423 <版权声明:本文为博主原创文章,未经博主允许不得转载> 本次利用SPECCPU2006测试工具来进行Intel ...

  8. 基于RedHatEnterpriseLinux V7(RHEL7)下SPEC CPU 2006环境搭建以及测试流程(之一)——介绍、安装准备、安装、config文件以及运行脚本介绍

    <版权声明:本文为博主原创文章,未经博主允许不得转载> 本次利用SPECCPU2006测试工具来进行Intel CPU Xeon E7-**** v4的测试以及调优,计划在机器I840-G ...

  9. Linux下的ssh实验环境搭建与管理

    实验环境 [size=10.5000pt]1:网桥模式 [size=10.5000pt]2:安装好vmtoos [size=10.5000pt]3:安装好yum [size=10.5000pt]4:安 ...

最新文章

  1. MFC获取文字高宽设置字符间隔
  2. 用python生成词云wordcloud
  3. CSS/Compass修改placeholder的文字样式
  4. leetcode 1720. 解码异或后的数组(位运算)
  5. 使用ASP.NET Core,JavaScript,PostegreSql和ChartJs的动态仪表板Web应用程序
  6. 了解FPS屏幕刷新率
  7. php 并发控制中的独占锁
  8. Linux命令整理-Kali
  9. 靠java_人生靠反省,Java靠泛型
  10. 大脚导入配置选择哪个文件_「干货」图解 IntelliJ IDEA 最常用配置,适合新手
  11. gunicorn: No module named 'fcntl'
  12. autojs和按键精灵哪个好?按键精灵打包开始收费了,是弃坑还是继续杠?
  13. [NOIP2015] 斗地主
  14. 一种用于NDN的安全的链路状态路由协议
  15. 【设计模式05】单例模式
  16. xshell6家用版下载和使用
  17. Python实现孤立森林(IForest)+SVR的组合预测模型
  18. idea关于找不到包的问题,比如:Java:程序包org.springframework.beans.factory.annotation不存在
  19. 多宫格视频是什么软件_抖音上的四/多宫格视频是怎么做出来的?多宫格视频一个接一个播放的制作技巧...
  20. 质子之死:粒子衰变如何推出万有理论

热门文章

  1. Ubuntu环境配置
  2. 10款非Windows免费操作系统推介
  3. 【阿冈评点】超女、我秀、好男和梦想的12项大PK(下)
  4. stm32单片机 北斗GPS 定位 vb上位机显示。 蓝牙主从级通信
  5. python求单次测量的标准偏差
  6. 运营商API接口用途简介
  7. concurrent.futures 并发爬取 wos 的部分专利号和施引专利
  8. python中的类方法(@classmethod)
  9. 程序员都会的五大算法之四(回溯算法),恶补恶补恶补!!!
  10. 博士申请 | 香港科技大学(广州)白云老师招收智能交通方向全奖博士生/RA