activemq-cpp开发手册

更新时间:2024-04-05 03:18:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Activemq-cpp开发手册

丁靖 2008-05-06

1 引言

1.1 编写目的

快速学习CMS,提高CMS开发效率,提供一个CMS开发参考手册

详细API手册请参考http://activemq.apache.org/cms/api_docs/activemqcpp-2.1/

1.2 功能介绍

Activemq-cpp是一个与ActiveMQ交互通讯的C++ API开发库,为C++开发者提供了一个访问ActiveMQ的接口。

Winkeemq-cpp是一个在Activemq-cpp基础上封装的API库,对一些重复机械的初始化及销毁清除及一些不关心的细节进行了封装,从而简化了编程。

1.3 术语解析

ActiveMQ :开源的消息队列服务器

Broker :消息中介,每个消息队列服务器中至少有一个broker,是消息队列的载体 Destination :消息在broker上的目的地 Queue :消息队列 Topic :主题 Message :消息

Producer :消息产生者 Consumer :消息消费者

Client :客户端,生产者和消费者都在客户端上 Server :Activemq服务器

BrokerUri :客户端访问服务器上broker时的Uri

其它资料请参考http://activemq.apache.org/

2 开发前准备

在开发前必须先安装activemq-cpp及winkeemq-cpp库,具体步骤参考《activemq-cpp安装及使用文档.doc》

3 CMS

3.1 概述

CMS(stands for C++ Messaging Service)是一组C++应用程序接口(C++ API),它提供创建、发送、接收、读取消息的服务。定义了一组和Sun公司和它的合作伙伴设计的CMS API相同的公共应用程序接口和相应语法,使得C++程序能够和其他消息组件进行通信。 CMS是一种与厂商无关的 API,用来访问消息收发系统。它类似于 JDBC (Java Database Connectivity):这里,JDBC 是可以用来访问许多不同关系数据库的 API,而 CMS 则提供同样与厂商无关的访问方法,以访问消息收发服务。CMS 使您能够通过消息收发服务(有时称为消息中介程序或路由器)从一个 CMS 客户机向另一个客户机发送消息。消息是 CMS 中的一种类型对象,由两部分组成:报头和消息主体。报头由路由信息以及有关该消息的元数据组成。消息主体则携带着应用程序的数据或有效负载。根据有效负载的类型来划分,可以将消息分为几种类型,它们分别携带:简单文本 (TextMessage)、可序列化的对象 (ObjectMessage)、属性集合 (MapMessage)、字节流 (BytesMessage)、原始值流 (StreamMessage),还有无有效负载的消息 (Message)。

消息收发系统是异步的,也就是说,CMS 客户机可以发送消息而不必等待回应。比较可知,这完全不同于基于 RPC 的(基于远程过程的)系统,如 EJB 1.1、CORBA 和 Java RMI 的引用实现。在 RPC 中,客户机调用服务器上某个分布式对象的一个方法。在方法调用返回之前,该客户机被阻塞;该客户机在可以执行下一条指令之前,必须等待方法调用结束。在 CMS 中,客户机将消息发送给一个虚拟通道(主题或队列),而其它 CMS 客户机则预订或监听这个虚拟通道。当 CMS 客户机发送消息时,它并不等待回应。它执行发送操作,然后继续执行下一条指令。消息可能最终转发到一个或许多个客户机,这些客户机都不需要作出回应。

CMS的通用接口集合以异步方式发送或接收消息。异步方式接收消息显然是使用间断网络连接的客户 机,诸如移动电话和PDA的最好的选择。另外, CMS采用一种宽松结合方式整合企业系统的方法,其主要的目的就是创建能够使用跨平台数据信息的、可移植的企业级应用程序,而把开发人力解放出来。

CMS消息服务支持两种消息模型:Point-to-Point消息(P2P)和发布订阅消息(Publish Subscribe messaging,简称Pub/Sub)。CMS规范并不要求供应商同时支持这两种消息模型,但开发者应该熟悉这两种消息模型的优势与缺点。 P2P消息模型是在点对点之间传递消息时使用。如果应用程序开发者希望每一条消息都能够被处理,那么应该使用P2P消息模型。与Pub/Sub消息模型不同,P2P消息总是能够被传送到指定的位置。

Pub/Sub模型在一到多的消息广播时使用。如果一定程度的消息传递的不可靠性可以被接受的话,那么应用程序开发者也可以使用Pub/Sub消息模型。换句话说,它适用于所有的消息消费程序并不要求能够收到所有的信息或者消息消费程序并不想接收到任何消息的情

况。

CMS通过允许创建持久订阅来简化时间相关性,即使消息预订者未激活也可以接收到消息。此外,使用 持久订阅还可通过队列提供灵活性和可靠性,而仍然允许消息被发给许多的接收者。 Topic Subscriber topic Subscriber = topicSession.createDurableSubscriber(topic, subscriptionName); Connection对象表示了到两种消息模型中的任一种的消息系统的连接。服务器端和客户机端对象要求管理创建的CMS连接的状态。连接是由 Connection Factory创建的并且通过JNDI查寻定位。

//取得用于 P2P的 QueueConnectionFactory

QueueConnectionFactory = queueConnectionFactory(); Context messaging = new InitialContext(); QueueConnectionFactory

= (QueueConnectionFactory) Messaging.lookup(“QueueConnectionFactory”); //取得用于 pub/sub的 TopicConnectionFactory TopicConnectonFactory topicConnectionFactory; Context messaging = new InitialContext(); topicConnectionFactory

= (TopicConnectionFactory)messaging.lookup(“TopicConnectionFactory”); 注意:用于P2P的代码和用于PublishSubscribe的代码非常相似。 如果session被标记为transactional的话,确认消息就通过确认和校正来自动地处理。如果session没有标记为 transactional,你有三个用于消息确认的选项。 · AUTO_ACKNOWLEDGE session将自动地确认收到一则消息。

· CLIENT_ACKNOWLEDGE 客户端程 序将确认收到一则消息,调用这则消息的确认方法。 · DUPS_OK_ACKNOWLEDGE 这个选项命令session“懒散的”确认消息传递,可以想到,这将导致消息提供者传递的一些复制消息可能会出错。这种确认的方式只应当用于消息消费程序 可以容忍潜在的副本消息存在的情况。 queueSession

=queueConnection.createQueueSession(false,session.AUTO_ACKNOWLEDGE);//P2P topicSession

= topicConnection.createTopicSession(false, session.AUTO_ACKNOWLEDGE); //Pub-Sub 注意:在本例中,一个session目的从连结中创建,非值指出session是non-transactional的,并且 session将自动地确认收到一则消息。

CMS现在有两种传递消息的方式。标记为NON_PERSISTENT的消息最多投递一次,而标记 为PERSISTENT的消息将使用暂存后再转送的机理投递。如果一个CMS服务离线,那么持久性消息不会丢失但是得等到这个服务恢复联机时才会被传递。 所以默认的消息传递方式是非持久性的。即使使用非持久性消息可能降低内务和需要的存储器,并且这种传递方式只有当你不需要接收所有的消息时才使用。

虽然 CMS规范并不需要CMS供应商实现消息的优先级路线,但是它需要递送加快的消息优先于普通级别的消息。CMS定义了从0到9的优先级路线级别,0是最低 的优先级而9则是最高的。更特殊的是0到4是正常优先级的变化幅度,而5到9是加快的优先级的变化幅度。举例来说: topicPublisher.publish (message, DeliveryMode.PERSISTENT, 8, 10000); //Pub-Sub 或 queueSender.send(message, DeliveryMode.PERSISTENT, 8, 10000);//P2P 这个代码片断,有两种消息模型,映射递送方式是持久的,优先级为加快型,生存周期是10000 (以毫秒度量 )。如果生存周期设置为零,这则消息将永远不会过期。当消息需要时间限制否则将使其无效时,设置生存周期是有用的。

CMS定义了五种不同的消息正文格式,以及调用的消息类型,允许你发送并接收以一些不同形式的数据,提供现有消息格式的一些级别的兼容性。 · StreamMessage -- Java原始值的数据流 · MapMessage--一套名称-值对 · TextMessage--一个字符串对象 · ObjectMessage--一个序列化的 Java对象 · BytesMessage--一个未解释字节的数据流

CMS应用程序接口提供用于创建每种类型消息和设置荷载的方法例如,为了在一个队列创建并发送一个 TextMessage实例,你可以使用下列语句:

TextMessage message = queueSession.createTextMessage(); message.setText(textMsg);

以异步方式接收消息,需要创建一个消息监听器然后注册一个或多个使用MessageConsumer的CMS MessageListener接口。会话(主题或队列)负责产生某些消息,这些消息被传送到使用onMessage方法的监听者那里。

Using namespace cms;

class ExampleListener : public MessageListener { //把消息强制转化为TextMessage格式 public void onMessage(Message message) { TextMessage textMsg = null; // 打开并处理这段消息 } }

当我们创建QueueReceiver和TopicSubscriber时,我们传递消息选择器字符串: //P2P QueueReceiver QueueReceiver receiver;

receiver = session.createReceiver(queue, selector); //Pub-Sub TopicSubscriber TopicSubscriber subscriber;

subscriber = session.createSubscriber(topic, selector);

为了启动消息的交付,不论是Pub/Sub还是P2P,都需要调用start方法。 TopicConnection.start(); //pub-sub QueueConnection.start(); //P2P

当一条消息被捕捉时,这条消息做为一条必须被强制转化为适当消息类型的普通Message对象到达。如TextMessage

void onMessage(const Message* message){

TextMessage txtMsg=dynamic_cast(message);

//对 txtMsg做一些处理

}

停止消息的传递,无论是Pub/Sub还是P2P,都调用stop方法。 TopicConnection. stop (); //pub-sub QueueConnection. stop (); //P2P

3.2 接口描述

CMS 支持两种消息类型P2P 和Pub/Sub,分别称作:P2P Domain 和Pub/Sub Domain,这两种接口都继承统一的CMS Parent 接口,CMS 主要接口如下所示:

CMS Parent ConnectionFactory Connection Destination Session MessageProducer MessageConsumer 以下是对这些接口的简单描述:

ConnectionFactory :连接工厂,CMS 用它创建连接 Connection :CMS 客户端到CMS Provider 的连接 Destination :消息的目的地

Session: 一个发送或接收消息的线程

MessageProducer: 由Session 对象创建的用来发送消息的对象 MessageConsumer: 由Session 对象创建的用来接收消息的对象

3.3 CMS消息模型

CMS 消息由以下几部分组成:消息头,属性,消息体。

消息头(Header) - 消息头包含消息的识别信息和路由信息,消息头包含一些标准的属性如:CMSDestination,CMSMessageID 等。 消息头 CMSDestination CMSDeliveryMode CMSExpiration CMSPriority CMSMessageID CMSTimestamp CMSCorrelationID CMSReplyTo CMSType CMSRedelivered 由谁设置 send 或 publish 方法 send 或 publish 方法 send 或 publish 方法 send 或 publish 方法 send 或 publish 方法 send 或 publish 方法 客户 客户 客户 CMS Provider

属性(Properties) - 除了消息头中定义好的标准属性外,CMS 提供一种机制增加新属性

到消息头中,这种新属性包含以下几种: 1. 应用需要用到的属性;

2. 消息头中原有的一些可选属性; 3. CMS Provider 需要用到的属性。 标准的CMS 消息头包含以下属性: CMSDestination --消息发送的目的地

CMSDeliveryMode --传递模式, 有两种模式: PERSISTENT 和NON_PERSISTENT,PERSISTENT 表示该消息一定要被送到目的地,否则会导致应用错误。NON_PERSISTENT 表示偶然丢失该消息是被允许的,这两种模式使开发者可以在消息传递的可靠性和吞吐量之间找到平衡点。

CMSMessageID 唯一识别每个消息的标识,由CMS Provider 产生。 CMSTimestamp 一个消息被提交给CMS Provider 到消息被发出的时间。

CMSCorrelationID 用来连接到另外一个消息,典型的应用是在回复消息中连接到原消息。 CMSReplyTo 提供本消息回复消息的目的地址。

CMSRedelivered 如果一个客户端收到一个设置了CMSRedelivered 属性的消息,则表示可能该客户端曾经在早些时候收到过该消息,但并没有签收(acknowledged)。 CMSType 消息类型的识别符。

CMSExpiration 消息过期时间,等于QueueSender 的send 方法中的timeToLive 值或TopicPublisher 的publish 方法中的timeToLive 值加上发送时刻的GMT 时间值。如果timeToLive值等于零,则CMSExpiration 被设为零,表示该消息永不过期。如果发送后,在消息过期时间之后消息还没有被发送到目的地,则该消息被清除。

CMSPriority 消息优先级,从0-9 十个级别,0-4 是普通消息,5-9 是加急消息。CMS 不要求CMS Provider 严格按照这十个优先级发送消息,但必须保证加急消息要先于普通消息到达。

消息体(Body) - CMS API 定义了4种消息体格式,也叫消息类型,你可以使用不同形式发送接收数据并可以兼容现有的消息格式,下面描述这4种类型: 消息类型 TextMessage MapMessage BytesMessage ObjectMessage Message 消息体 string对象,如xml文件内容 名/值对的集合,名是string对象,值类型可以是c++任何基本类型 字节流 对象类型 没有消息体,只有消息头和属性。 下例演示创建并发送一个TextMessage到一个队列:

TextMessage message = queueSession.createTextMessage(); message.setText(msg_text); // msg_text is a String message.setCMSType(“text”); queueSender.send(message);

下例演示接收消息并转换为合适的消息类型:

Message* m = queueReceiver.receive();

If (m->getCMSType() == “text”){

TextMessage txt=dynamic_cast(m); // do something }else{ }

4 消息生产者客户端

消息生产者产生消息并将消息发送到broker上的队列或主题中。

要使消息生产者生产的消息被消息消费者消费,必须满足两个条件:

生产者和消费者必须连接到同一个Broker, 即BrokerUri中主机名和端口相同 生产者和消费者必须具有相同的destination, 即同一个队列名或主题名

4.1 使用activemq-cpp来创建消息生产者 4.1.1 头文件及名字空间

#include #include #include #include

#include

#include #include #include

using namespace activemq;

using namespace activemq::core; using namespace cms; using namespace std;

4.1.2 创建一个生产者类

class SimpleProducer { private:

Connection* connection; //连接对象 Session* session; //会话

Destination* destination; //消息目的地

MessageProducer* producer; //消息生产者 bool useTopic; //是否采用采用主题模式 bool clientAck; //是否自动确认消息接收 unsigned int numMessages; //生产消息数 std::string brokerURI; //连接borker uri std::string destURI; //队列或主题名

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;

this->numMessages = numMessages; this->useTopic = useTopic; this->brokerURI = brokerURI; this->destURI = destURI; this->clientAck = clientAck;

initialize(); }

virtual ~SimpleProducer(){ cleanup(); }

4.1.3 初始化及销毁

// 初始化 private:

Virtual void initialize(){

try {

// 创建连接工厂

ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory( brokerURI ); // 创建一个到broker的连接

connection = connectionFactory->createConnection(); connection->start();

// 关闭连接工厂

delete connectionFactory; // 创建一个会话

if( clientAck ) {//消息接收后由消费者客户端确认

session= connection->createSession( Session::CLIENT_ACKNOWLEDGE ); } else //消息接收自动确认

session = connection->createSession( Session::AUTO_ACKNOWLEDGE ); }

// 创建一个队列或主题 (Topic or Queue) if( useTopic ) {

destination = session->createTopic( destURI ); } else {

destination = session->createQueue( destURI ); }

// 创建生产者并设定消息传送模式

producer = session->createProducer( destination );

producer->setDeliveryMode( DeliveryMode::NON_PERSISTENT );

}catch ( CMSException& e ) { e.printStackTrace(); } }

// 销毁

void cleanup(){

// Destroy resources. 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; // Close open resources. 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; }

4.1.4 生产一个消息并发送到队列中

public : void send(){

// 消息内容

string text = (string)\

for( std::size_t ix=0; ix

// 创建一个文本类型的消息

TextMessage* message = session->createTextMessage( text ); // 发送消息

printf( \ producer->send( message ); // 释放消息 delete message;

} } };

4.1.5 发送消息主程序

Int main(void){ // broker uri

std::string brokerURI =

\ \

\

// 发送消息数

unsigned int numMessages = 2000; // 消息队列名

std::string destURI = \// 使用队列模式

bool useTopics = false;

//初始化一个消息生产者对象并发送消息

SimpleProducer producer( brokerURI, numMessages, destURI, useTopics );

producer.send(); return 0; }

4.1.6 总结

综上例子可知,每次发送一个消息到消息队列中都需要定义一个生产者类,并完成一个机械的初始化及销毁过程。为了提高软件开发效率,可以模仿生产者类定义一个消息发送者类,封装相关细节,并编译成共享库以供使用,简化编程过程。

4.2 使用winkeemq-cpp来创建消息生产者 4.2.1 头文件及名字空间

#include using namespace winkeemq; using namespace std;

4.2.2 发送消息主程序

int main(int argc, char* argv[]){

// broker uri

std::string brokerURI =

\ \

\ \

\

// 队列名

string mqName=\

// 创建一个消息发送对象(采用队列模式,每次只发一个消息) MessageSender ms(brokerURI,1,false,mqName);

string body=”hello world\\n”; // 创建一个文本消息

TextMessage* msg=dynamic_cast

(ms.createMessag(MessageSender::TEXT_MESSAGE));

// 设定消息体内容 msg->setText(body); // 发送消息

ms.sendMessage(); // 销毁消息

ms.deleteMessage(); }

4.2.3 总结

由上述例子可看出,采用winkeemq-cpp后代码量精简了很多,开发员不需要关心那些机械的初始化细节。要创建一个消息生产者,只需要给定Broker uri, 队列名,消息目的模式,然后调用MessageSender的createMessage()创建一个具体类型的消息,createMessage()的参数是一个在MessageSender中定义的一个无名enum, 指明消息的类型。调用MessageSender的sendMessage()发送消息到broker中,最后销毁消息

5 消息消费者客户端

消息消费者从Broker上的队列或主题中取出消息并做相应的处理。

5.1 使用activemq-cpp来创建消息消息者 5.1.1 头文件及名字空间

#include #include

#include

#include #include #include

#include

#include #include #include #include

using namespace activemq;

using namespace activemq::core; using namespace cms; using namespace std;

5.1.2 创建一个生产者类

class SimpleAsyncConsumer : public ExceptionListener, public MessageListener { private:

Connection* connection; //连接对象 Session* session; //会话

Destination* destination; //消息目的地 MessageConsumer* consumer;; //消息消费者 bool useTopic; //是否采用采用主题模式 bool clientAck; //是否自动确认消息接收 std::string brokerURI; //连接borker uri std::string destURI; //队列或主题名

public:

/./构造函数

SimpleAsyncConsumer( const std::string& brokerURI, const std::string& destURI, bool useTopic = false, bool clientAck = false ) { connection = NULL; session = NULL; destination = NULL; consumer = NULL;

this->useTopic = useTopic; this->brokerURI = brokerURI; this->destURI = destURI;

this->clientAck = clientAck;

initialize ();

}

virtual ~ SimpleAsyncConsumer (){ cleanup(); }

5.1.3 初始化及销毁

private: // 初始化

virtual void initialize(){

try {

// 创建连接工厂

ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory( brokerURI ); // 创建一个到broker的连接

connection = connectionFactory->createConnection(); connection->start();

// 设置连接异常侦听类

connection->setExceptionListener(this);

// 关闭连接工厂

delete connectionFactory; // 创建一个会话

if( clientAck ) {//消息接收后由消费者客户端确认

session= connection->createSession( Session::CLIENT_ACKNOWLEDGE ); } else //消息接收自动确认

session = connection->createSession( Session::AUTO_ACKNOWLEDGE ); }

// 创建一个队列或主题 (Topic or Queue) if( useTopic ) {

destination = session->createTopic( destURI ); } else {

destination = session->createQueue( destURI ); }

// 创建消费者并设定消息接收侦听类

consumer = session->createConsumer( destination ); consumer->setMessageListener( this );

}catch ( CMSException& e ) { e.printStackTrace(); } }

// 销毁

void cleanup(){

// Destroy resources. 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; // Close open resources. 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; }

5.1.4 从消息队列中异步接收消息

如果队列中有消息到来,程序会自动调用onMessage函数,因此只需要在onMessage()中编写对消息的处理。onMessage()函数中的message参数在onMessage()返回后便会自动销毁,可以通过调用Message的clone()方法拷贝自身来扩展其生命周期,clone()返回的是在椎上分配的消息,因此需要手动销毁。

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 = \ }

// 确认已经接收 if( clientAck ) {

message->acknowledge(); }

printf( \ } catch (CMSException& e) { e.printStackTrace(); }

} };

5.1.5 接收消息主程序

Int main(void){ // broker uri

std::string brokerURI =

\

\

\

// 消息队列名

std::string destURI = \// 使用队列模式

bool useTopics = false;

//初始化一个消息消费者对象并侦听消息

SimpleAsyncConsumer consumer( brokerURI, destURI, useTopics, clientAck );

// 侦听接收消息5秒钟,如果有消息到来会自动执行onMessage()对消息处理 Thread::sleep(5000); return 0; }

5.1.6 总结

综合上例及消息生产者例子可知国,两者的初始化及销毁过程非常相似,因此,和消息生产者一样,也可以对消息消费者进行封装并编译成共享库于提高开发效率。

5.2 使用winkeemq-cpp来创建消息消息者 5.2.1 头文件及名字空间

#include #include #include

using namespace winkeemq; using namespace std;

5.2.2 接收消息主程序

int main(int argc, char* argv[]){ std::string brokerURI = \ \

\ \

\

// 消息队列名

std::string destName=\

// 创建一个消息消费者并设置侦听时间为30秒

MessageReceiver mr(brokerURI,1,false,destName,1000*30); mr.waitUnitlReady();

// 侦听消息,并把接收到的消息存入到本地MessageQueue队列中 mr.listen();

// 创建一个消息处理对象 MessageParser mp;

whille (!MessageQueue::instance()->empty()){

// 从本地MessageQueue队列取一个消息并处理

mp.run(); }

// 关闭本地队列

MessageQueue::instance()->shutdown(); // 关闭消息接收者 mr.shutdown(); }

上例中有些关键代码实现将在Winkeemq-cpp介绍章节中给出。

5.2.3 总结

由上例可知,winkeemq-cpp对消息消费者创建销毁流程进行了封装,划分了模块,使整个过程更加清晰,将消息处理独立成一个模块,方便以后的扩展。通过在本地创建一个MessageQueue缓冲队列,如果消息到来onMessage要做的仅是将其插入到缓冲返回,大加速了onMessage的处理进度,提高程序的效率。另外,采用本地缓冲为采用多线程编程提供了基础。

6 消息类型

CMS支持TextMessage, MapMessage, ByteMessage, ObjectMessage四种消息类型,其都继承自cms::Message类型。

以上几个消息类都是抽象类,不能直接实例化。在实际使用中通常以指针形式出现,指向由调用Session类的方法创建的具体实类。

指向具体消息类的指针在消息发送时向上转换成基类指针Message*, 接收又将基类指针强制向下转换成具体的消息类型指针。如

发送时:

TextMessage* txt=session.createTextMessage(); txt->setText(“hello world\\n”);

sender.send(txt); // send参数是Message* 做向上转换 接收时:

virtual void onMessage(const Message* msg){ // 强制向下转换

TextMessage* txt=dynamic_cast(msg); // do something }

6.1 cms::Message 6.1.1 概述

cms::Message是cms中所有消息的抽象基类,包含三部分:消息头,消息体,用户定义属性。 详情参考http://activemq.apache.org/cms/api_docs/activemqcpp-2.1/classcms_1_1_message.html

6.1.2 接口

6.1.2.1 virtual cms::Message::~Message ( ) [inline, virtual] 6.1.2.2 virtual Message* cms::Message::clone () const [pure

virtual]

Clone this message exactly, returns a new instance that the caller is required to delete. Returns:

new copy of this message

6.1.2.3 virtual void cms::Message::acknowledge () const throw

( CMSException ) [pure virtual]

Acknowledges all consumed messages of the session of this consumed message.

All consumed CMS messages support the acknowledge method for use when a client has specified that its CMS session's consumed messages are to be explicitly acknowledged. By invoking acknowledge on a consumed message, a client acknowledges all messages consumed by the session that the message was delivered to.

Calls to acknowledge are ignored for both transacted sessions and sessions specified to use implicit acknowledgement modes.

A client may individually acknowledge each message as it is consumed, or it may choose to acknowledge messages as an application-defined group (which is done by calling acknowledge on the last received message of the group, thereby acknowledging all messages consumed by the session.)

Messages that have been received but not acknowledged may be redelivered.

6.1.2.4 virtual void cms::Message::clearBody () [pure virtual]

Clears out the body of the message.

This does not clear the headers or properties.

6.1.2.5 virtual void cms::Message::clearProperties () [pure

virtual]

Clears out the message body.

Clearing a message's body does not clear its header values or property entries.

If this message body was read-only, calling this method leaves the message body in the same state as an empty body in a newly created message.

6.1.2.6 Virtual std::vector

cms::Message::getPropertyName () const [pure virtual]

Retrieves the propery names.

Returns:

The complete set of property names currently in this message.

6.1.2.7 virtual bool cms::Message::propertyExists ( const

std::string & name) const [pure virtual]

Indicates whether or not a given property exists.

Parameters: name The name of the property to look up.

Returns:

True if the property exists in this message.

6.1.2.8 virtual bool cms::Message::getBooleanProperty (const

std::string & name) const throw ( CMSException ) [pure virtual]

Gets a boolean property.

Parameters: name The name of the property to retrieve.

Returns:

The value for the named property.

Exceptions: CMSException if the property does not exist.

6.1.2.9 virtual unsigned char cms::Message::getByteProperty

( const std::string & name) const throw ( CMSException ) [pure virtual]

Gets a byte property.

Parameters: name The name of the property to retrieve.

Returns:

The value for the named property.

Exceptions: CMSException if the property does not exist.

6.1.2.10 virtual double cms::Message::getDoubleProperty

( const std::string & name) const throw ( CMSException ) [pure virtual]

Gets a double property.

Parameters: name The name of the property to retrieve.

Returns:

The value for the named property.

Exceptions: CMSException if the property does not exist.

6.1.2.11 virtual float cms::Message::getFloatProperty ( const

std::string & name) const throw ( CMSException ) [pure virtual]

Gets a float property.

Parameters: name The name of the property to retrieve.

Returns:

The value for the named property.

Exceptions: CMSException if the property does not exist.

6.1.2.12 virtual int cms::Message::getIntProperty ( const

std::string & name) const throw ( CMSException ) [pure virtual]

Gets a int property.

Parameters: name The name of the property to retrieve.

Returns:

The value for the named property.

Exceptions: CMSException if the property does not exist.

6.1.2.13 virtual long long cms::Message::getLongProperty

( const std::string & name) const throw ( CMSException ) [pure virtual]

Gets a long property.

Parameters: name The name of the property to retrieve.

Returns:

The value for the named property.

Exceptions: CMSException if the property does not exist.

6.1.2.14 virtual short cms::Message::getShortProperty

( const std::string & name) const throw ( CMSException ) [pure virtual]

Gets a short property.

Parameters: name The name of the property to retrieve.

Returns:

The value for the named property.

Exceptions: CMSException if the property does not exist.

6.1.2.15 virtual std::string cms::Message::getStringProperty

( const std::string & name) const throw ( CMSException ) [pure virtual]

Gets a string property.

Parameters: name The name of the property to retrieve.

Returns:

The value for the named property.

Exceptions: CMSException

if the property does not exist.

6.1.2.16 virtual void cms::Message::setBooleanProperty

( const std::string & name, bool value) throw ( CMSException ) [pure virtual]

Sets a boolean property.

Parameters: name The name of the property to retrieve. value The value for the named property.

Exceptions: CMSException

6.1.2.17 virtual void cms::Message::setByteProperty ( const

std::string & name, unsigned char value) throw ( CMSException ) [pure virtual]

Sets a byte property.

Parameters: name The name of the property to retrieve. value The value for the named property.

Exceptions: CMSException

6.1.2.18 virtual void cms::Message::setDoubleProperty

( const std::string & name,double value) throw ( CMSException ) [pure virtual]

Sets a double property.

Parameters: name The name of the property to retrieve. value The value for the named property.

Exceptions: CMSException

6.1.2.19 virtual void cms::Message::setFloatProperty (const

std::string & name,float value)throw ( CMSException ) [pure virtual]

Sets a float property.

Parameters: name The name of the property to retrieve. value The value for the named property.

Exceptions: CMSException

6.1.2.20 virtual void cms::Message::setIntProperty (const

std::string & name, int value)throw ( CMSException ) [pure virtual]

Sets a int property.

Parameters: name The name of the property to retrieve. value The value for the named property.

Exceptions: CMSException

6.1.2.21 virtual void cms::Message::setLongProperty (const

std::string&

name,

long

long

value)throw

( CMSException ) [pure virtual]

Sets a long property.

Parameters: name The name of the property to retrieve. value The value for the named property.

Exceptions: CMSException

6.1.2.22 virtual void cms::Message::setShortProperty (const

std::string& name, short value)throw ( CMSException ) [pure virtual]

Sets a short property.

Parameters: name The name of the property to retrieve. value The value for the named property.

Exceptions: CMSException

6.1.2.23 virtual void cms::Message::setStringProperty (const

std::string& name, const std::string & value)throw ( CMSException ) [pure virtual]

Sets a string property.

Parameters: name The name of the property to retrieve. value The value for the named property.

Exceptions: CMSException

6.1.2.24 virtual std::string

cms::Message::getCMSCorrelationID () const [pure virtual]

Gets the correlation ID for the message.

This method is used to return correlation ID values that are either provider-specific message IDs or application-specific String values.

Returns:

string representation of the correlation Id

6.1.2.25 virtual void cms::Message::setCMSCorrelationID

(const std::string & correlationId ) [pure virtual]

Sets the correlation ID for the message.

A client can use the CMSCorrelationID header field to link one message with another. A typical use is to link a response message with its request message.

CMSCorrelationID can hold one of the following:

* A provider-specific message ID * An application-specific String * A provider-native byte[] value

Since each message sent by a CMS provider is assigned a message ID value, it is convenient to link messages via message ID. All message ID values must start with the 'ID:' prefix.

In some cases, an application (made up of several clients) needs to use an application-specific value for linking messages. For instance, an application may use CMSCorrelationID to hold a value referencing some external information. Application-specified values must not start with the 'ID:' prefix; this is reserved for provider-generated message ID values.

If a provider supports the native concept of correlation ID, a CMS client may need to assign specific CMSCorrelationID values to match those expected by clients that do not use the CMS

API. A byte[] value is used for this purpose. CMS providers without native correlation ID values are not required to support byte[] values. The use of a byte[] value for CMSCorrelationID is non-portable.

Parameters: correlationId - the message ID of a message being referred to.

6.1.2.26 virtual void cms::Message::setCMSDeliveryMode (int

mode

) [pure virtual]

Sets the DeliveryMode for this message.

CMS providers set this field when a message is sent. This method can be used to change the value for a message that has been received.

Parameters: mode - DeliveryMode enumerated value.

6.1.2.27 virtual const

()

Destination* const

[pure

cms::Message::getCMSDestination virtual]

Gets the Destination object for this message.

The CMSDestination header field contains the destination to which the message is being sent.

When a message is sent, this field is ignored. After completion of the send or publish method, the field holds the destination specified by the method.

When a message is received, its CMSDestination value must be equivalent to the value assigned when it was sent.

Returns:

Destination object

ector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.28 virtual void cms::Message::setCMSDestination

(const Destination * destination ) [pure virtual]

Sets the Destination object for this message.

CMS providers set this field when a message is sent. This method can be used to change the value for a message that has been received.

Parameters: destination - Destination Object

ector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.29 virtual long long cms::Message::getCMSExpiration ()

const [pure virtual]

Gets the message's expiration value.

When a message is sent, the CMSExpiration header field is left unassigned. After completion of the send or publish method, it holds the expiration time of the message. This is the sum of the time-to-live value specified by the client and the GMT at the time of the send or publish.

If the time-to-live is specified as zero, CMSExpiration is set to zero to indicate that the message does not expire.

When a message's expiration time is reached, a provider should discard it. The CMS API does not define any form of notification of message expiration.

Clients should not receive messages that have expired; however, the CMS API does not guarantee that this will not happen.

Returns:

the time the message expires, which is the sum of the time-to-live value specified by the client and the GMT at the time of the send

ector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.30 virtual void cms::Message::setCMSExpiration (long

long expireTime ) [pure virtual]

Sets the message's expiration value.

CMS providers set this field when a message is sent. This method can be used to change the value for a message that has been received.

Parameters: expireTime the message's expiration time

ector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.31 virtual std::string cms::Message::getCMSMessageID

() const [pure virtual]

The CMSMessageID header field contains a value that uniquely identifies each message sent by a provider.

When a message is sent, CMSMessageID can be ignored. When the send or publish method returns, it contains a provider-assigned value.

A CMSMessageID is a String value that should function as a unique key for identifying messages in a historical repository. The exact scope of uniqueness is provider-defined. It should at least cover all messages for a specific installation of a provider, where an installation is some connected set of message routers.

All CMSMessageID values must start with the prefix 'ID:'. Uniqueness of message ID values across different providers is not required.

Since message IDs take some effort to create and increase a message's size, some CMS providers may be able to optimize message overhead if they are given a hint that the message ID is not used by an application. By calling the MessageProducer.setDisableMessageID method, a CMS client enables this potential optimization for all messages sent by that message producer. If the CMS provider accepts this hint, these messages must have the message ID set to null; if the provider ignores the hint, the message ID must be set to its normal unique value.

Returns:

provider-assigned message id

ector::stomp::commands::StompMessage< cms::Message >, activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

and

6.1.2.32 virtual void cms::Message::setCMSMessageID

(const std::string & id ) [pure virtual]

Sets the message ID.

CMS providers set this field when a message is sent. This method can be used to change the vaue for a message that has been received.

Parameters: id - the ID of the message

Implemented in activemq::connector::stomp::commands::StompMessage< cms::BytesMessage >, activemq::connector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.33 virtual int cms::Message::getCMSPriority () const

[pure virtual]

Gets the message priority level.

The CMS API defines ten levels of priority value, with 0 as the lowest priority and 9 as the highest. In addition, clients should consider priorities 0-4 as gradations of normal priority and priorities 5-9 as gradations of expedited priority.

The CMS API does not require that a provider strictly implement priority ordering of messages; however, it should do its best to deliver expedited messages ahead of normal messages.

Returns:

priority value

ector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.34 virtual void cms::Message::setCMSPriority

priority) [pure virtual]

(int

Sets the Priority Value for this message.

CMS providers set this field when a message is sent. This method can be used to change the value for a message that has been received.

Parameters: priority - priority value for this message

ector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.35 virtual bool cms::Message::getCMSRedelivered ()

const [pure virtual]

Gets an indication of whether this message is being redelivered.

If a client receives a message with the CMSRedelivered field set, it is likely, but not guaranteed, that this message was delivered earlier but that its receipt was not acknowledged at that time.

Returns:

true if this message is being redelivered

ector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.36 virtual void cms::Message::setCMSRedelivered (bool

redelivered ) [pure virtual]

Specifies whether this message is being redelivered.

This field is set at the time the message is delivered. This method can be used to change the value for a message that has been received.

Parameters: redelivered - boolean redelivered value

ector::stomp::commands::StompMessage< cms::Message >, activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

and

6.1.2.37 virtual const cms::Destination*

cms::Message::getCMSReplyTo () const [pure virtual]

Gets the Destination object to which a reply to this message should be sent.

Returns:

Destination to which to send a response to this message

ector::stomp::commands::StompMessage< cms::Message >, activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

and

6.1.2.38 virtual void cms::Message::setCMSReplyTo (const

cms::Destination * destination) [pure virtual]

Sets the Destination object to which a reply to this message should be sent.

The CMSReplyTo header field contains the destination where a reply to the current message should be sent. If it is null, no reply is expected. The destination may be either a Queue object or a Topic object.

Messages sent with a null CMSReplyTo value may be a notification of some event, or they may just be some data the sender thinks is of interest.

Messages with a CMSReplyTo value typically expect a response. A response is optional; it is up to the client to decide. These messages are called requests. A message sent in response to a request is called a reply.

In some cases a client may wish to match a request it sent earlier with a reply it has just received. The client can use the CMSCorrelationID header field for this purpose.

Parameters: destination - Destination to which to send a response to this message

ector::stomp::commands::StompMessage< cms::Message >, and activemq::connector::stomp::commands::StompMessage< cms::TextMessage >.

6.1.2.39 virtual long long cms::Message::getCMSTimestamp ()

const [pure virtual]

Gets the message timestamp.

The CMSTimestamp header field contains the time a message was handed off to a provider to be sent. It is not the time the message was actually transmitted, because the actual send may occur later due to transactions or other client-side queueing of messages.

When a message is sent, CMSTimestamp is ignored. When the send or publish method returns, it contains a time value somewhere in the interval between the call and the return. The value is in the format of a normal millis time value in the Java programming language.

Since timestamps take some effort to create and increase a message's size, some CMS providers may be able to optimize message overhead if they are given a hint that the timestamp is not used by an application. By calling the MessageProducer.setDisableMessageTimestamp method, a CMS client enables this potential optimization for all messages sent by that message producer. If the CMS provider accepts this hint, these messages must have the timestamp set to zero; if the provider ignores the hint, the timestamp must be set to its normal value.

Returns:

the message timestamp

6.1.2.40 virtual void cms::Message::setCMSTimestamp (long

long timeStamp ) [pure virtual]

Sets the message timestamp.

CMS providers set this field when a message is sent. This method can be used to change the value for a message that has been received.

Parameters: timeStamp - integer time stamp value

virtual std::string cms::Message::getCMSType () const [pure virtual]

Gets the message type identifier supplied by the client when the message was sent.

Returns:

the message type

See also:

setCMSType

6.1.2.41 virtual void cms::Message::setCMSType (const

std::string & type ) [pure virtual]

Sets the message type.

Some CMS providers use a message repository that contains the definitions of messages sent by applications. The CMSType header field may reference a message's definition in the provider's repository.

The CMS API does not define a standard message definition repository, nor does it define a naming policy for the definitions it contains.

Some messaging systems require that a message type definition for each application message be created and that each message specify its type. In order to work with such CMS providers, CMS clients should assign a value to CMSType, whether the application makes use of it or not. This ensures that the field is properly set for those providers that require it.

To ensure portability, CMS clients should use symbolic values for CMSType that can be configured at installation time to the values defined in the current provider's message repository. If string literals are used, they may not be valid type names for some CMS providers.

Parameters: type the message type

See also:

getCMSType

6.2 cms::TextMessage 6.2.1 概述

消息体为字符串的消息类型

详情http://activemq.apache.org/cms/api_docs/activemqcpp-2.1/classcms_1_1_text_message.html

6.2.2 接口

6.2.2.1 virtual cms::TextMessage::~TextMessage ( ) [inline,

virtual]

Member Function Documentation

6.2.2.2 virtual std::string cms::TextMessage::getText ( ) const

throw ( CMSException ) [pure virtual]

Gets the message character buffer.

Returns:

The message character buffer.

Implemented in activemq::connector::openwire::commands::ActiveMQTextMessage, activemq::connector::stomp::commands::TextMessageCommand.

and

6.2.2.3 virtual void cms::TextMessage::setText ( const char *

msg ) throw ( CMSException ) [pure virtual]

Sets the message contents, does not take ownership of the passed char*, but copies it instead.

Parameters: msg The message buffer.

Implemented in activemq::connector::openwire::commands::ActiveMQTextMessage, and activemq::connector::stomp::commands::TextMessageCommand.

6.2.2.4 virtual void cms::TextMessage::setText ( const

std::string& msg)throw ( CMSException ) [pure virtual]

Sets the message contents.

Parameters: msg The message buffer.

Implemented in activemq::connector::openwire::commands::ActiveMQTextMessage, activemq::connector::stomp::commands::TextMessageCommand.

and

6.3 cms::MapMessage 6.3.1 概述

MapMessage的消息体是一个组<键-值>对的集合,其中键类型只能为string, 且不能为空串。MapMessage消息的元素访问是随机顺序的,根据关键值能找到对应的值。接收到的MapMessage是只读的,不能对其修改。

详情http://activemq.apache.org/cms/api_docs/activemqcpp-2.1/classcms_1_1_map_message.html

注意:经过一段时间的测试,MapMessage的接口对C++的支持不好,可以使用基类的接口来模拟实现MapMessage功能,如setString可用基类的setStringProperty代替,其它同理。

6.3.2 接口

6.3.2.1 virtual

virtual]

Member Function Documentation

virtual std::vector< std::string > cms::MapMessage::getMapNames()const [pure virtual]

Returns an Enumeration of all the names in the MapMessage object.

Returns:

STL Vector of String values, each of which is the name of an item in the MapMessage

Exceptions: CMSException

cms::MapMessage::~MapMessage() [inline,

6.3.2.2 virtual bool cms::MapMessage::itemExists(const

std::string& name) const [pure virtual]

Indicates whether an item exists in this MapMessage object.

Parameters: name - String name of the Object in question

Returns:

boolean value indicating if the name is in the map

6.3.2.3 virtual bool cms::MapMessage::getBoolean(const

std::string& name)const throw(CMSException)[pure virtual]

Returns the Boolean value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.4 virtual void cms::MapMessage::setBoolean( const

std::string& name,bool

value)

throw(CMSException) [pure virtual]

Sets a boolean value with the specified name into the Map.

Parameters: name - the name of the boolean value - the boolean value to set in the Map

Exceptions: CMSException

6.3.2.5 virtual unsigned char cms::MapMessage::getByte(const

std::string& name)const throw(CMSException) [pure virtual]

Returns the Byte value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.6 virtual void cms::MapMessage::setByte(const

std::string& name,unsigned char value) throw(CMSException) [pure virtual]

Sets a Byte value with the specified name into the Map.

Parameters: name - the name of the Byte value - the Byte value to set in the Map

Exceptions: CMSException

6.3.2.7 virtual std::vector

cms::MapMessage::getBytes(const std::string& name) const throw(CMSException) [pure virtual]

Returns the Bytes value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions:

CMSException

6.3.2.8 virtual void cms::MapMessage::setBytes(const

std::string& name,const std::vector< unsigned char > & value) throw(CMSException) [pure virtual]

Sets a Bytes value with the specified name into the Map.

Parameters: name - the name of the Bytes value - the Bytes value to set in the Map

Exceptions: CMSException

6.3.2.9 virtual char cms::MapMessage::getChar(const

std::string& name)const throw(CMSException) [pure virtual]

Returns the Char value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.10 virtual void cms::MapMessage::setChar(const

std::string& name, char value)throw(CMSException) [pure virtual]

Sets a Char value with the specified name into the Map.

Parameters: name - the name of the Char

value - the Char value to set in the Map

Exceptions: CMSException

6.3.2.11 virtual double cms::MapMessage::getDouble(const

std::string& name)const throw(CMSException) [pure virtual]

Returns the Double value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.12 virtual void cms::MapMessage::setDouble(const

std::string& name,double value)throw(CMSException) [pure virtual]

Sets a Double value with the specified name into the Map.

Parameters: name - the name of the Double value - the Double value to set in the Map

Exceptions: CMSException

6.3.2.13 virtual float cms::MapMessage::getFloat(const

std::string& name)const throw(CMSException) [pure virtual]

Returns the Float value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.14 virtual void cms::MapMessage::setFloat(const

std::string& name,float value) throw(CMSException) [pure virtual]

Sets a Float value with the specified name into the Map.

Parameters: name - the name of the Float value - the Float value to set in the Map

Exceptions: CMSException

6.3.2.15 virtual int cms::MapMessage::getInt(const

std::string& name) const throw(CMSException) [pure virtual]

Returns the Int value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.16 virtual void cms::MapMessage::setInt(const

throw(CMSException)

std::string& name,int value) [pure virtual]

Sets a Int value with the specified name into the Map.

Parameters: name - the name of the Int value - the Int value to set in the Map

Exceptions: CMSException

6.3.2.17 virtual long long cms::MapMessage::getLong(const

std::string& name)const throw(CMSException) [pure virtual]

Returns the Long value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.18 virtual void cms::MapMessage::setLong(const

std::string& name,long long value)throw(CMSException) [pure virtual]

Sets a Long value with the specified name into the Map.

Parameters: name - the name of the Long value - the Long value to set in the Map

Exceptions: CMSException

6.3.2.19 virtual short cms::MapMessage::getShort(const

std::string& name) const throw(CMSException) [pure virtual]

Returns the Short value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.20 virtual void cms::MapMessage::setShort(const

throw(CMSException)

std::string& name,short value) [pure virtual]

Sets a Short value with the specified name into the Map.

Parameters: name - the name of the Short value - the Short value to set in the Map

Exceptions: CMSException

6.3.2.21 virtual std::string

cms::MapMessage::getString(const std::string& name) const throw(CMSException) [pure virtual]

Returns the String value of the Specified name.

Parameters: name of the value to fetch from the map

Exceptions: CMSException

6.3.2.22 virtual

std::string&

void cms::MapMessage::setString(const

std::string&

value)

name,const

throw(CMSException) [pure virtual]

Sets a String value with the specified name into the Map.

Parameters: name - the name of the String value - the String value to set in the Map

Exceptions: CMSException

6.4 cms::ByteMessage 6.4.1 概述

消息体是字节流的消息,可用于传输二进制数据。 详情参考:

http://activemq.apache.org/cms/api_docs/activemqcpp-2.1/classcms_1_1_bytes_message.html

6.4.2 接口

6.4.2.1 virtual cms::BytesMessage::~BytesMessage() [inline,

virtual]

6.4.2.2 virtual void cms::BytesMessage::setBodyBytes(const

unsigned char * buffer,std::size_t numBytes) throw(CMSException) [pure virtual]

sets the bytes given to the message body. Parameters: Byte Buffer to copy Number of bytes in Buffer to copy Exceptions: CMSException

6.4.2.3 virtual const unsigned

const

char* [pure

cms::BytesMessage::getBodyBytes() virtual]

Gets the bytes that are contained in this message, user should copy this data into a user allocated buffer.

Call getBodyLength to determine the number of bytes to expect. Returns:

const pointer to a byte buffer

6.4.2.4 virtual std::size_t cms::BytesMessage::getBodyLength()

const [pure virtual]

Returns the number of bytes contained in the body of this message. Returns:

number of bytes.

6.4.2.5 virtual void cms::BytesMessage::reset()

throw(cms::CMSException) [pure virtual]

Puts the message body in read-only mode and repositions the stream of bytes to the beginning. Exceptions: CMSException

6.4.2.6 virtual bool cms::BytesMessage::readBoolean()

throw(cms::CMSException) [pure virtual]

Reads a Boolean from the Bytes message stream. Returns:

boolean value from stream Exceptions: CMSException

6.4.2.7 virtual void cms::BytesMessage::writeBoolean(bool

value) throw(cms::CMSException) [pure virtual]

Writes a boolean to the bytes message stream as a 1-byte value.

The value true is written as the value(byte) 1; the value false is written as the value(byte) 0. Parameters: value - boolean to write to the stream Exceptions: CMSException

6.4.2.8 virtual unsigned char cms::BytesMessage::readByte()

throw(cms::CMSException) [pure virtual]

Reads a Byte from the Bytes message stream. Returns:

unsigned char value from stream Exceptions: CMSException

virtual void cms::BytesMessage::writeByte(unsigned char

本文来源:https://www.bwwdw.com/article/5w5r.html

Top