Author: tabish
Date: Tue Nov 25 06:58:57 2008
New Revision: 720509
URL: http://svn.apache.org/viewvc?rev=720509&view=rev
Log:
Updated Producer example with connection retries.
Modified:
activemq/activemq-cpp/trunk/src/examples/producers/SimpleProducer.cpp
Modified: activemq/activemq-cpp/trunk/src/examples/producers/SimpleProducer.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/examples/producers/SimpleProducer.cpp?rev=720509&r1=720508&r2=720509&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/examples/producers/SimpleProducer.cpp
(original)
+++ activemq/activemq-cpp/trunk/src/examples/producers/SimpleProducer.cpp Tue
Nov 25 06:58:57 2008
@@ -31,6 +31,7 @@
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <iostream>
+#include <memory>
using namespace activemq;
using namespace activemq::core;
@@ -54,6 +55,7 @@
unsigned int numMessages;
std::string brokerURI;
std::string destURI;
+ unsigned int connectRetries;
public:
@@ -61,7 +63,8 @@
unsigned int numMessages,
const std::string& destURI,
bool useTopic = false,
- bool clientAck = false ){
+ bool clientAck = false,
+ unsigned int connectRetries = 0 ){
connection = NULL;
session = NULL;
destination = NULL;
@@ -71,24 +74,41 @@
this->brokerURI = brokerURI;
this->destURI = destURI;
this->clientAck = clientAck;
+ this->connectRetries = 0;
}
virtual ~SimpleProducer(){
cleanup();
}
+ void setConnectRetries( unsigned int retries ) {
+ this->connectRetries = retries;
+ }
+
+ unsigned int getConnectRetries() const {
+ return this->connectRetries;
+ }
+
virtual void run() {
try {
// Create a ConnectionFactory
- ActiveMQConnectionFactory* connectionFactory =
- new ActiveMQConnectionFactory( brokerURI );
-
- // Create a Connection
- connection = connectionFactory->createConnection();
- connection->start();
+ auto_ptr<ActiveMQConnectionFactory> connectionFactory(
+ new ActiveMQConnectionFactory( brokerURI ) );
- // free the factory, we are done with it.
- delete connectionFactory;
+ unsigned int retries = this->connectRetries;
+ do{
+ // Create a Connection
+ try{
+ connection = connectionFactory->createConnection();
+ connection->start();
+ } catch( CMSException& e ) {
+ e.printStackTrace();
+
+ if( retries == 0 ) {
+ return;
+ }
+ }
+ } while( retries-- != 0 );
// Create a Session
if( clientAck ) {
@@ -215,8 +235,20 @@
//============================================================
bool useTopics = false;
+ // Pass an integer value to the producer for retry
+ unsigned int connectRetries = 0;
+
+ if( argc > 1 ) {
+ try {
+ connectRetries = decaf::lang::Integer::parseInt( argv[1] );
+ } catch( decaf::lang::exceptions::NumberFormatException& ex ) {
+ connectRetries = 0;
+ }
+ }
+
// Create the producer and run it.
SimpleProducer producer( brokerURI, numMessages, destURI, useTopics );
+ producer.setConnectRetries( connectRetries );
producer.run();
std::cout << "-----------------------------------------------------\n";