Skip to Content
2
Oct 30, 2019 at 11:55 AM

Abap Push Channel cannot receive message

178 Views

Hi Experts,

I have a question about APC( Abap Push Channel ).I can send JSON file to POS device successfully but I can not receive JSON file from POS device. if_apc_wsp_event_handler~on_message method does not triggered. Do you have any advice ? Thanks,

CLASS apc_handler DEFINITION FINAL .
  PUBLIC SECTION.
    INTERFACES if_apc_wsp_event_handler.
    DATA       message TYPE string.
ENDCLASS.


CLASS apc_handler IMPLEMENTATION.
  METHOD if_apc_wsp_event_handler~on_open.
  ENDMETHOD.


  METHOD if_apc_wsp_event_handler~on_message.
    TRY.
        message = i_message->get_text( ).
      CATCH cx_apc_error INTO DATA(apc_error).
        message = apc_error->get_text( ).
    ENDTRY.
  ENDMETHOD.


  METHOD if_apc_wsp_event_handler~on_close.
    message = 'Connection closed!'.
  ENDMETHOD.


  METHOD if_apc_wsp_event_handler~on_error.
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.
  DATA(terminator) = `0A`.
  lv_message = /ui2/cl_json=>serialize( data = ls_json name_mappings = lt_mapping ).
    TRY.
        DATA(event_handler) = NEW apc_handler( ).
        DATA(client) = cl_apc_tcp_client_manager=>create( i_host  = p_ip
                                                          i_port  = p_port
                                                          i_frame = VALUE apc_tcp_frame( frame_type = if_apc_tcp_frame_types=>co_frame_type_terminator
                                                                                         terminator = terminator )
                                                          i_event_handler = event_handler ).
        client->connect( ).
        DATA(message_manager)   = CAST if_apc_wsp_message_manager( client->get_message_manager( ) ).
        DATA(message)           = CAST if_apc_wsp_message( message_manager->create_message( ) ).
        DATA(binary_terminator) = CONV xstring( terminator ).
        DATA(binary_msg)        = cl_abap_codepage=>convert_to( lv_message ).
        CONCATENATE binary_msg binary_terminator INTO binary_msg IN BYTE MODE.
        message->set_binary( binary_msg ).
        message_manager->send( message ).
        CLEAR event_handler->message.
        WAIT FOR PUSH CHANNELS UNTIL event_handler->message IS NOT INITIAL UP TO 100 SECONDS.
        IF sy-subrc = 4.
          cl_demo_output=>display( 'No handler for APC messages registered!' ).
        ELSEIF sy-subrc = 8.
          cl_demo_output=>display( 'Timeout occured!' ).
        ELSE.
          cl_demo_output=>display( |TCP client received:\n\n{ event_handler->message }| ).
        ENDIF.


        client->close( i_reason = 'Application closed connection!' ).


      CATCH cx_apc_error INTO DATA(apc_error).
        cl_demo_output=>display( apc_error->get_text( ) ).
    ENDTRY.