Pub/Sub messaging example

A Pub/Sub messaging example consists of the basic steps described in this topic.

Creating a Pub/Sub session procedure

In the following example, the application creates a session object by calling pubsubsession.p persistently.

DEFINE VARIABLE hPTPSession AS HANDLE NO-UNDO.
RUN jms/pubsubsession.p PERSISTENT 
  SET hPubSubSession ("-H localhost -S 5162 ").

Creating the session object specifies the connection parameters to the JMS-provider Broker. This allows an application to set different session-level attributes before starting the JMS session. The connection to the JMS-provider Broker and the JMS session does not occur until the application calls the beginSession procedure.

Connecting to the broker

The OpenEdge application connects to the JMS-provider Broker to begin exchanging messages, as follows:

RUN setBrokerURL IN hPubSubSession ("tcp://machinename:2506").
RUN beginSession IN hPubSubSession.

Creating a Message Subscriber

You create a message subscriber to receive the message from the topic newtopic. The subscriber handles the message using the internal procedure myintproc, as shown in the following example.

/* Receives requests from the newTopic */
RUN createMessageConsumer IN hPubSubSession 
  (THIS-PROCEDURE, "myintproc", OUTPUT hConsumer).
Note: Topics can be configured at run time.

Subscribing to a topic

Applications subscribe to topics of interest. To subscribe to a topic, the application subscribes to a topic and prepares to receive messages from the topic, as shown in the following example.

/* Subscribes to newtopic */
RUN SUBSCRIBE IN hPubSubSession ("newTopic", ?, ?, NO, hConsumer). 
/* Start receiving requests */
RUN startReceiveMessages IN hPubSubSession.
/* Wait to receive the messages. */
WAIT-FOR u1 OF THIS-PROCEDURE.

Publishing to a topic

An application uses the publish procedure to publish messages to a topic, as shown in the following example.

DEFINE VARIABLE hMessage AS HANDLE NO-UNDO.
/* Code to create message */
RUN publish IN hPubSubSession ("newTopic", hMessage, ?, ?, ?).

Consuming messages from a topic

The Message Consumer receives a message from the topic and executes the business logic.

Deleting a message

The application deletes the messages after it finishes using them, as shown in the following example.

RUN deleteMessage IN hMessage.

Pub/Sub messaging example summary

To see the full sample code for sending and receiving a message using a publisher/subscriber model, see Pub/sub messaging example summary.