在介绍ActiveMQ-CP之前,先介绍下CMS(C++ Messaging Service),CMS是C++程序与消息中间件进行通信的一种标准接口,可以通过CMS接口与类似ActiveMQ这样的消息中间件进行通信,它能够让C++程序与消息中间件之间的交互更加优雅和便捷。
ActiceMQ-CPP是CMS的一种实现,是一个能够与ActiveMQ进行通信的C++客户端库,ActiveMQ-CPP的架构设计能够支持可插拨的传输协议和消息封装格式,并且支持客户端容量,能够与ActiveMQ高效和便捷地进行通信,并且提供一系列跨平台的类Java API的特性,如多线程处理、I/O、sockets等。
1. ActiveMQ-CPP安装
ActiveMQ-CPP的编译依赖libuuid、apr、apr-util、apr-iconv、openssl几个库,如果需要跑CppUnit编写的test,还依赖于cppunit,因此在安装使用ActiveMQ-CPP之前,至少得安装libuuid、apr、apr-util、apr-iconv、openssl几个库。
1.1 安装libuuid
util-linux-ng在2.15.1版本以后,已经将libuuid作为一部分包含进来,因此,只需要安装util-linux-ng即可
(1)查询系统是否安装util-linux-ng
(2)若没有安装的话安装正常流程:
下载
解压
指定目录安装:./configure --without-ncurses --prefix=/usr/local/util-linux-ng
编译安装:make
sudo make install
1.2 安装apr
(1)下载apr:http://archive.apache.org/dist/apr/
(2)解压
tar -xf apr-1.4.8.tar.gz
(3)配置apr源码,并指定路径
./configure --prefix=/usr/local/apr
(4)编译安装
make
sudo make install
1.3 安装apr-iconv
(1)下载apr-iconv:http://archive.apache.org/dist/apr/
(2)解压
tar -xf apr-iconv-1.2.1.tar.gz
(3)配置源码,指定路径/usr/local/apr-iconv,并且指定apr的安装
./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
(4)编译安装
make
sudo make install
1.4 安装openssl
在安装openssl的过程中发现系统中已经安装了openssl,查找安装目录:
lib的安装目录很重要,后面安装ActiveMQ-CPP时需要
若系统中没有安装openssl,则按照正常流程安装
(1)下载:openssl-1.0.1e.tar.gz
(2)解压:tar -xf openssl-1.0.1e.tar.gz
(3)配置源码,并指定路径
./configure --prefix=/usr/local/openssl
(4)编译安装
make
sudo make install
1.5 安装ActiveMQ-CPP
(1)下载:http://activemq.apache.org/cms/activemq-cpp-381-release.html
(2)解压
tar -xf activemq-cpp-library-3.8.1-src.tar.gz
(3)配置源码,指定apr、openssl安装路径,以及ActiveMQ-CPP将要安装的路径
./configure --prefix=/usr/local/activemq-cpp --with-apr=/usr/local/apr/ --with-openssl=/usr/local/openssl
(4)编译安装
make
sudo make install
2. CMS应用程序接口
(1)ConnectionFactory
用户用来创建到JMS提供者的连接的被管对象。JMS客户通过可移植的接口访问连接,这样当下层的实现改变时,代码不需要进行修改。 管理员
在JNDI名字空间中配置连接工厂,这样,JMS客户才能够查找到它们。根据消息类型的不同,用户将使用队列连接工厂,或者主题连接工厂。
#ifndef _CMS_CONNECTIONFACTORY_H_ #define _CMS_CONNECTIONFACTORY_H_ #include <cms/Config.h> #include <cms/CMSException.h> #include <string> namespace cms { class Connection; class ExceptionListener; class MessageTransformer; class CMS_API ConnectionFactory { public: virtual ~ConnectionFactory(); virtual cms::Connection* createConnection() = 0; virtual cms::Connection* createConnection(const std::string& username, const std::string& password) = 0; virtual cms::Connection* createConnection(const std::string& username, const std::string& password, const std::string& clientId) = 0; virtual void setExceptionListener(cms::ExceptionListener* listener) = 0; virtual cms::ExceptionListener* getExceptionListener() const = 0; virtual void setMessageTransformer(cms::MessageTransformer* transformer) = 0; virtual cms::MessageTransformer* getMessageTransformer() const = 0; public: static cms::ConnectionFactory* createCMSConnectionFactory(const std::string& brokerURI); }; } #endif /*_CMS_CONNECTIONFACTORY_H_*/
(2)Connection
连接代表了应用程序和消息服务器之间的通信链路。在获得了连接工厂后,就可以创建一个与JMS提供者的连接。根据不同的连接类型,连接允许用户创建会话,以发送和接收队列和主题到目标。
#ifndef _CMS_CONNECTION_H_ #define _CMS_CONNECTION_H_ #include <cms/Config.h> #include <cms/Startable.h> #include <cms/Stoppable.h> #include <cms/Closeable.h> #include <cms/Session.h> #include <cms/ConnectionMetaData.h> namespace cms { class ExceptionListener; class MessageTransformer; class CMS_API Connection : public Startable, public Stoppable, public Closeable { public: virtual ~Connection(); virtual void close() = 0; virtual const ConnectionMetaData* getMetaData() const = 0; virtual Session* createSession() = 0; virtual Session* createSession(Session::AcknowledgeMode ackMode) = 0; virtual std::string getClientID() const = 0; virtual void setClientID(const std::string& clientID) = 0; virtual ExceptionListener* getExceptionListener() const = 0; virtual void setExceptionListener(ExceptionListener* listener) = 0; virtual void setMessageTransformer(cms::MessageTransformer* transformer) = 0; virtual cms::MessageTransformer* getMessageTransformer() const = 0; }; } #endif /*_CMS_CONNECTION_H_*/
(3)Session
表示一个单线程的上下文,用于发送和接收消息。由于会话是单线程的,所以消息是连续的,就是说消息是按照发送的顺序一个一个接收的。会话的好处是它支持事务。如果用户选择了事务支持,会话上下文将保存一组消息,直到事务被提交才发送这些消息。在提交事务之前,用户可以使用回滚操作取消这些消息。一个会话允许用户创建消息生产者来发送消息,创建消息消费者来接收消息。
namespace cms { class MessageTransformer; class CMS_API Session : public Closeable, public Startable, public Stoppable { public: enum AcknowledgeMode { AUTO_ACKNOWLEDGE, // 使用此确认模式,会话自动进行 DUPS_OK_ACKNOWLEDGE, CLIENT_ACKNOWLEDGE, SESSION_TRANSACTED, INDIVIDUAL_ACKNOWLEDGE }; public: virtual ~Session(); virtual void close() = 0; virtual void commit() = 0; virtual void rollback() = 0; virtual void recover() = 0; virtual MessageConsumer* createConsumer(const Destination* destination) = 0; virtual MessageConsumer* createConsumer(const Destination* destination, const std::string& selector) = 0; virtual MessageConsumer* createConsumer(const Destination* destination, const std::string& selector, bool noLocal) = 0; virtual MessageConsumer* createDurableConsumer(const Topic* destination, const std::string& name, const std::string& selector, bool noLocal = false) = 0; virtual MessageProducer* createProducer(const Destination* destination = NULL) = 0; virtual QueueBrowser* createBrowser(const cms::Queue* queue) = 0; virtual QueueBrowser* createBrowser(const cms::Queue* queue, const std::string& selector) = 0; virtual Queue* createQueue(const std::string& queueName) = 0; virtual Topic* createTopic(const std::string& topicName) = 0; virtual TemporaryQueue* createTemporaryQueue() = 0; virtual TemporaryTopic* createTemporaryTopic() = 0; virtual Message* createMessage() = 0; virtual BytesMessage* createBytesMessage() = 0; virtual BytesMessage* createBytesMessage(const unsigned char* bytes, int bytesSize) = 0; virtual StreamMessage* createStreamMessage() = 0; virtual TextMessage* createTextMessage() = 0; virtual TextMessage* createTextMessage(const std::string& text) = 0; virtual MapMessage* createMapMessage() = 0; virtual AcknowledgeMode getAcknowledgeMode() const = 0; virtual bool isTransacted() const = 0; virtual void unsubscribe(const std::string& name) = 0; virtual void setMessageTransformer(cms::MessageTransformer* transformer) = 0; virtual cms::MessageTransformer* getMessageTransformer() const = 0; }; } #endif /*_CMS_SESSION_H_*/
(4)Destination
目标是一个包装了消息目标标识符的被管对象,消息目标是指消息发布和接收的地点,或者是队列,或者是主题。JMS管理员创建这些对象,然后用户通过JNDI发现它们。和连接工厂一样,管理员可以创建两种类型的目标,点对点模型的队列,以及发布者/订阅者模型的主题。
namespace cms { class CMS_API Destination { public: enum DestinationType { TOPIC, QUEUE, TEMPORARY_TOPIC, TEMPORARY_QUEUE }; public: virtual ~Destination(); virtual DestinationType getDestinationType() const = 0; virtual cms::Destination* clone() const = 0; virtual void copy(const cms::Destination& source) = 0; virtual bool equals(const cms::Destination& other) const = 0; virtual const CMSProperties& getCMSProperties() const = 0; }; }
(5)MessageConsumer
由会话创建的对象,用于接收发送到目标的消息。消费者可以同步地(阻塞模式),或异步(非阻塞)接收队列和主题类型的消息。
namespace cms { class MessageTransformer; class CMS_API MessageConsumer : public Closeable, public Startable, public Stoppable { public: virtual ~MessageConsumer(); virtual Message* receive() = 0; virtual Message* receive(int millisecs) = 0; virtual Message* receiveNoWait() = 0; virtual void setMessageListener(MessageListener* listener) = 0; virtual MessageListener* getMessageListener() const = 0; virtual std::string getMessageSelector() const = 0; virtual void setMessageTransformer(cms::MessageTransformer* transformer) = 0; virtual cms::MessageTransformer* getMessageTransformer() const = 0; virtual void setMessageAvailableListener(cms::MessageAvailableListener* listener) = 0; virtual cms::MessageAvailableListener* getMessageAvailableListener() const = 0; }; }
(6)MessageProducer
由会话创建的对象,用于发送消息到目标。用户可以创建某个目标的发送者,也可以创建一个通用的发送者,在发送消息时指定目标。
namespace cms { class MessageTransformer; class CMS_API MessageProducer : public Closeable { public: virtual ~MessageProducer(); virtual void send(Message* message) = 0; virtual void send(Message* message, AsyncCallback* onComplete) = 0; virtual void send(Message* message, int deliveryMode, int priority, long long timeToLive) = 0; virtual void send(Message* message, int deliveryMode, int priority, long long timeToLive, AsyncCallback* onComplete) = 0; virtual void send(const Destination* destination, Message* message) = 0; virtual void send(const Destination* destination, Message* message, AsyncCallback* onComplete) = 0; virtual void send(const Destination* destination, Message* message, int deliveryMode, int priority, long long timeToLive) = 0; virtual void send(const Destination* destination, Message* message, int deliveryMode, int priority, long long timeToLive, AsyncCallback* onComplete) = 0; virtual void setDeliveryMode(int mode) = 0; virtual int getDeliveryMode() const = 0; virtual void setDisableMessageID(bool value) = 0; virtual bool getDisableMessageID() const = 0; virtual void setDisableMessageTimeStamp(bool value) = 0; virtual bool getDisableMessageTimeStamp() const = 0; virtual void setPriority(int priority) = 0; virtual int getPriority() const = 0; virtual void setTimeToLive(long long time) = 0; virtual long long getTimeToLive() const = 0; virtual void setMessageTransformer(cms::MessageTransformer* transformer) = 0; virtual cms::MessageTransformer* getMessageTransformer() const = 0; }; }
(7)Message
是在消费者和生产者之间传送的对象,也就是说从一个应用程序创送到另一个应用程序。一个消息有三个主要部分:消息头(必须):包含用于识别和为消息寻找路由的操作设置。一组消息属性(可选):包含额外的属性,支持其他提供者和用户的兼容。可以创建定制的字段和过滤器(消息选择器)。一个消息体(可选):允许用户创建五种类型的消息(文本消息,映射消息,字节消息,流消息和对象消息)。消息接口非常灵活,并提供了许多方式来定制消息的内容。
namespace cms { class CMS_API Message { public: static const int DEFAULT_DELIVERY_MODE; static const int DEFAULT_MSG_PRIORITY; static const long long DEFAULT_TIME_TO_LIVE; enum ValueType { NULL_TYPE = 0, BOOLEAN_TYPE = 1, BYTE_TYPE = 2, CHAR_TYPE = 3, SHORT_TYPE = 4, INTEGER_TYPE = 5, LONG_TYPE = 6, DOUBLE_TYPE = 7, FLOAT_TYPE = 8, STRING_TYPE = 9, BYTE_ARRAY_TYPE = 10, UNKNOWN_TYPE = 11 }; public: virtual ~Message(); virtual Message* clone() const = 0; virtual void acknowledge() const = 0; virtual void clearBody() = 0; virtual void clearProperties() = 0; virtual std::vector<std::string> getPropertyNames() const = 0; virtual bool propertyExists(const std::string& name) const = 0; virtual ValueType getPropertyValueType(const std::string& name) const = 0; virtual bool getBooleanProperty(const std::string& name) const = 0; virtual unsigned char getByteProperty(const std::string& name) const = 0; virtual double getDoubleProperty(const std::string& name) const = 0; virtual float getFloatProperty(const std::string& name) const = 0; virtual int getIntProperty(const std::string& name) const = 0; virtual long long getLongProperty(const std::string& name) const = 0; virtual short getShortProperty(const std::string& name) const = 0; virtual std::string getStringProperty(const std::string& name) const = 0; virtual void setBooleanProperty(const std::string& name, bool value) = 0; virtual void setByteProperty(const std::string& name, unsigned char value) = 0; virtual void setDoubleProperty(const std::string& name, double value) = 0; virtual void setFloatProperty(const std::string& name, float value) = 0; virtual void setIntProperty(const std::string& name, int value) = 0; virtual void setLongProperty(const std::string& name, long long value) = 0; virtual void setShortProperty(const std::string& name, short value) = 0; virtual void setStringProperty(const std::string& name, const std::string& value) = 0; virtual std::string getCMSCorrelationID() const = 0; virtual void setCMSCorrelationID(const std::string& correlationId) = 0; virtual int getCMSDeliveryMode() const = 0; virtual void setCMSDeliveryMode(int mode) = 0; virtual const Destination* getCMSDestination() const = 0; virtual void setCMSDestination(const Destination* destination) = 0; virtual long long getCMSExpiration() const = 0; virtual void setCMSExpiration(long long expireTime) = 0; virtual std::string getCMSMessageID() const = 0; virtual void setCMSMessageID(const std::string& id) = 0; virtual int getCMSPriority() const = 0; virtual void setCMSPriority(int priority) = 0; virtual bool getCMSRedelivered() const = 0; virtual void setCMSRedelivered(bool redelivered) = 0; virtual const cms::Destination* getCMSReplyTo() const = 0; virtual void setCMSReplyTo(const cms::Destination* destination) = 0; virtual long long getCMSTimestamp() const = 0; virtual void setCMSTimestamp(long long timeStamp) = 0; virtual std::string getCMSType() const = 0; virtual void setCMSType(const std::string& type) = 0; }; }