1、创建连接工厂
ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.0.135:61613");
2、创建连接
Connection* connection = connectionFactory->createConnection();
connection->start();//开启连接
connection->close();//关闭连接
3、创建会话
Session* session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
1).Session::AUTO_ACKNOWLEDGE
2).Session::CLIENT_ACKNOWLEDGE
3).Session::DUPS_OK_ACKNOWLEDGE
4).Session::SESSION_TRANSACTED
4、指定生产消息目标和消费消息来源的对象
Destination* destination = session->createQueue( "TEST.FOO" );
5(1)、创建生产者
MessageProducer* producer = session->createProducer( destination );
producer->setDeliveryMode( DeliveryMode::NON_PERSISTANT );//发送模式,PERSISTENT和NON_PERSISTENT 两种模式
producer-> setPriority(4);//发送优先级,消息优先级从0-9十个级别,0-4是普通消息,5-9是加急消息。如果不指定优先级,则默认为4。
Producer->setTimeToLive(1000);//消息过期时间
producer->send( message );//发送
5(2)、创建消费者
MessageConsumer* consumer = session->createConsumer( destination );
Message message = consumer->receive();//接收
或
consumer->setMessageListener( this );virtual void onMessage( const Message* message ){ //process message}//接收
6、Message创建
1)、发送
TextMessage* message = session->createTextMessage( text ); // text is a string
producer->send( message );
delete message;
2)、接收
Message *message = consumer->receive();
const TextMessage* textMessage = dynamic_cast< const TextMessage* >( message );
string text = textMessage->getText();
printf( "Received: %s\n", text.c_str() );
delete message;
阅读(1893) | 评论(0) | 转发(0) |