Show TOC

Receiving AMC MessagesLocate this document in the navigation structure

The local class message_receiver implements the interfaces IF_AMC_MESSAGE_RECEIVER_TEXT, IF_AMC_MESSAGE_RECEIVER_BINARY and IF_AMC_MESSAGE_RECEIVER_PCP. Instances of this class are registered for the Messaging Channels /demo_text, /demo_binary and /demo_pcp of the application DEMO_AMC from package SABAPDEMOS with AMC consumers that have been generated using the factory method CREATE_MESSAGE_CONSUMER of the system class CL_AMC_CHANNEL_MANAGER.

You can choose which messages are to be waited for. Corresponding to your choice the WAIT statement generates a waiting state that lasts until the respective fields text_message, binary_message und pcp_message have been supplied in the callback routines triggered by the messages. The waiting time is limited to a number of seconds you can specify. Transaction SMAMC displays the registered AMC consumers during the waiting time.

You can send the required messages using the program DEMO_SEND_AMC (see Sending AMC Messages) from any user session of the current AS ABAP. After receiving the messages, their content is displayed. The example APC-WebSocket Communication shows the collaboration of the Messaging Channels with ABAP Push Channels (APC). the program DEMO_RECEIVE_APC also receives messages that are sent by web sites connected to such an APC.

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(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.

    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 60 SECONDS.

    TYPES:
      BEGIN OF struct,
        name  TYPE string,
        value TYPE string,
      END OF struct,
      fields TYPE STANDARD TABLE OF struct WITH EMPTY KEY.
    TRY.
        DATA(fields) = VALUE fields(
          ( name = 'Field1'
            value = receiver->pcp_message->get_field( 'Field1' ) )
          ( name  = 'Field2'
            value = receiver->pcp_message->get_field( 'Field2' ) ) ).
        DATA(body) = cl_abap_codepage=>convert_from(
          receiver->pcp_message->get_binary( ) ).
      CATCH cx_ac_message_type_pcp_error INTO DATA(pcp_exc).
        cl_demo_output=>display( pcp_exc->get_text( ) ).
    ENDTRY.

    cl_demo_output=>new(
      )->begin_section( receiver->text_message
      )->begin_section( 'Direct Message'
      )->write_json( receiver->binary_message
      )->next_section( 'Push Channel Protocol (PCP)'
      )->write( fields
      )->write_html( body
      )->display( ).
  ENDMETHOD.
ENDCLASS.

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