From f012622ff410566f6dccd3479ad56b49795080f2 Mon Sep 17 00:00:00 2001 From: Nathan Christopher Mittler Date: Sun, 13 Aug 2006 23:01:56 +0000 Subject: [PATCH] AMQ-874 - updating example on the wiki for basic activemq-cpp usage git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@431268 13f79535-47bb-0310-9956-ffa450edef68 --- activemq-cpp/src/examples/examples.vcproj | 205 +++++++++ activemq-cpp/src/examples/main.cpp | 486 +++++++++++----------- 2 files changed, 448 insertions(+), 243 deletions(-) create mode 100644 activemq-cpp/src/examples/examples.vcproj diff --git a/activemq-cpp/src/examples/examples.vcproj b/activemq-cpp/src/examples/examples.vcproj new file mode 100644 index 0000000000..f05af3603b --- /dev/null +++ b/activemq-cpp/src/examples/examples.vcproj @@ -0,0 +1,205 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/activemq-cpp/src/examples/main.cpp b/activemq-cpp/src/examples/main.cpp index 836f34daf2..f5df48a98c 100644 --- a/activemq-cpp/src/examples/main.cpp +++ b/activemq-cpp/src/examples/main.cpp @@ -1,243 +1,243 @@ - -// START SNIPPET: demo - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace activemq::core; -using namespace activemq::concurrent; -using namespace cms; -using namespace std; - -class HelloWorldProducer : public Runnable { -private: - - Connection* connection; - Session* session; - Destination* destination; - MessageProducer* producer; - int numMessages; - -public: - - HelloWorldProducer( int numMessages ){ - connection = NULL; - session = NULL; - destination = NULL; - producer = NULL; - this->numMessages = numMessages; - } - - virtual ~HelloWorldProducer(){ - cleanup(); - } - - virtual void run() { - try { - // Create a ConnectionFactory - ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61613"); - - // Create a Connection - connection = connectionFactory->createConnection(); - connection->start(); - - // Create a Session - session = connection->createSession( Session::AutoAcknowledge ); - - // Create the destination (Topic or Queue) - destination = session->createQueue("TEST.FOO"); - - // Create a MessageProducer from the Session to the Topic or Queue - producer = session->createProducer(*destination); - producer->setDeliveryMode( Message::NONPERSISTANT); - - // Stringify the thread id - char threadIdStr[100]; - itoa( Thread::getId(), threadIdStr, 10 ); - - // Create a messages - string text = (string)"Hello world! from thread " + threadIdStr; - - for( int ix=0; ixcreateTextMessage( text ); - - // Tell the producer to send the message - printf( "Sent message from thread %s\n", threadIdStr ); - producer->send(*message); - - delete message; - } - - }catch (CMSException& e) { - e.printStackTrace(); - } - } - -private: - - void cleanup(){ - - // Close open resources. - try{ - if( session != NULL ) session->close(); - if( connection != NULL ) connection->close(); - }catch (CMSException& e) {} - - // Destroy resources. - try{ - if( destination != NULL ) delete destination; - }catch (CMSException& e) {} - destination = NULL; - - try{ - if( producer != NULL ) delete producer; - }catch (CMSException& e) {} - producer = NULL; - - try{ - if( session != NULL ) delete session; - }catch (CMSException& e) {} - session = NULL; - - try{ - if( connection != NULL ) delete connection; - }catch (CMSException& e) {} - connection = NULL; - } -}; - -class HelloWorldConsumer : public ExceptionListener, - public MessageListener, - public Runnable { - -private: - - Connection* connection; - Session* session; - Destination* destination; - MessageConsumer* consumer; - long waitMillis; - -public: - - HelloWorldConsumer( long waitMillis ){ - connection = NULL; - session = NULL; - destination = NULL; - consumer = NULL; - this->waitMillis = waitMillis; - } - virtual ~HelloWorldConsumer(){ - cleanup(); - } - - virtual void run() { - - try { - - // Create a ConnectionFactory - ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61613"); - - // Create a Connection - connection = connectionFactory->createConnection(); - delete connectionFactory; - connection->start(); - - connection->setExceptionListener(this); - - // Create a Session - session = connection->createSession( Session::AutoAcknowledge ); - - // Create the destination (Topic or Queue) - destination = session->createQueue("TEST.FOO"); - - // Create a MessageConsumer from the Session to the Topic or Queue - consumer = session->createConsumer(*destination); - - consumer->setMessageListener( this ); - - // Sleep while asynchronous messages come in. - Thread::sleep( waitMillis ); - - } catch (CMSException& e) { - e.printStackTrace(); - } - } - - virtual void onMessage( const Message& message ){ - - try - { - const TextMessage& textMessage = dynamic_cast(message); - string text = textMessage.getText(); - printf( "Received: %s\n", text.c_str() ); - } - catch( std::bad_cast& ex ) - { - printf( "Received something other than a text Message\n" ); - } - } - - virtual void onException( const CMSException& ex ) { - printf("JMS Exception occured. Shutting down client.\n"); - } - -private: - - void cleanup(){ - - // Close open resources. - try{ - if( session != NULL ) session->close(); - if( connection != NULL ) connection->close(); - }catch (CMSException& e) {} - - // Destroy resources. - try{ - if( destination != NULL ) delete destination; - }catch (CMSException& e) {} - destination = NULL; - - try{ - if( consumer != NULL ) delete consumer; - }catch (CMSException& e) {} - consumer = NULL; - - try{ - if( session != NULL ) delete session; - }catch (CMSException& e) {} - session = NULL; - - try{ - if( connection != NULL ) delete connection; - }catch (CMSException& e) {} - connection = NULL; - } -}; - -void main(int argc, char* argv[]) { - - HelloWorldProducer producer( 1000 ); - HelloWorldConsumer consumer( 5000 ); - - // Start the consumer thread. - Thread consumerThread( &consumer ); - consumerThread.start(); - - // Start the producer thread. - Thread producerThread( &producer ); - producerThread.start(); - - // Wait for the threads to complete. - producerThread.join(); - consumerThread.join(); -} - -// END SNIPPET: demo + +// START SNIPPET: demo + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace activemq::core; +using namespace activemq::concurrent; +using namespace cms; +using namespace std; + +class HelloWorldProducer : public Runnable { +private: + + Connection* connection; + Session* session; + Destination* destination; + MessageProducer* producer; + int numMessages; + +public: + + HelloWorldProducer( int numMessages ){ + connection = NULL; + session = NULL; + destination = NULL; + producer = NULL; + this->numMessages = numMessages; + } + + virtual ~HelloWorldProducer(){ + cleanup(); + } + + virtual void run() { + try { + // Create a ConnectionFactory + ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61613"); + + // Create a Connection + connection = connectionFactory->createConnection(); + connection->start(); + + // Create a Session + session = connection->createSession( Session::AUTO_ACKNOWLEDGE ); + + // Create the destination (Topic or Queue) + destination = session->createQueue( "TEST.FOO" ); + + // Create a MessageProducer from the Session to the Topic or Queue + producer = session->createProducer( destination ); + producer->setDeliveryMode( DeliveryMode::NON_PERSISTANT ); + + // Stringify the thread id + char threadIdStr[100]; + itoa( Thread::getId(), threadIdStr, 10 ); + + // Create a messages + string text = (string)"Hello world! from thread " + threadIdStr; + + for( int ix=0; ixcreateTextMessage( text ); + + // Tell the producer to send the message + printf( "Sent message from thread %s\n", threadIdStr ); + producer->send( message ); + + delete message; + } + + }catch ( CMSException& e ) { + e.printStackTrace(); + } + } + +private: + + void cleanup(){ + + // Destroy resources. + try{ + if( destination != NULL ) delete destination; + }catch ( CMSException& e ) {} + destination = NULL; + + try{ + if( producer != NULL ) delete producer; + }catch ( CMSException& e ) {} + producer = NULL; + + // Close open resources. + try{ + if( session != NULL ) session->close(); + if( connection != NULL ) connection->close(); + }catch ( CMSException& e ) {} + + try{ + if( session != NULL ) delete session; + }catch ( CMSException& e ) {} + session = NULL; + + try{ + if( connection != NULL ) delete connection; + }catch ( CMSException& e ) {} + connection = NULL; + } +}; + +class HelloWorldConsumer : public ExceptionListener, + public MessageListener, + public Runnable { + +private: + + Connection* connection; + Session* session; + Destination* destination; + MessageConsumer* consumer; + long waitMillis; + +public: + + HelloWorldConsumer( long waitMillis ){ + connection = NULL; + session = NULL; + destination = NULL; + consumer = NULL; + this->waitMillis = waitMillis; + } + virtual ~HelloWorldConsumer(){ + cleanup(); + } + + virtual void run() { + + try { + + // Create a ConnectionFactory + ActiveMQConnectionFactory* connectionFactory = + new ActiveMQConnectionFactory( "tcp://127.0.0.1:61613" ); + + // 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 (Topic or Queue) + destination = session->createQueue( "TEST.FOO" ); + + // Create a MessageConsumer from the Session to the Topic or Queue + consumer = session->createConsumer( destination ); + + consumer->setMessageListener( this ); + + // Sleep while asynchronous messages come in. + Thread::sleep( waitMillis ); + + } catch (CMSException& e) { + e.printStackTrace(); + } + } + + virtual void onMessage( const Message* message ){ + + try + { + const TextMessage* textMessage = + dynamic_cast< const TextMessage* >( message ); + string text = textMessage->getText(); + printf( "Received: %s\n", text.c_str() ); + } catch (CMSException& e) { + e.printStackTrace(); + } + } + + virtual void onException( const CMSException& ex ) { + printf("JMS Exception occured. Shutting down client.\n"); + } + +private: + + void cleanup(){ + + // Destroy resources. + try{ + if( destination != NULL ) delete destination; + }catch (CMSException& e) {} + destination = NULL; + + try{ + if( consumer != NULL ) delete consumer; + }catch (CMSException& e) {} + consumer = NULL; + + // Close open resources. + try{ + if( session != NULL ) session->close(); + if( connection != NULL ) connection->close(); + }catch (CMSException& e) {} + + try{ + if( session != NULL ) delete session; + }catch (CMSException& e) {} + session = NULL; + + try{ + if( connection != NULL ) delete connection; + }catch (CMSException& e) {} + connection = NULL; + } +}; + +void main(int argc, char* argv[]) { + + HelloWorldProducer producer( 1000 ); + HelloWorldConsumer consumer( 5000 ); + + // Start the consumer thread. + Thread consumerThread( &consumer ); + consumerThread.start(); + + // Start the producer thread. + Thread producerThread( &producer ); + producerThread.start(); + + // Wait for the threads to complete. + producerThread.join(); + consumerThread.join(); +} + +// END SNIPPET: demo