Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger/Handle events between programs in different ABAP sessions

Tags:

I have two programs running in separated sessions. I want to send a event from program A and catch this event in program B.

How can I do that ?

like image 746
rayashi Avatar asked Jan 25 '18 12:01

rayashi


1 Answers

Using class-based events is not really an option, as these cannot be used to communicate between user sessions.

There is a mechanism that you can use to send messages between sessions: ABAP Messaging Channels. You can send anything that is either a text string, a byte string or can be serialised in any of the above.

You will need to create such a message channel using the repository browser SE80 (Create > Connectivity > ABAP Messaging Channel) or with the Eclipse ADT (New > ABAP Messaging Channel Application).

In there, you will have to define:

  • The message type (text vs binary)
  • The ABAP programs that are authorised to access the message channel.
  • The scope of the messages (i.e. do you want to send messages between users? or just for the same user? what about between application servers?)

The message channels work through a publish - subscribe mechanism. You will have to use specialised classes to publish to the channel (inside report A) and to read from the channel (inside report B). In order to wait for a message to arrive once you have subscribed, you can use the statement WAIT FOR MESSAGE CHANNELS.

Example code:

 " publishing a message
 CAST if_amc_message_producer_text(
     cl_amc_channel_manager=>create_message_producer(
     i_application_id = 'DEMO_AMC'
     i_channel_id     = '/demo_text'
     i_suppress_echo  = abap_true )
 )->send( i_message = text_message ).

 " subscribing to a channel
 DATA(lo_receiver) = NEW message_receiver( ).
 cl_amc_channel_manager=>create_message_consumer(
     i_application_id = 'DEMO_AMC'
     i_channel_id     = '/demo_text'
 )->start_message_delivery( i_receiver = lo_receiver )

 " waiting for a message
 WAIT FOR MESSAGING CHANNELS 
     UNTIL lo_receiver->text_message IS NOT INITIAL
     UP TO time SECONDS.

If you want to avoid waiting inside your subscriber report B and to do something else in the meanwhile, then you can wrap the WAIT FOR... statement inside a RFC and call this RFC using the aRFC variant. This would allow you to continue doing stuff inside report B while waiting for an event to happen. When this event happens, the aRFC callback method that you defined inside your report when calling the RFC would be executed.

Inside the RFC, you would simply have the subscription part and the WAIT statement plus an assignment of the message itself to an EXPORTING parameter. In your report, you could have something like:

CALL FUNCTION 'ZMY_AMC_WRAPPER' STARTING NEW TASK 'MY_TASK'
    CALLING lo_listener->my_method ON END OF TASK.

" inside your 'listener' class implementation
METHOD my_method.
    DATA lv_message TYPE my_message_type.
    RECEIVE RESULTS FROM FUNCTION 'ZMY_AMC_WRAPPER'
        IMPORTING ev_message = lv_message.
    " do something with the lv_message
ENDMETHOD.
like image 123
Serban Petrescu Avatar answered Sep 23 '22 13:09

Serban Petrescu