19. Producer // Create a ConnectionFactory
ConnectionFactory* connectionFactory = ConnectionFactory::createCMSConnectionFactory( brokerURI );
// Create a Connection
connection = connectionFactory->createConnection();
connection->start();
// free the factory, we are done with it.
delete connectionFactory;
// Create a Session
session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
// Create the destination (Queue)
destination = session->createQueue( destURI );
// Create a MessageProducer from the Session to the Topic or Queue
producer = session->createProducer( destination );
producer->setDeliveryMode( DeliveryMode::NON_PERSISTENT );
// Create a messages
string text = (string)"Hello world! “;
for( unsigned int ix=0; ixcreateTextMessage( text );
message->setIntProperty( "Integer", ix );
// Tell the producer to send the message
printf( "Sent message #%d \n", ix+1 );
producer->send( message );
delete message;
}
20. Consumer建立一个类,继承自ExceptionListener,启动消费者 代码
// Create a ConnectionFactory
ConnectionFactory* connectionFactory = ConnectionFactory::createCMSConnectionFactory( brokerURI );
// Create a Connection
connection = connectionFactory->createConnection();
delete connectionFactory;
connection->start();
connection->setExceptionListener(this);
// Create a Session
session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
// Create the destination (Queue)
destination = session->createQueue( destURI );
// Create a MessageConsumer from the Session to the Topic or Queue
consumer = session->createConsumer( destination );
consumer->setMessageListener( this );