How to Receive a Message from an Apache ActiveMQ Topic with C/C++

W/o Durability

#include <stdlib.h>

#include "apr_general.h"
#include "stomp.h"

void main() {
  apr_initialize();

  apr_pool_t *pool;

  apr_pool_create(&pool, NULL);

  stomp_connection *connection;

  stomp_connect(&connection, "localhost", 61613, pool);

  stomp_frame connect_frame;

  connect_frame.command = "CONNECT";

  connect_frame.headers = NULL;

  stomp_write(connection, &connect_frame, pool);

  stomp_frame *connected_frame; // Skip the CONNECTED frame

  stomp_read(connection, &connected_frame, pool);

  stomp_frame subscribe_frame;

  subscribe_frame.command = "SUBSCRIBE";

  subscribe_frame.headers = apr_hash_make(pool);

  apr_hash_set(subscribe_frame.headers, "destination", APR_HASH_KEY_STRING, "/topic/SampleTopic");

  stomp_write(connection, &subscribe_frame, pool);

  stomp_frame *message_frame;

  stomp_read(connection, &message_frame, pool);

  printf("%s\n", message_frame->body);

  stomp_frame disconnect_frame;

  disconnect_frame.command = "DISCONNECT";

  disconnect_frame.headers = NULL;

  stomp_write(connection, &disconnect_frame, pool);

  stomp_disconnect(&connection);

  apr_pool_destroy(pool);

  apr_terminate();
}

W/ Durability

#include "apr_general.h"
#include "stomp.h"
#include <stdlib.h>

void main() {
  apr_initialize();

  apr_pool_t *pool;

  apr_pool_create(&pool, NULL);

  stomp_connection *connection;

  stomp_connect(&connection, "localhost", 61613, pool);

  stomp_frame connect_frame;

  connect_frame.command = "CONNECT";

  connect_frame.headers = apr_hash_make(pool);

  apr_hash_set(connect_frame.headers, "client-id", APR_HASH_KEY_STRING, "SampleClient");

  stomp_write(connection, &connect_frame, pool);

  stomp_frame *connected_frame; // Skip the CONNECTED frame

  stomp_read(connection, &connected_frame, pool);

  stomp_frame subscribe_frame;

  subscribe_frame.command = "SUBSCRIBE";

  subscribe_frame.headers = apr_hash_make(pool);

  apr_hash_set(subscribe_frame.headers, "activemq.subscriptionName", APR_HASH_KEY_STRING, "SampleSubscription");

  apr_hash_set(subscribe_frame.headers, "destination", APR_HASH_KEY_STRING, "/topic/SampleTopic");

  stomp_write(connection, &subscribe_frame, pool);

  stomp_frame *message_frame;

  stomp_read(connection, &message_frame, pool);

  printf("%s\n", message_frame->body);

  stomp_frame disconnect_frame;

  disconnect_frame.command = "DISCONNECT";

  disconnect_frame.headers = NULL;

  stomp_write(connection, &disconnect_frame, pool);

  stomp_disconnect(&connection);

  apr_pool_destroy(pool);

  apr_terminate();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s