Consultor Eletrônico



Kbase 21932: ADM2: PUBLISH / SUBSCRIBE Event Mechanism
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   4/1/2002
SUMMARY:

This Knowledge Base Solution explains the Progress Version 9 PUBLISH / SUBSCRIBE event mechanism that allows communication (named events) between procedures within a Progress session. These can be external procedures, SmartObjects, etc.

The Progress ADM2 implements SmartLinks using the Progress 4GL PUBLISH / SUBSCRIBE event mechanism. It is also possible to publish (or subscribe) to such Progress events as, for example, 'fetchnext' with:

PUBLISH "fetchnext".

EXPLANATION:

The mechanism PUBLISH / SUBSCRIBE is based on PUBLISH and SUBSCRIBE commands. The PUBLISH command allows a procedure (or widget) to fire an event (named event) that will be received by other procedures "interested" (subscribed) in that particular event. Each subscriber contains an internal procedure that Progress runs when the named event occurs. This procedure can receive parameters from the publisher and can have a different implementation for each subscriber.

The following example shows the use of this mechanism. The sample code contains two windows (publisher.w and subscriber.w). The secondary window, subscriber.w, has to be run from the main window publisher.w since the PUBLISH / SUBSCRIBE mechanism only works within the same session.

***PUBLISHER.W:

DEFINE BUTTON BtnDone DEFAULT LABEL "&Done" SIZE 15 BY 1.14 BGCOLOR 8.
DEFINE BUTTON BUTTON-1 LABEL "Publish Event 1" SIZE 18 BY 1.19.
DEFINE BUTTON BUTTON-2 LABEL "Publish Event 2" SIZE 17 BY 1.19.
DEFINE BUTTON BUTTON-5 LABEL "Open Subscriber window" SIZE 26 BY 1.19.
DEFINE BUTTON BUTTON-9 LABEL "Published Events" SIZE 26 BY 1.19.

DEFINE FRAME DEFAULT-FRAME
BtnDone AT ROW 1.95 COL 58
BUTTON-1 AT ROW 3.86 COL 14
BUTTON-2 AT ROW 3.86 COL 36
BUTTON-5 AT ROW 5.29 COL 6
BUTTON-9 AT ROW 5.29 COL 36
" PUBLISHER" VIEW-AS TEXT
SIZE 16 BY 1.19 AT ROW 1.95 COL 24 FONT 6
WITH 1 DOWN NO-BOX KEEP-TAB-ORDER OVERLAY
SIDE-LABELS NO-UNDERLINE THREE-D
AT COL 1 ROW 1 SIZE 73.2 BY 5.57
DEFAULT-BUTTON BtnDone.

ON CHOOSE OF BtnDone IN FRAME DEFAULT-FRAME /* Done */
DO:
APPLY "CLOSE":U TO THIS-PROCEDURE.
END.

ON CHOOSE OF BUTTON-1 IN FRAME DEFAULT-FRAME /* Publish Event 1 */
DO:

PUBLISH "MyEvent" ("Good Morning").

/* PUBLISH "myEvent" ("<param1>"): this command means that this object sends the event "myEvent" with an input parameter <param1>, not to one particular object, but to all the objects interested in receiving this event. */

END.

ON CHOOSE OF BUTTON-2 IN FRAME DEFAULT-FRAME /* Publish Event 2 */
DO:

PUBLISH "MyEvent" ("Good Afternoon").
/* You can publish the same named event with different parameters */

END.

ON CHOOSE OF BUTTON-5 IN FRAME DEFAULT-FRAME /*Open Subscriberwindow*/
DO:
RUN subscriber.w. /* You need to run the subscriber procedure within the same session */
END.

ON CHOOSE OF BUTTON-9 IN FRAME DEFAULT-FRAME /* Display Published Events */
DO:
MESSAGE THIS-PROCEDURE:PUBLISHED-EVENTS VIEW-AS ALERT-BOX.
END.

ENABLE BtnDone BUTTON-1 BUTTON-2 BUTTON-5 BUTTON-9 WITH FRAME DEFAULT-FRAME.
WAIT-FOR CLOSE OF THIS-PROCEDURE.

/********************************************************************/

****SUBSCRIBER.W:

CREATE WIDGET-POOL.
DEFINE VAR C-Win AS WIDGET-HANDLE NO-UNDO.
DEFINE BUTTON BtnDone DEFAULT LABEL "&Done" SIZE 15 BY 1.14 BGCOLOR 8.
DEFINE BUTTON BUTTON-6 LABEL "Unsubscribe" SIZE 15 BY 1.19.
DEFINE BUTTON BUTTON-7 LABEL "Subscribe" SIZE 15 BY 1.14.

DEFINE FRAME DEFAULT-FRAME
BUTTON-7 AT ROW 4.57 COL 14
BUTTON-6 AT ROW 4.57 COL 33
BtnDone AT ROW 4.57 COL 54
" SUBSCRIBER 1" VIEW-AS TEXT
SIZE 22 BY 2.14 AT ROW 1.71 COL 23 FONT 6
WITH 1 DOWN NO-BOX KEEP-TAB-ORDER OVERLAY
SIDE-LABELS NO-UNDERLINE THREE-D
AT COL 1 ROW 1 SIZE 69.8 BY 5.33
DEFAULT-BUTTON BtnDone.

IF SESSION:DISPLAY-TYPE = "GUI":U THEN
CREATE WINDOW C-Win ASSIGN
HIDDEN = YES
TITLE = "SUBSCRIBER"
HEIGHT = 5.33 WIDTH = 69.8
MAX-HEIGHT = 25.19 MAX-WIDTH = 160
VIRTUAL-HEIGHT = 25.19 VIRTUAL-WIDTH = 160
RESIZE = yes SCROLL-BARS = no
STATUS-AREA = no BGCOLOR = ?
FGCOLOR = ? KEEP-FRAME-Z-ORDER = yes
THREE-D = yes MESSAGE-AREA = no
SENSITIVE = yes.
ELSE C-Win = CURRENT-WINDOW.

IF SESSION:DISPLAY-TYPE = "GUI":U AND VALID-HANDLE(C-Win)
THEN C-Win:HIDDEN = no.
ON WINDOW-CLOSE OF C-Win /* SUBSCRIBER */
DO:
APPLY "CLOSE":U TO THIS-PROCEDURE.
RETURN NO-APPLY.
END.

ON CHOOSE OF BtnDone IN FRAME DEFAULT-FRAME /* Done */
DO:
APPLY "CLOSE":U TO THIS-PROCEDURE.
END.

ON CHOOSE OF BUTTON-6 IN FRAME DEFAULT-FRAME /* Unsubscribe */
DO:
UNSUBSCRIBE TO ALL.
/* You can unsubscribe to one particular named event or to all of them (from all or one particular subscriber-handler and in all or one particular publisher-handle) */

END.

ON CHOOSE OF BUTTON-7 IN FRAME DEFAULT-FRAME /* Subscribe */
DO:
SUBSCRIBE TO "MyEvent" ANYWHERE.

/* SUBSCRIBE TO "myEvent" ANYWHERE [RUN-PROCEDURE "miproc"]: This command means that this object is interested in receiving the event "myEvent" and that when this event is received, an internal procedure has to be executed. By default, the name of this internal procedure is the name of the event (MyEvent). The subscriber can specify a different name by using the SUBSCRIBE statement's RUN-PROCEDURE option. */

END.
ASSIGN CURRENT-WINDOW = C-Win
THIS-PROCEDURE:CURRENT-WINDOW = C-Win.

ON CLOSE OF THIS-PROCEDURE
RUN disable_UI.
PAUSE 0 BEFORE-HIDE.

MAIN-BLOCK:
DO ON ERROR UNDO MAIN-BLOCK, LEAVE MAIN-BLOCK
ON END-KEY UNDO MAIN-BLOCK, LEAVE MAIN-BLOCK:
RUN enable_UI.
IF NOT THIS-PROCEDURE:PERSISTENT THEN
WAIT-FOR CLOSE OF THIS-PROCEDURE.
END.

PROCEDURE disable_UI :
IF SESSION:DISPLAY-TYPE = "GUI":U AND VALID-HANDLE(C-Win)
THEN DELETE WIDGET C-Win.
IF THIS-PROCEDURE:PERSISTENT THEN DELETE PROCEDURE THIS-PROCEDURE.
END PROCEDURE.

PROCEDURE enable_UI :
ENABLE BUTTON-7 BUTTON-6 BtnDone
WITH FRAME DEFAULT-FRAME IN WINDOW C-Win.
VIEW C-Win.
END PROCEDURE.

PROCEDURE MyEvent :
DEFINE INPUT PARAMETER var1 AS CHAR.
MESSAGE "message from subscriber:" var1 SOURCE-PROCEDURE:FILE-NAME TARGET-PROCEDURE:FILE-NAME VIEW-AS ALERT-BOX.
END PROCEDURE.

/*******************************************************************/


NOTE: Version 9 PUBLISH 'proc' [ FROM hdl ] is similar to Version 8 RUN notify [ IN hdl ] ( [ INPUT ] 'proc' ).


References to Written Documentation:

Progress Knowledge Base Solutions:
21933, "ADM2: PUBLISH & SUBSCRIBE Example with Browses"
21934, "PUBLISH & SUBSCRIBE with ADM2"

Progress Language Reference Manual: PUBLISH Statement, SUBSCRIBE Statement, UNSUBSCRIBE Statement, PUBLISHED-EVENTS Attribute, SOURCE-PROCEDURE System Handle

Progress Programming Handbook: Chapter 4, Named Events