ABAP News for Release 7.50 – ABAP Channels Reloaded
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.
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:
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.
___
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.
Really? But I guess the question does not belong here.
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..
Why don't you write a blog about what's bothering with "shared memory"?
I cannot see how's that going to help me...
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.
Hi,
I have not looked into doco yet but does this mean that we can finally have a native Kafka client in ABAP?
Thanks
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
Hello Horst,
I'm struggling with the ABAP TCP client example.
The ncat TCP server is running successfully
but then i get the following error message:
The error is raised from client->connect( ).
ICM trace shows this
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
Masoud Aghadavoodi Jolfaei, any idea? Thks, Horst
Hi Christian,
You have tried to test a telnet connection to IP and port?
Possibly can be firewall restriction.
hm, connecting via telnet is not possible .
But when i adjust the ncat call a bit (omitting the ip-adress) i can connect via telnet
But i get the same error from the report
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
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.
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
Hello,
Would you have more information about Detached Client ? Which are the typical uses cases for that?
I believe it's not possible that an ABAP AS create the connection and to retrieve it in UI5?
Thanks
Aurélien
Hi,
I want to do that TCP/IP send data to SAP via ABAP. So is there any way to listen the TCP/IP with ABAP? Please let me know is it possible and how to do?
Thanks in advance.
Best regards.
Nurullah Rüstem
Guess https://help.sap.com/viewer/05d041d3df1a4595a3c45f57c15e2325/1709.000/en-US/4f3f842b2e2447789c3a2ad1e5b67668.html is what you are looking for
Hello,
when using native TCP Servers (Connection type TCP Socket) on SAP Systems, how can I restrict access to specific devices (for example by checking the IP-address of the incomming message) ?
regards
Tobias
Hello,
is there a Limit on how many concurrent Connections are possible to TCP Servers ?
Is it possible to have for example hundreds of sensors sending their data to SAP using the same TCP port ?
regards
Tobias
Hi Keller,
Good day,
I found link to this blog in one of the SCN Threads related to Weigh Scale, so thought to check whether we can use ABAP Channel in my case or not.
Currently we are reading the Weight Information from Weigh Bridge by reading the com port but with this we are facing issues
With the above issues our client operations are getting delayed client is asking for stable solution we requested WB Vendor for API & Vendor said he can't.
kindly please help us to know whether it is possible through ABAP Channel, thanks.
HI,
I am confused a little bit by the detached client usage. I want to use APC to send messages to kafka triggered from different user's operation. To reuse the connection, I need a kind of detach in one connection set up session. Then in different sending sessions try to get the connection based on 'attach_handle' then send message from there. Would this be suitable scenario for the detail client usage?
If not, may u have any suggestion about reuse connection? Thanks in advance.