什么是ActiveMQ?
ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件;由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行。
支持的编程语言包括:C、C++、C#、Delphi、Erlang、Adobe Flash、Haskell、Java、JavaScript、Perl、PHP、Pike、Python和Ruby。
支持的消息类型
主要有Queues和Topics两种消息类型。Queues是一对一的模式,一个生产者对应一个消费者,消息被消费了就没有了。 Topics是多对对的模式,多个生产者,多个消费者。
本地安装ActiveMQ服务
下载地址
有经典版和高性能版。
Linux可以用curl下载
curl -O https://downloads.apache.org//activemq/5.16.0/apache-activemq-5.16.0-bin.tar.gz
curl 网址 会将请求到的内容打印到控制台上,所以要加 -O 参数
启动
下载好后,解压,双击bin下的win64下的activemq.bat启动服务
linux下:
启动:/home/yuan/ActiveMQ/apache-activemq-5.16.0/bin/linux-x86-64/activemq start
停止:/home/yuan/ActiveMQ/apache-activemq-5.16.0/bin/linux-x86-64/activemq stop
可以使用
ps -elf|grep active
查看是否启动成功
ActiveMQ是java程序需要jre才能启动
可用浏览器登录ActiveMQ的管理端,可以查看消息和连接等情况。默认账号和密码都是admin 可以在/conf/users.properties中寻找
http://127.0.0.1:8161/admin
配置文件activemq.xml
修改配置文件,实现消息的恢复,当消费者客户端重启后能恢复历史消息
C++实现连接ActiveMQ
ActiveMQ服务起来后,现在就是实现消息的生产者和消费者了
下载activemq-cpp对应库代码并编译
可以阅读目录下的README.txt查看依赖
下载activemq-cpp
https://activemq.apache.org/components/cms/
下载cppunit
https://sourceforge.net/projects/cppunit/files/cppunit/1.12.1/
下载apr
https://downloads.apache.org//apr/
http://mirrors.hust.edu.cn/apache/apr/apr-1.6.5.tar.gz
https://downloads.apache.org/apr/apr-iconv-1.2.2.tar.gz
下载openssl
https://www.openssl.org/source/openssl-1.0.0a.tar.gz
编译openssl
解压tar xzvf openssl-1.0.0a.tar.gz
进入文件夹cd openssl-1.0.0a
./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
编译apr
./configure --prefix=/usr/local/apr/
make
make install
编译apr-util
./configure --prefix=/usr/local/aprutil --with-apr=/usr/local/apr/
make
make install
编译apr-iconv
./configure --prefix=/usr/local/apr-iconv/ --with-apr=/usr/local/apr/
make
make install
编译ActiveMQ-CPP
./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
若在make时报错,提示找不到ssl库
/usr/bin/ld: cannot find -lssl
进入到 /usr/local/openssl目录,将lib64目录复制一份,复制的新目录名为lib,命令:cp -r lib64 lib,这是由于make时使用的 /usr/local/openssl/lib路径导致
测试程序
至此编译工作完成,在/usr/local目录下生成了6个目录,分别为ActiveMQ-CPP、apr、apr-iconv、aprutil、cppunit、openssl。
下面编写一段测试代码(main.cpp),用于检测cms开发库是否可用。
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.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::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
using namespace std;
int main()
{
//初始化MQ
activemq::library::ActiveMQCPP::initializeLibrary();
Connection *conn;
Session *sess;
Destination *dest;
MessageProducer *producer;
std::string brokerurl("failover:(tcp://127.0.0.1:61616)");
try
{
auto_ptr<ConnectionFactory> connFactory(ConnectionFactory::createCMSConnectionFactory(brokerurl));
conn = connFactory->createConnection();
conn->start();
sess = conn->createSession(Session::AUTO_ACKNOWLEDGE);
dest = sess->createQueue("QueueFromLinuxTest");
producer = sess->createProducer(dest);
producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);
string text("Hello ActiveMQ from LinuxTest");
for (int i = 0; i < 3; ++i)
{
TextMessage *msg = sess->createTextMessage(text);
msg->setIntProperty("IntProp1", i);
producer->send(msg);
cout << "SEND-> " << text << endl;
delete msg;
}
}
catch (CMSException &e)
{
e.printStackTrace();
}
try
{
delete dest;
dest = NULL;
delete producer;
producer = NULL;
if (NULL != sess)
sess->close();
if (NULL != conn)
conn->close();
delete sess;
sess = NULL;
delete conn;
conn = NULL;
}
catch (CMSException &e)
{
e.printStackTrace();
}
cout << "test end" << endl;
activemq::library::ActiveMQCPP::shutdownLibrary();
}
编译命令:
g++ main.cpp -I/usr/local/ActiveMQ-CPP/include/activemq-cpp-3.9.5 -I/usr/local/apr/include/apr-1 -L/usr/local/ActiveMQ-CPP/lib -lactivemq-cpp -o ActiveDemo
其中-I指定了两个include目录,-L指定了一个库目录,-l指定了一个链接库
运行demo
export LD_LIBRARY_PATH=/usr/local/ActiveMQ-CPP/lib
./ActiveDemo