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
This commit is contained in:
Nathan Christopher Mittler 2006-08-13 23:01:56 +00:00
parent b84c864d85
commit f012622ff4
2 changed files with 448 additions and 243 deletions

View File

@ -0,0 +1,205 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8.00"
Name="examples"
ProjectGUID="{5830D72A-1239-45B7-B94E-04153B2D4EA4}"
RootNamespace="examples"
Keyword="Win32Proj"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="&quot;D:\Program Files\Microsoft Platform SDK\Include&quot;;&quot;E:\Eclipse\ActiveMQ\activemq-cpp\src\main&quot;;&quot;E:\dev\cppunit-1.11.6\include&quot;;&quot;E:\Eclipse\ActiveMQ\activemq-cpp\src\test&quot;"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="2"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="activemq-cpp.lib ws2_32.lib cppunitd.lib rpcrt4.lib"
LinkIncremental="2"
AdditionalLibraryDirectories="&quot;E:\dev\cppunit-1.11.6\lib&quot;;&quot;D:\Program Files\Microsoft Platform SDK\Lib&quot;;&quot;E:\Eclipse\ActiveMQ\activemq-cpp\src\main\Debug&quot;"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="kernel32.lib $(NoInherit)"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\main.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
<File
RelativePath=".\ReadMe.txt"
>
</File>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@ -1,243 +1,243 @@
// START SNIPPET: demo // START SNIPPET: demo
#include <activemq/concurrent/Thread.h> #include <activemq/concurrent/Thread.h>
#include <activemq/concurrent/Runnable.h> #include <activemq/concurrent/Runnable.h>
#include <activemq/core/ActiveMQConnectionFactory.h> #include <activemq/core/ActiveMQConnectionFactory.h>
#include <cms/Connection.h> #include <cms/Connection.h>
#include <cms/Session.h> #include <cms/Session.h>
#include <cms/TextMessage.h> #include <cms/TextMessage.h>
#include <cms/ExceptionListener.h> #include <cms/ExceptionListener.h>
#include <cms/MessageListener.h> #include <cms/MessageListener.h>
#include <stdlib.h> #include <stdlib.h>
using namespace activemq::core; using namespace activemq::core;
using namespace activemq::concurrent; using namespace activemq::concurrent;
using namespace cms; using namespace cms;
using namespace std; using namespace std;
class HelloWorldProducer : public Runnable { class HelloWorldProducer : public Runnable {
private: private:
Connection* connection; Connection* connection;
Session* session; Session* session;
Destination* destination; Destination* destination;
MessageProducer* producer; MessageProducer* producer;
int numMessages; int numMessages;
public: public:
HelloWorldProducer( int numMessages ){ HelloWorldProducer( int numMessages ){
connection = NULL; connection = NULL;
session = NULL; session = NULL;
destination = NULL; destination = NULL;
producer = NULL; producer = NULL;
this->numMessages = numMessages; this->numMessages = numMessages;
} }
virtual ~HelloWorldProducer(){ virtual ~HelloWorldProducer(){
cleanup(); cleanup();
} }
virtual void run() { virtual void run() {
try { try {
// Create a ConnectionFactory // Create a ConnectionFactory
ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61613"); ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61613");
// Create a Connection // Create a Connection
connection = connectionFactory->createConnection(); connection = connectionFactory->createConnection();
connection->start(); connection->start();
// Create a Session // Create a Session
session = connection->createSession( Session::AutoAcknowledge ); session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
// Create the destination (Topic or Queue) // Create the destination (Topic or Queue)
destination = session->createQueue("TEST.FOO"); destination = session->createQueue( "TEST.FOO" );
// Create a MessageProducer from the Session to the Topic or Queue // Create a MessageProducer from the Session to the Topic or Queue
producer = session->createProducer(*destination); producer = session->createProducer( destination );
producer->setDeliveryMode( Message::NONPERSISTANT); producer->setDeliveryMode( DeliveryMode::NON_PERSISTANT );
// Stringify the thread id // Stringify the thread id
char threadIdStr[100]; char threadIdStr[100];
itoa( Thread::getId(), threadIdStr, 10 ); itoa( Thread::getId(), threadIdStr, 10 );
// Create a messages // Create a messages
string text = (string)"Hello world! from thread " + threadIdStr; string text = (string)"Hello world! from thread " + threadIdStr;
for( int ix=0; ix<numMessages; ++ix ){ for( int ix=0; ix<numMessages; ++ix ){
TextMessage* message = session->createTextMessage( text ); TextMessage* message = session->createTextMessage( text );
// Tell the producer to send the message // Tell the producer to send the message
printf( "Sent message from thread %s\n", threadIdStr ); printf( "Sent message from thread %s\n", threadIdStr );
producer->send(*message); producer->send( message );
delete message; delete message;
} }
}catch (CMSException& e) { }catch ( CMSException& e ) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private: private:
void cleanup(){ void cleanup(){
// Close open resources. // Destroy resources.
try{ try{
if( session != NULL ) session->close(); if( destination != NULL ) delete destination;
if( connection != NULL ) connection->close(); }catch ( CMSException& e ) {}
}catch (CMSException& e) {} destination = NULL;
// Destroy resources. try{
try{ if( producer != NULL ) delete producer;
if( destination != NULL ) delete destination; }catch ( CMSException& e ) {}
}catch (CMSException& e) {} producer = NULL;
destination = NULL;
// Close open resources.
try{ try{
if( producer != NULL ) delete producer; if( session != NULL ) session->close();
}catch (CMSException& e) {} if( connection != NULL ) connection->close();
producer = NULL; }catch ( CMSException& e ) {}
try{ try{
if( session != NULL ) delete session; if( session != NULL ) delete session;
}catch (CMSException& e) {} }catch ( CMSException& e ) {}
session = NULL; session = NULL;
try{ try{
if( connection != NULL ) delete connection; if( connection != NULL ) delete connection;
}catch (CMSException& e) {} }catch ( CMSException& e ) {}
connection = NULL; connection = NULL;
} }
}; };
class HelloWorldConsumer : public ExceptionListener, class HelloWorldConsumer : public ExceptionListener,
public MessageListener, public MessageListener,
public Runnable { public Runnable {
private: private:
Connection* connection; Connection* connection;
Session* session; Session* session;
Destination* destination; Destination* destination;
MessageConsumer* consumer; MessageConsumer* consumer;
long waitMillis; long waitMillis;
public: public:
HelloWorldConsumer( long waitMillis ){ HelloWorldConsumer( long waitMillis ){
connection = NULL; connection = NULL;
session = NULL; session = NULL;
destination = NULL; destination = NULL;
consumer = NULL; consumer = NULL;
this->waitMillis = waitMillis; this->waitMillis = waitMillis;
} }
virtual ~HelloWorldConsumer(){ virtual ~HelloWorldConsumer(){
cleanup(); cleanup();
} }
virtual void run() { virtual void run() {
try { try {
// Create a ConnectionFactory // Create a ConnectionFactory
ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61613"); ActiveMQConnectionFactory* connectionFactory =
new ActiveMQConnectionFactory( "tcp://127.0.0.1:61613" );
// Create a Connection
connection = connectionFactory->createConnection(); // Create a Connection
delete connectionFactory; connection = connectionFactory->createConnection();
connection->start(); delete connectionFactory;
connection->start();
connection->setExceptionListener(this);
connection->setExceptionListener(this);
// Create a Session
session = connection->createSession( Session::AutoAcknowledge ); // Create a Session
session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
// Create the destination (Topic or Queue)
destination = session->createQueue("TEST.FOO"); // 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); // Create a MessageConsumer from the Session to the Topic or Queue
consumer = session->createConsumer( destination );
consumer->setMessageListener( this );
consumer->setMessageListener( this );
// Sleep while asynchronous messages come in.
Thread::sleep( waitMillis ); // Sleep while asynchronous messages come in.
Thread::sleep( waitMillis );
} catch (CMSException& e) {
e.printStackTrace(); } catch (CMSException& e) {
} e.printStackTrace();
} }
}
virtual void onMessage( const Message& message ){
virtual void onMessage( const Message* message ){
try
{ try
const TextMessage& textMessage = dynamic_cast<const TextMessage&>(message); {
string text = textMessage.getText(); const TextMessage* textMessage =
printf( "Received: %s\n", text.c_str() ); dynamic_cast< const TextMessage* >( message );
} string text = textMessage->getText();
catch( std::bad_cast& ex ) printf( "Received: %s\n", text.c_str() );
{ } catch (CMSException& e) {
printf( "Received something other than a text Message\n" ); e.printStackTrace();
} }
} }
virtual void onException( const CMSException& ex ) { virtual void onException( const CMSException& ex ) {
printf("JMS Exception occured. Shutting down client.\n"); printf("JMS Exception occured. Shutting down client.\n");
} }
private: private:
void cleanup(){ void cleanup(){
// Close open resources. // Destroy resources.
try{ try{
if( session != NULL ) session->close(); if( destination != NULL ) delete destination;
if( connection != NULL ) connection->close(); }catch (CMSException& e) {}
}catch (CMSException& e) {} destination = NULL;
// Destroy resources. try{
try{ if( consumer != NULL ) delete consumer;
if( destination != NULL ) delete destination; }catch (CMSException& e) {}
}catch (CMSException& e) {} consumer = NULL;
destination = NULL;
// Close open resources.
try{ try{
if( consumer != NULL ) delete consumer; if( session != NULL ) session->close();
}catch (CMSException& e) {} if( connection != NULL ) connection->close();
consumer = NULL; }catch (CMSException& e) {}
try{ try{
if( session != NULL ) delete session; if( session != NULL ) delete session;
}catch (CMSException& e) {} }catch (CMSException& e) {}
session = NULL; session = NULL;
try{ try{
if( connection != NULL ) delete connection; if( connection != NULL ) delete connection;
}catch (CMSException& e) {} }catch (CMSException& e) {}
connection = NULL; connection = NULL;
} }
}; };
void main(int argc, char* argv[]) { void main(int argc, char* argv[]) {
HelloWorldProducer producer( 1000 ); HelloWorldProducer producer( 1000 );
HelloWorldConsumer consumer( 5000 ); HelloWorldConsumer consumer( 5000 );
// Start the consumer thread. // Start the consumer thread.
Thread consumerThread( &consumer ); Thread consumerThread( &consumer );
consumerThread.start(); consumerThread.start();
// Start the producer thread. // Start the producer thread.
Thread producerThread( &producer ); Thread producerThread( &producer );
producerThread.start(); producerThread.start();
// Wait for the threads to complete. // Wait for the threads to complete.
producerThread.join(); producerThread.join();
consumerThread.join(); consumerThread.join();
} }
// END SNIPPET: demo // END SNIPPET: demo