Skip to Content

ABAP Channels (ABAP Messaging Channels AMC and ABAP Push Channels APC) were introduced with ABAP 7.40, SP02, SP05. They enable an event based bidirectional communication between ABAP application servers and with the internet. For that, a dedicated Push Channel Protocol (PCP) can be used since 7.40, SP08. Before ABAP 7.50, the communication between ABAP servers and the internet was restricted to the Web Socket Protocol (WS) and the AS ABAP acting as a stateless APC server. The following features are new with ABAP 7.50:

Stateful APC Servers

Before ABAP 7.50, each ABAP Push Channel that you created as a repository object in SE80 or SAPC was automatically stateless. An APC handler class always inherited from CL_APC_WSP_EXT_STATELESS_BASE or CL_APC_WSP_EXT_STATELESS_PCP_B. Now you can select Stateful too.

/wp-content/uploads/2015/11/adoc7_838934.gif

The respective APC handler classes will inherit from CL_APC_WSP_EXT_STATEFUL_BASE or CL_APC_WSP_EXT_STATEFUL_PCP_B. For a stateful APC server its context and especially the attributes of the APC handler class are not deleted between different client accesses.


Stateful APC applications are running in a so called Non Blocking Model, where all blocking statements are forbidden.Those are the usual suspects that call programs or screens, leave programs or screens, or interrupt.

AS ABAP as APC Client

From ABAP 7.50 on, you can define handler classes in ABAP, that implement either IF_APC_WSP_EVENT_HANDLER or IF_APC_WSP_EVENT_HANDLER_PCP (the latter for using the Push Channel Protocol). Methods of such classes can handle messages from an APC server. The actual client is then created with factory methods of the classes CL_APC_WSP_CLIENT_MANAGER or CL_APC_TCP_CLIENT_MANAGER. Using a client object, you can open and close connections to an APC server and you can create and send messages.

In order to receive messages in an ABAP APC client, the AS ABAP has to wait for those. Such a wait state can be programmed explicitly with the new ABAP statement WAIT FOR PUSH CHANNELS, that completes the already existing WAIT FOR MESSAGING CHANNELS and WAIT FOR ASYNCHRONOUS TASKS.

See the example in the next section.

TCP Protocol

Besides the WebSocket protocol (WSP), the ABAP Push Channels framework now also supports native TCP (Transmission Control Protocol) sockets. This allows communication with Clients and Servers that  do not support WSP. Those can be embedded systems or Progammable Logic Controllers (PLC). And this ultimately connects our good old ABAP directly to – attention, buzz word alarm – the internet of things (IOT)!

For an AS ABAP to work as a TCP server, you simply select the respective connection type:

/wp-content/uploads/2015/11/adoc8_838935.gif

PCP cannot be used as subprotocol here, but stateful TCP servers are possible. The respective APC handler classes will inherit from CL_APC_TCP_EXT_STATELESS_BASE or CL_APC_TCP_EXT_STATEFUL_BASE.

For an AS ABAP to work as a TCP client, you basically do the same as for WSP clients (see above) but use CL_APC_TCP_CLIENT_MANAGER in order to create the client object.

The following is a complete example for an ABAP program that creates an ABAP TCP client:

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.

CLASS apc_demo DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS main.

ENDCLASS.

CLASS apc_demo IMPLEMENTATION.

  METHOD main.

    DATA(tcp_server) = `C:\ncat\ncat.exe`.

    DATA(ip_adress)  = cl_gui_frontend_services=>get_ip_address( ).

    DATA(port)       = `12345`.

    DATA(terminator) = `0A`.

    DATA(msg)        = `Hello TCP, answer me!`.

    “Server

    IF cl_gui_frontend_services=>file_exist(

         file = tcp_server ) IS INITIAL.

      cl_demo_output=>display( ‘TCP Server not found!’ ).

      LEAVE PROGRAM.

    ENDIF.

    cl_gui_frontend_services=>execute(

    EXPORTING

      application = `cmd.exe`

      parameter  =  `/c ` && tcp_server &&

                   ` -l ` && ip_adress && ` -p ` && port ).

    WAIT UP TO 1 SECONDS.

    TRY.

        DATA(event_handler) = NEW apc_handler( ).

        “Client

        DATA(client) = cl_apc_tcp_client_manager=>create(

          i_host   = ip_adress

          i_port  = 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( ).

        “Send mesasage from client

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

        CONCATENATE binary_msg binary_terminator

               INTO binary_msg IN BYTE MODE.

        message->set_binary( binary_msg ).

        message_manager->send( message ).

        “Wait for a message from server

        CLEAR event_handler->message.

        WAIT FOR PUSH CHANNELS

             UNTIL event_handler->message IS NOT INITIAL

             UP TO 10 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.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  apc_demo=>main( ).

To simulate an external TCP server, the program uses the current frontend computer, to which the freely available Ncat can be downloaded. The ABAP program starts Ncat.exe, which waits for a message. The ABAP APC client sends a message and waits itself by using WAIT FOR PUSH CHANNELS. You can enter a message in the Ncat console and will receive it in ABAP. Note, that the terminator character of the TCP frame structure must be defined explicitly.

Detached APC Client

This one is a little bit tricky. The use case is a scenario, where an AS ABAP wants to work as an APC server, but wants to open the connection itself. In order to do so, the AS ABAP first acts like an APC client (see above) and opens a connection. But instead of sending a message, it is immediately detached but the connection remains open. Now the same or any other ABAP application server can be attached to the connection as a so called attached client, and the AS ABAP of the detached client then plays the role of a stateless or stateful server.

A deatched client similar to a real client needs handler classes implementing IF_APC_WSP_EVENT_HANDLER or IF_APC_WSP_EVENT_HANDLER_PCP. But only the ON_OPEN event is needed in order to receive a connection handle. The detached clients itself are created with CL_APC_WSP_CLIENT_CONN_MANAGER or CL_APC_TCP_CLIENT_CONN_MANAGER. Such a detached client is used to open the connection and to be detached immediately by its method CONNECT_AND_DETACH.

Now, the connection handle can be used to create an attached client with method ATTACH of CL_APC_WSP_CLIENT_CONN_MANAGER or CL_APC_TCP_CLIENT_CONN_MANAGER. The attached client object can send messages to the AS ABAP of the detached client. Oh boy.

APC Access Object

With a similar mechanism as for attaching an attached client to a detached client, you can create an access object for any APC connection. If an APC handler class decides to publish a connection handle that it can get itself with method GET_CONNECTION_ATTACH_HANDLE from its context object, you can use the same method ATTACH as above to create an access object, that can send messages to the connection. In this case some restrictions for the connection handle apply. Only the same session, client and user or the same program and client are allowed to use such a connection handle.


AMC Point-to-Point-Communication

ABAP Messaging Channels as you know them up to now are based on a publlish and subscribe mechanism, where the sender does not know the receivers. With ABAP 7.50, also a point-to-point communication is possible, where a sender can send messages to a specific receiver. For that, the sender needs the id of the receiver. A receiver can get its id with method GET_CONSUMER_SESSION_ID of its channel manager and publish it appropriately. A sender can use that id to create a respective sender object with method CREATE_MESSAGE_PRODUCER_BY_ID of its channel manager.The message can be send asynchronously or synchronously. In the latter case, the sender waits for a feedback.


More Information and Examples

See ABAP Channels.

___

To report this post you need to login first.

14 Comments

You must be Logged on to comment or reply to a post.

  1. Daniel Ruiz

    and I can finally retry the agar.io server implementation.. – @Horst: is there anything scheduled to make shared memory “perform” – it’s pretty slow in general.

    (0) 
      1. Daniel Ruiz

        well, you always post about new abap stuff so I can see no better place to ask.. in fact you’re sort of the only one who does that (on SAP side) as far I’m aware..

        (0) 
            1. Nicola Fankhauser

              Even though certainly OT here, the “Shared Memory” implementation is not working in productive environments in scenarios where any client can change content of the shared memory. It is clearly stated in SAP Help that data must only “rarely change”, that is “once a day up to a maximum of once an hour“. So much for your idea about using Shared Memory. Been there, done that.

              (0) 
    1. Horst Keller Post author

      Hello,

       

      I’d rather say you can use a native Kafka client to connect to an AS ABAP server or you can use an AS ABAP client to connect to a native Kafka server using the TCP protocol.

       

      Horst

      (0) 
  2. Christian Guenter

    Hello Horst,

     

    I’m struggling with the ABAP TCP client example.

    The ncat TCP server is running successfully

    2016-03-19 11_31_06-C__Windows_SysWOW64_cmd.exe - C__temp_ncat.exe  -l 192.168.1.107 -p 12345.png

     

    but then i get the following error message:

    WebSocket connection set up has failed with error text:  Connection refused by server (ICM_HTTP_CONNECTION_REFUSED)

     

    The error is raised from client->connect( ).

     

    ICM trace shows this

     

    2016-03-19 11_43_58-A4H(3)_001 ICM Monitor of Server vhcala4hci_A4H_00.png

     

    Any hints?

    I use SAP NetWeaver AS ABAP and SAP BW 7.5 SP01 on SAP HANA SP10 [Developer Edition] from the CAL.


    Probably the ICM cannot reach my local running ncat server?

     

    Thanks.

     

    Regards Christian

    (0) 
      1. Christian Guenter

        hm, connecting via telnet is not possible .

        2016-03-25 11_40_35-A4H(1)_001 Output.png

         

        But when i adjust the ncat call a bit (omitting the ip-adress) i can connect via telnet

         

        2016-03-25 11_47_22-Telnet 192.168.1.107.png

        But i get the same error from the report

        WebSocket connection set up has failed with error text:  Connection refused by server (ICM_HTTP_CONNECTION_REFUSED)

         

        Just to clarify i’m running this on my local machine where also SAPGUI and AiE is running. I wonder how shall the ICM of AS ABAP be able to connect to my local running ncat server? Can he somehow reuse the SAPGui connection? Maybe i lack the basic understanding…

         

        Christian

        (0) 
        1. Horst Keller Post author

          The above example works fine on my local machine (otherwise wouldn’t had published it ). The AS ABAP connects via the IP address.

           

          The APC and ncat stuff comes from Masoud. I added the ABAP handling of the server with the frontend services.

           

          The example is also delivered with the example library from SP02 or so.

          (0) 
        2. Masoud Aghadavoodi Jolfaei

          Hi Christian,

          in this example the ncat act as a TCP server (listener) running on the local PC (localhost:12345). In your case seems  that the IP address of your localhost is 192.168.1.162. The IP address can also be obtained on MS Windows OS using  “ipconfig” command and check the entry “IPv4 Address”. The SAPGUI instance running on the desktop is usually able to start an application, e.g. ncat, on the local PC. But it does not mean that the ABAP application running somewhere in intranet/internet (located in different network segment) is also able to reach the local PC. In this example the ABAP application server tries to connect to the ncat server which obviously fails, due to issues regarding network connectivity from AS server -> local PC. This scenario works fine as far the ABAP server runs on the same intranet and DMZ zone as the PC. By the way the same issue arises when you remotely access to an ABAP server using VPN.


          You also find additional documentation and examples for different TCP Socket scenarios in SAP help portal and for the subject ABAP Channels.

          Cheers,

          Masoud


          (0) 

Leave a Reply