一天功夫终于知道怎么使用activeMQ的c++接口了。 花费时间最多的是不知道activemqcpp需要在activeMQ的基础上使用。
下面分别讲述activeMQ和activemqcpp的安装和使用。
一。 ActiveMQ的安装
1. 下载 源包: http://activemq.apache.org/download-archives.html
2. tar zxvf apache-activemq-xxx.bin.tar.gz -C /usr/local
3. cd apache-activemq-xxx/bin
4. chmod 755 activemq
5. 启动 ./activemq start
启动后就可以查 看: http://localhost:8161/admin, 默认 用户名和密码都是admin
二。 安装ActiveMQ-CPP
需要较多的依赖。参考 http://blog.youkuaiyun.com/lgh1700/article/details/51055784
(1)cppunit
打开http://activemq.apache.org/cms/building.html页面,这里介绍了cms build时用到的依赖库。
cppunit下载页面:
https://sourceforge.net/projects/cppunit/files/cppunit/1.12.1/
这里选择1.12.1版本,获取到下载地址后,在linux下可以用wget命令直接下载,或者下载完成后传到linux系统中。
tar解压后,进入目录,编译三部曲,configure、make、make install(install需要root权限):
./configure --prefix=/usr/local/cppunit/
make
make install
执行完后在/usr/local/cppunit/目录下可以看到头文件和库文件。
(2)apr
apr的全称为Apache Portable Runtime(Apache可移植运行时),Apache旗下有很多开源软件。
apr介绍页面:
http://apr.apache.org/download.cgi
这里选择最新的APR 1.5.2版本,地址为:
http://mirrors.hust.edu.cn/apache//apr/apr-1.5.2.tar.gz
同上,解压进入目录,三部曲:
./configure --prefix=/usr/local/apr/
make
make install
(3)apr-util
这里选择最新的APR-util 1.5.4版本,下载地址为:
http://mirrors.hust.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
解压编译:
./configure --prefix=/usr/local/aprutil --with-apr=/usr/local/apr/
make
make install
(4)apr-iconv
这里选择最新的APR iconv 1.2.1版本,地址为:
http://mirrors.hust.edu.cn/apache//apr/apr-iconv-1.2.1.tar.gz
解压编译:
./configure --prefix=/usr/local/apr-iconv/ --with-apr=/usr/local/apr/
make
make install
(5)openssl
这里选择openssl 1.0.0a版本,下载地址为:
http://www.openssl.org/source/openssl-1.0.0a.tar.gz
解压编译:
./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 3.9.3版本,下载页面为:
http://activemq.apache.org/cms/activemq-cpp-393-release.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/下有了ActiveMQ-CPP目录。 包含activemqcpp的头文件和lib文件
三。 示例
启动ActiveMQ,然后启动activemqcpp的代码。 再查看http://localhost:8161/admin中的queue、topic等
测试用例使用了别人的:
/*
* 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.
*/
// START SNIPPET: demo
#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>
#include <decaf/util/Random.h>
using namespace activemq::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
using namespace std;
#define QUEUE_NAME "eventQueue"
#define NAME_BYTE_LEN 16
#define USE_PRODUCER 1
class HelloWorldProducer : public ExceptionListener,
public MessageListener,
public Runnable {
private:
CountDownLatch latch;
CountDownLatch doneLatch;
Connection* connection;