OCILIB Tutorials v5.0.0
Advanceddemo/queue_async.c

AQ — async dequeue & named agents

Objectives — by the end of this page you will be able to:

Concepts

Polling wastes round-trips.

Subscribing your dequeues once lets Oracle call your callback when a message arrives — needs the environment's events flag (like all async features):

void on_message(OCI_Dequeue *deq)
{
    OCI_Message *msg = OCI_DequeueGet(deq);        /* the message that woke us */
    OCI_Object *obj = OCI_MessageGetObject(msg);
    printf("got: %s\n", OCI_ObjectGetString(obj, OTEXT("TITLE")));
    OCI_MessageFree(msg);
    OCI_ConnectionCommit(cn);                          /* dequeue is transactional — commit it */
}
OCI_Environment *env = OCI_EnvironmentCreate(err, NULL, OCI_ENV_EVENTS);
/* … queue table/queue started, dequeues created … */

OCI_Agent *c1 = OCI_AgentCreate(con, OTEXT("C1"), NULL);
OCI_DequeueSetConsumer(deq1, OTEXT("C1"));
OCI_DequeueSetNavigation(deq1, OCI_ADN_FIRST_MSG);

OCI_Dequeue *deqs[2] = { deq1, deq2 };
OCI_EnvironmentSetSubscriptionPort(env, 9998);
OCI_DequeueSubscribe(con, deqs, 2, 0, on_message);   /* async! */

/* … later … */
OCI_DequeueUnsubscribe(deq1);
OCI_DequeueUnsubscribe(deq2);

The Agent — named endpoints

An Agent is a named producer/consumer identity (AgentCreate(con, name, addr) / Agent(con, name, addr)):

That's pub/sub: enqueue to a consumer list, each named consumer gets its copy.

The async lifecycle

  1. Events-flagged environment + a subscription port for callbacks.
  2. Create dequeues, set consumer/navigation.
  3. Subscribe a dequeue set to a callback — one registration per set.
  4. Callbacks fire on message arrival — dequeue inside the callback.
  5. Unsubscribe to stop.

Putting it together — the typical sequence

Async dequeue is a callback, not a loop:

  1. Register the callback — OCI_DequeueSubscribe(queue, callback) with your handler signature.
  2. Provide an OCI_Event-style context — the callback fires when a message arrives.
  3. Process in the callback — payload access is the same MsgGet calls.
  4. DequeueUnsubscribe when done — the subscription dies with the connection otherwise.

Hints: callbacks run on an OCILIB notification thread — keep them short and thread-aware; subscribe once per queue, not per message; and async is for consumers — producers still enqueue the synchronous way.

Try it yourself

  1. Subscribe one dequeue; enqueue 3 messages; count callback invocations.
  2. Two named consumers — send targeted messages and verify routing.
  3. Unsubscribe mid-stream and confirm callbacks stop.

Common pitfalls

Recap

Next: change notification — database events on any table.