Author: tabish
Date: Thu Oct 7 20:01:03 2010
New Revision: 1005604
URL: http://svn.apache.org/viewvc?rev=1005604&view=rev
Log:
First working example showing send and receive.
Modified:
activemq/activemq-cpp/trunk/activemq-c/src/examples/main.c
Modified: activemq/activemq-cpp/trunk/activemq-c/src/examples/main.c
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-c/src/examples/main.c?rev=1005604&r1=1005603&r2=1005604&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-c/src/examples/main.c (original)
+++ activemq/activemq-cpp/trunk/activemq-c/src/examples/main.c Thu Oct 7
20:01:03 2010
@@ -16,8 +16,15 @@
*/
#include <cms.h>
+#include <CMS_ConnectionFactory.h>
+#include <CMS_Connection.h>
+#include <CMS_Session.h>
+#include <CMS_Message.h>
+#include <CMS_MessageConsumer.h>
+#include <CMS_MessageProducer.h>
#include <CMS_Destination.h>
+#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
@@ -28,7 +35,83 @@ int main(int argc, char* argv[]) {
printf("Starting the example:\n");
printf("-----------------------------------------------------\n");
+ const char* brokerUri = "failover:(tcp://127.0.0.1:61616)";
+ const char* queueName = "cms.test.c.client.queue";
+ CMS_ConnectionFactory* factory = NULL;
+ CMS_Connection* connection = NULL;
+ CMS_Session* session = NULL;
+ CMS_Destination* destination = NULL;
+ CMS_MessageProducer* producer = NULL;
+ CMS_MessageConsumer* consumer = NULL;
+ CMS_Message* txtMessage = NULL;
+
+ if (createConnectionFactory(&factory, brokerUri, NULL, NULL) !=
CMS_SUCCESS) {
+ printf("Failed to create a Connection Factory\n");
+ exit(1);
+ }
+
+ if (createDefaultConnection(factory, &connection) != CMS_SUCCESS) {
+ printf("Failed to create a Connection\n");
+ exit(1);
+ }
+
+ destroyConnectionFactory(factory);
+
+ if (createDefaultSession(connection, &session) != CMS_SUCCESS) {
+ printf("Failed to create a Session\n");
+ exit(1);
+ }
+
+ if (createDestination(session, CMS_QUEUE, queueName, &destination) !=
CMS_SUCCESS) {
+ printf("Failed to create a Destination\n");
+ exit(1);
+ }
+
+ if (createDefaultConsumer(session, destination, &consumer) != CMS_SUCCESS)
{
+ printf("Failed to create a MessageConsumer\n");
+ exit(1);
+ }
+ if (createProducer(session, destination, &producer) != CMS_SUCCESS) {
+ printf("Failed to create a MessageProducer\n");
+ exit(1);
+ }
+
+ int i = 0;
+ for(; i < 10; ++i) {
+ CMS_Message* message = NULL;
+ createTextMessage(session, &message, "Test Message");
+
+ if (producerSendWithDefaults(producer, message) != CMS_SUCCESS) {
+ printf("Failed to send the Message\n");
+ destroyMessage(message);
+ exit(1);
+ }
+
+ destroyMessage(message);
+ }
+
+ if (startConnection(connection) != CMS_SUCCESS) {
+ printf("Failed to start the Connection\n");
+ exit(1);
+ }
+
+ for(i = 0; i < 10; ++i) {
+ CMS_Message* message = NULL;
+ if (consumerReceiveWithTimeout(consumer, &message, 5000) !=
CMS_SUCCESS) {
+ printf("Timed Receive call terminated abnormally\n");
+ exit(1);
+ }
+
+ printf("Received Message #%d\n", i);
+ destroyMessage(message);
+ }
+
+ destroyProducer(producer);
+ destroyConsumer(consumer);
+ destroyDestination(destination);
+ destroySession(session);
+ destroyConnection(connection);
printf("-----------------------------------------------------\n");
printf("Finished with the example.\n");
@@ -36,5 +119,3 @@ int main(int argc, char* argv[]) {
cms_terminate();
}
-
-// END SNIPPET: demo