ABAP Keyword Documentation → ABAP - Reference → Data Interfaces and Communication Interfaces → ABAP Channels → AMC - ABAP Messaging Channels → Examples of AMC
AMC, Sending Messages
This example demonstrates how messages are sent using AMC.
Other versions:
7.31 | 7.40 | 7.54
Source Code
TYPES:
BEGIN OF struct,
name TYPE string,
value TYPE string,
END OF struct,
fields TYPE STANDARD TABLE OF struct WITH EMPTY KEY.
DATA: text TYPE abap_bool,
hex TYPE abap_bool,
pcp TYPE abap_bool.
DATA text_message TYPE string VALUE `I am a text message`.
DATA binary_message TYPE string VALUE `I am a binary message`.
DATA pcp_body TYPE string VALUE `I am a PCP body`.
DATA field1 TYPE string VALUE `Field1`.
DATA value1 TYPE string VALUE `Value1`.
DATA field2 TYPE string VALUE `Field2`.
DATA value2 TYPE string VALUE `Value2`.
cl_demo_input=>new(
)->add_field( EXPORTING text = 'Text message'
CHANGING field = text_message
)->add_field( EXPORTING text = 'Send text message'
as_checkbox = 'X'
CHANGING field = text
)->add_line(
)->add_field( EXPORTING text = 'Binary message'
CHANGING field = binary_message
)->add_field( EXPORTING text = 'Send binary message'
as_checkbox = 'X'
CHANGING field = hex
)->add_line(
)->add_field( EXPORTING text = 'Field1' CHANGING field = value1
)->add_field( EXPORTING text = 'Field2' CHANGING field = value2
)->add_field( EXPORTING text = 'PCP body'
CHANGING field = pcp_body
)->add_field( EXPORTING text = 'Send PCP message'
as_checkbox = 'X'
CHANGING field = pcp
)->request( ).
IF text IS NOT INITIAL.
TRY.
CAST if_amc_message_producer_text(
cl_amc_channel_manager=>create_message_producer(
i_application_id = 'DEMO_AMC'
i_channel_id = '/demo_text' )
)->send( i_message = text_message ).
CATCH cx_amc_error INTO DATA(text_exc).
cl_demo_output=>display( text_exc->get_text( ) ).
ENDTRY.
ENDIF.
IF hex IS NOT INITIAL.
DATA(json_writer) = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE message = binary_message
RESULT XML json_writer.
DATA(json) = json_writer->get_output( ).
TRY.
CAST if_amc_message_producer_binary(
cl_amc_channel_manager=>create_message_producer(
i_application_id = 'DEMO_AMC'
i_channel_id = '/demo_binary' )
)->send( i_message = json ).
CATCH cx_amc_error INTO DATA(binary_exc).
cl_demo_output=>display( binary_exc->get_text( ) ).
ENDTRY.
ENDIF.
IF pcp IS NOT INITIAL.
DATA(fields) = VALUE fields( ( name = 'Field1' value = value1 )
( name = 'Field2' value = value2 ) ).
DATA(body) = |<b>{ pcp_body }</b>|.
TRY.
DATA(pcp_message) = cl_ac_message_type_pcp=>create( ).
pcp_message->set_field( i_name = fields[ 1 ]-name
i_value = fields[ 1 ]-value ).
pcp_message->set_field( i_name = fields[ 2 ]-name
i_value = fields[ 2 ]-value ).
pcp_message->set_text( body ).
CAST if_amc_message_producer_pcp(
cl_amc_channel_manager=>create_message_producer(
i_application_id = 'DEMO_AMC'
i_channel_id = '/demo_pcp' )
)->send( i_message = pcp_message ).
CATCH cx_amc_error INTO DATA(amc_exc).
cl_demo_output=>display( amc_exc->get_text( ) ).
CATCH cx_ac_message_type_pcp_error INTO DATA(pcp_exc).
cl_demo_output=>display( pcp_exc->get_text( ) ).
ENDTRY.
ENDIF.
Description
The factory method CREATE_MESSAGE_PRODUCER of the system class CL_AMC_CHANNEL_MANAGER is used to create sender objects for the messaging channels /demo_text, /demo_binary, and /demo_pcp of the application DEMO_AMC from the package SABAPDEMOS and performs a casting for these objects to the appropriate interfaces. The SEND methods of these interfaces send a text string, JSON data as a byte string and two name/value pairs plus a body in SAP's own Push Channel protocol (PCP) through the messaging channels. The data of the PCP message is passed to an object of the class CL_AC_MESSAGE_TYPE_PCP and serialized there.
The example Receiving AMC Messages demonstrates how these messages can be received in ABAP programs. The example APC, WebSocket Communication shows how the messaging channels are associated with ABAP push channels (APC) and that the messages sent using DEMO_SEND_APC are received by Web pages associated with an APC like this.