Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ABAP Channels →  AMC - ABAP Messaging Channels →  AMC Examples 

AMC, Receiving Messages

This example demonstrates how messages are received using AMC.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_receive_amc.

CLASS message_receiver DEFINITION.
  PUBLIC SECTION.
    INTERFACES:
      if_amc_message_receiver_text,
      if_amc_message_receiver_binary,
      if_amc_message_receiver_pcp.
    DATA: text_message   TYPE string,
          binary_message TYPE xstring,
          pcp_message    TYPE REF TO if_ac_message_type_pcp.
ENDCLASS.

CLASS message_receiver IMPLEMENTATION.
  METHOD if_amc_message_receiver_text~receive.
    text_message = i_message.
  ENDMETHOD.
  METHOD if_amc_message_receiver_binary~receive.
    binary_message = i_message.
  ENDMETHOD.
  METHOD if_amc_message_receiver_pcp~receive.
    pcp_message = i_message.
  ENDMETHOD.
ENDCLASS.

CLASS amc_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS amc_demo IMPLEMENTATION.
  METHOD main.
   DATA(in) = cl_demo_input=>new( ).

    TRY.
        in->add_text( `Session id: ` &&
          cl_amc_channel_manager=>get_consumer_session_id( ) ).
      CATCH cx_amc_error INTO DATA(id_exc).
        cl_demo_output=>display( id_exc->get_text( ) ).
    ENDTRY.

    DATA(txt) = ` `.
    in->add_field(
      EXPORTING as_checkbox = 'X'
                text        = 'Wait for text messages'
      CHANGING  field       = txt ).
    DATA(hex) = ` `.
    in->add_field(
      EXPORTING as_checkbox = 'X'
                text        = 'Wait for binary messages'
      CHANGING  field       = hex ).
    DATA(pcp) = ` `.
    in->add_field(
      EXPORTING as_checkbox = 'X'
                text        = 'Wait for PCP message'
      CHANGING  field       = pcp ).
    DATA(time) = 60.
    in->request(
      EXPORTING  text        = 'Waiting time'
      CHANGING  field       = time ).
    DATA(patt) = |{ txt WIDTH = 1 }{ hex WIDTH = 1 }{ pcp WIDTH = 1 }|.

    DATA(receiver) = NEW message_receiver( ).

    TRY.
        cl_amc_channel_manager=>create_message_consumer(
            i_application_id = 'DEMO_AMC'
            i_channel_id     = '/demo_text'
            )->start_message_delivery( i_receiver = receiver ).
      CATCH cx_amc_error INTO DATA(text_exc).
        cl_demo_output=>display( text_exc->get_text( ) ).
    ENDTRY.

    TRY.
        cl_amc_channel_manager=>create_message_consumer(
            i_application_id = 'DEMO_AMC'
            i_channel_id     = '/demo_binary'
            )->start_message_delivery( i_receiver = receiver ).
      CATCH cx_amc_error INTO DATA(binary_exc).
        cl_demo_output=>display( binary_exc->get_text( ) ).
    ENDTRY.

    TRY.
        cl_amc_channel_manager=>create_message_consumer(
            i_application_id = 'DEMO_AMC'
            i_channel_id     = '/demo_pcp'
            )->start_message_delivery( i_receiver = receiver ).
      CATCH cx_amc_error INTO DATA(amc_exc).
        cl_demo_output=>display( amc_exc->get_text( ) ).
    ENDTRY.

    CASE patt.
      WHEN `X  `.
        WAIT FOR MESSAGING CHANNELS
             UNTIL receiver->text_message   IS NOT INITIAL
             UP TO time SECONDS.
      WHEN `XX `.
        WAIT FOR MESSAGING CHANNELS
             UNTIL receiver->text_message   IS NOT INITIAL AND
                   receiver->binary_message IS NOT INITIAL
             UP TO time SECONDS.
      WHEN `XXX`.
        WAIT FOR MESSAGING CHANNELS
             UNTIL receiver->text_message   IS NOT INITIAL AND
                   receiver->binary_message IS NOT INITIAL AND
                   receiver->pcp_message    IS BOUND
             UP TO time SECONDS.
      WHEN ` X `.
        WAIT FOR MESSAGING CHANNELS
             UNTIL receiver->binary_message IS NOT INITIAL
             UP TO time SECONDS.
      WHEN ` XX`.
        WAIT FOR MESSAGING CHANNELS
             UNTIL receiver->binary_message IS NOT INITIAL AND
                   receiver->pcp_message    IS BOUND
             UP TO time SECONDS.
      WHEN `  X`.
        WAIT FOR MESSAGING CHANNELS
             UNTIL receiver->pcp_message    IS BOUND
             UP TO time SECONDS.
      WHEN OTHERS.
        RETURN.
    ENDCASE.

    DATA(out) = cl_demo_output=>new( ).
    IF txt = 'X' AND receiver->text_message IS NOT INITIAL.
      out->next_section( `Text Message`
        )->write( receiver->text_message ).
    ENDIF.
    IF hex = 'X' AND receiver->binary_message IS NOT INITIAL.
      out->next_section( `Binary Message`
        )->write_json( receiver->binary_message ).
    ENDIF.
    IF pcp = 'X' AND receiver->pcp_message IS BOUND.
      DATA fields TYPE pcp_fields.
      TRY.
          receiver->pcp_message->get_fields(
            CHANGING c_fields = fields ).
          DATA(body) = receiver->pcp_message->get_text( ).
        CATCH cx_ac_message_type_pcp_error INTO DATA(pcp_exc).
          cl_demo_output=>display( pcp_exc->get_text( ) ).
      ENDTRY.
      IF fields IS NOT INITIAL OR
         body   IS NOT INITIAL.
        out->next_section( 'Push Channel Protocol (PCP)'
          )->write( fields
          )->write_html( body ).
      ENDIF.
    ENDIF.
    out->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  amc_demo=>main( ).

Description

The local class message_receiver implements the interfaces IF_AMC_MESSAGE_RECEIVER_TEXT, IF_AMC_MESSAGE_RECEIVER_BINARY, and IF_AMC_MESSAGE_RECEIVER_PCP. AMC consumers (created using the factory method CREATE_MESSAGE_CONSUMER of the system class CL_AMC_CHANNEL_MANAGER) are used to register instances of this local class for the messaging channels /demo_text, /demo_binary, and /demo_pcp of the application DEMO_AMC from the package SABAPDEMOS.

It is possible to select which messages are waited for. As specified by this selection, the WAIT statement creates a wait state until all the fields in question text_message, binary_message, and pcp_message have been filled in the callback routines RECEIVE (themselves triggered by messages). The wait time is limited to a definable number of seconds. The transaction SMAMC displays the registered AMC consumer during the wait time.

The required messages can be sebt by the program DEMO_SEND_AMC (see the executable example Sending AMC Messages) from any current AS ABAP user session. The content of the messages is displayed once they have been received. The executable example APC, WebSocket Communication shows how the messaging channels are associated with ABAP push channels (APC). The program DEMO_RECEIVE_APC also receives messages sent from Web pages associated with an APC like this.

The ID of the receiver session is displayed on the input window on the program. If this ID is entered in the executable example for sending AMC messages, point-to-point communication is used and only this receiver session is sent.