c++ ActiveMQ

在C++中使用ActiveMQ,你可以使用activemq-cpp库,这是一个ActiveMQ的C++客户端实现。下面是一个简单的步骤说明,用于在C++项目中设置和使用activemq-cpp

1. 安装activemq-cpp

首先,你需要安装activemq-cpp库。这通常可以通过包管理器(如aptyumvcpkg)或直接从源代码编译来完成。

使用包管理器(例如apt)

如果你使用的是基于Debian的系统(如Ubuntu),你可以使用apt来安装:

sudo apt-get update
sudo apt-get install libactivemq-cpp-dev

注意:不是所有的Linux发行版都提供了activemq-cpp的包,你可能需要查找适合你发行版的包或从源代码编译。

从源代码编译

你可以从ActiveMQ的官方网站或GitHub仓库下载activemq-cpp的源代码,并按照项目中的说明进行编译和安装。

2. 编写C++代码

下面是一个简单的C++代码示例,展示了如何使用activemq-cpp库连接到ActiveMQ broker并发送和接收消息。

#include <activemq/library/ActiveMQCPP.h>  
#include <activemq/core/ActiveMQConnectionFactory.h>  
#include <cms/Connection.h>  
#include <cms/Session.h>  
#include <cms/TextMessage.h>  
#include <cms/Destination.h>  
#include <cms/Producer.h>  
#include <cms/Consumer.h>  
  
using namespace cms;  
using namespace activemq;  
using namespace activemq::core;  
  
int main(int argc, char* argv[]) {  
    activemq::library::ActiveMQCPP::initializeLibrary();  
  
    try {  
        // 创建一个连接工厂  
        std::unique_ptr<ConnectionFactory> connectionFactory(new ActiveMQConnectionFactory("tcp://localhost:61616"));  
        // 创建一个连接  
        std::unique_ptr<Connection> connection(connectionFactory->createConnection());  
        // 启动连接  
        connection->start();  
        // 创建一个会话  
        std::unique_ptr<Session> session(connection->createSession(Session::AUTO_ACKNOWLEDGE));  
        // 创建一个目标(例如一个队列)  
        Destination* destination = session->createQueue("MY.QUEUE");  
  
        // 创建一个生产者并发送消息  
        {  
            std::unique_ptr<MessageProducer> producer(session->createProducer(destination));  
            TextMessage* message = session->createTextMessage("Hello, ActiveMQ!");  
            producer->send(message);  
            delete message;  
        }  
  
        // 创建一个消费者并接收消息  
        {  
            std::unique_ptr<MessageConsumer> consumer(session->createConsumer(destination));  
            Message* receivedMessage = consumer->receive(5000); // 等待5秒以接收消息  
            if (receivedMessage != nullptr) {  
                TextMessage* textMessage = dynamic_cast<TextMessage*>(receivedMessage);  
                if (textMessage != nullptr) {  
                    std::cout << "Received: " << textMessage->getText() << std::endl;  
                }  
                delete receivedMessage;  
            }  
        }  
  
        // 关闭会话和连接  
        session.reset();  
        connection.reset();  
    }  
    catch (const CMSException& e) {  
        e.printStackTrace();  
    }  
  
    activemq::library::ActiveMQCPP::shutdownLibrary();  
  
    return 0;  
}

3. 编译和运行

编译你的C++代码时,你需要链接到activemq-cpp库。这通常可以通过在编译命令中添加-lactivemq-cpp标志来完成。

例如,使用g++编译上述代码:

g++ -o my_activemq_app my_activemq_app.cpp -lactivemq-cpp -lstdc++ -lpthread

然后运行你的程序:

./my_activemq_app

确保ActiveMQ broker正在运行,并且监听在代码中指定的端口(默认为61616)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值