ABAP Channels: APC and AMC
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A normal web page asks, and the server answers: the server can never speak first. Many business screens need the opposite. When a clerk approves an expense claim, the manager’s browser should show the news at once, without anybody pressing reload. ABAP Channels are the classic ABAP answer, and they have two parts. An ABAP Messaging Channel (AMC) lets one ABAP program publish a message that any number of other ABAP programs, in other user sessions and even on other application servers, receive. An ABAP Push Channel (APC) lets an ABAP system keep a two-way connection open to a browser or another program, using the WebSocket protocol or plain TCP sockets, and send data through it whenever it likes. Linked together, an AMC message can travel all the way to a browser. This page explains both, the repository objects they need, the statements and classes the Keyword Documentation names, and the limits. It is written against Standard ABAP release 8.16. ABAP Channels belong to classic Standard ABAP on premise: the Keyword Documentation for ABAP Cloud has no topic for them, and the Standard ABAP topic carries a notice that it is out of maintenance (see the section about status below). After reading, you can say which channel to use for which job, read the sender and receiver code of an AMC example, and follow the path of a message from an approving program to a browser.
Before you read
-
Memory Organization and Sessions: what a user session and an internal session are, because a message goes from one session to another.
-
Classes and Objects and Interfaces and Inheritance: the code on this page is made of classes that implement interfaces and inherit from system classes.
-
Events: the in-program way to notify listeners. AMC does the same job between programs, and the page shows where the two differ.
-
Exceptions: both channel types report errors with class-based exceptions.
-
SAP LUW, Update Tasks and Commit: several channel statements cause a database commit, so the rules of the SAP LUW matter.
-
WebRTC and WebSockets: the general technology that APC builds on; the page The WebSocket Protocol and The Browser WebSocket API cover the browser side.
Key terms
-
ABAP Channels: the common name of AMC and APC, event-based communication by messages between application server instances and with the Internet.
-
AMC (ABAP Messaging Channel): a named channel, stored as a repository object, through which ABAP programs send and receive messages.
-
APC (ABAP Push Channel): a repository object that lets AS ABAP act as a WebSocket or TCP socket server or client.
-
Publish and subscribe: a way to communicate in which the sender publishes a message without knowing who receives it.
-
WebSocket: a protocol that keeps one connection open so that both sides can send at any time.
-
APC handler class: the class that the system instantiates to react to the events of one push channel connection.
-
Push Channel Protocol (PCP): SAP’s own message format for WebSocket messages, with name and value fields and a body.
-
Stateful and stateless APC: whether the attributes of the handler are kept between the messages of one connection.
Push instead of pull
In the usual web model, which this documentation calls the pull principle, every answer of a server needs a request from the client before it. The push principle only needs an open connection between client and server, and the server uses it to pass information as soon as the information exists. APC provides the open connection, over the WebSocket protocol, which every standard browser supports, or over TCP sockets, which suit devices such as embedded systems and programmable logic controllers that do not speak WebSocket.
The second problem is inside the ABAP system. A big system has several application server instances, and a browser is connected to exactly one of them. A program on another instance cannot reach that connection directly. AMC solves this: a message sent through a messaging channel is delivered to every registered receiver on all instances (the communication between instances goes through the message server). So the two pieces fit together: AMC moves the news between programs, APC moves it between the system and the browser.
The diagram shows the example of this page: the program that approves a claim publishes once, and every connected browser and every waiting ABAP receiver gets the message. The names in the diagram are the demo objects of the Keyword Documentation, introduced below.
Where this applies and what its status is
ABAP Channels are a classic Standard ABAP technology. Three facts decide how much weight to give them.
-
The Standard ABAP Keyword Documentation topic "ABAP Channels", including all its subtopics, carries a notice that it is out of maintenance, that there is no guarantee that the information is complete and up to date, and that no active support is provided. The topic still describes the statements and classes in full, and the systems that use them keep working, but the wording is a reason to confirm the details on your own release before you design something new.
-
The Keyword Documentation for ABAP for Cloud Development has no topic for AMC or APC. Its examples also say that they use demo objects that are not released for ABAP Cloud development. Treat channels as not available in ABAP Cloud.
-
Release notes describe changes over the years. For example, the length limit of an AMC message grew in ABAP release 7.65 from about 30,000 bytes to about 1 MB, so older texts give a smaller number than the current documentation.
For new work in ABAP Cloud, the neighbouring tools are the RAP business events of RAP Business Events and the ABAP daemons that the Keyword Documentation groups together with channels under "Channels and Daemons".
ABAP Messaging Channels (AMC)
An AMC communication needs a channel that exists as a repository object. You create it in the ABAP Workbench by opening
the context menu of a package and choosing Connectivity, or in the Connectivity Browser of the Object Navigator, which
transaction SAMC opens. A channel is identified by the application it belongs to and by its name. The name starts with a
slash and is not case-sensitive. These are its properties, as the documentation lists them:
| Property | What it decides |
|---|---|
Message type |
The data type of the messages: |
Scope |
Who may receive: |
Authorized programs |
A list of the programs that may do each activity: send, receive, or bind an APC WebSocket as a consumer (usually the class pool of an APC handler class). |
Any data object can be sent if the sender serializes it and the receiver deserializes it, for example as XML or JSON in a
string. A message is limited to about 1 MB by default; the profile parameter rdisp/long_messages/max_length raises the
limit. Text is converted to UTF-8.
Sending (publish)
The sender asks the factory method CREATE_MESSAGE_PRODUCER of the system class CL_AMC_CHANNEL_MANAGER for a sender
object, passing the application and the channel. The returned reference has the general type IF_AMC_MESSAGE_PRODUCER and
is cast to the interface for the message type, IF_AMC_MESSAGE_PRODUCER_TEXT, IF_AMC_MESSAGE_PRODUCER_BINARY or
IF_AMC_MESSAGE_PRODUCER_PCP, each with a method SEND. The sender normally does not know whether anyone listens: that is
the publish half of publish and subscribe. Errors raise CX_AMC_ERROR. The optional parameter I_SUPPRESS_ECHO stops the
message from coming back to the sender’s own session, which matters when the sender’s session also holds an APC
connection.
The listing is a trimmed version of the Keyword Documentation example for sending. It uses the demo channel /demo_text of
the application DEMO_AMC (package SABAPDEMOS), because the channel must already exist. It requires an SAP system; not
run here (the local lab has no AMC).
CLASS zcl_claim_notifier DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
CLASS-METHODS announce IMPORTING text TYPE string.
ENDCLASS.
CLASS zcl_claim_notifier IMPLEMENTATION.
METHOD announce.
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 ).
CATCH cx_amc_error INTO DATA(error).
" the channel does not exist, or the program is not authorized to send
ENDTRY.
ENDMETHOD.
ENDCLASS.
Source: AMC - Sending Messages and AMC - ABAP Messaging Channels
Receiving (subscribe)
A receiver does two things. It creates a consumer with CREATE_MESSAGE_CONSUMER of the same class, and it registers
a receiver object with the consumer method START_MESSAGE_DELIVERY. A receiver object is an instance of a class that
implements IF_AMC_MESSAGE_RECEIVER_TEXT, IF_AMC_MESSAGE_RECEIVER_BINARY or IF_AMC_MESSAGE_RECEIVER_PCP. Each
interface has a method RECEIVE, which the system calls back for every message. Messages arrive only while the program is
ready: START_MESSAGE_DELIVERY makes it ready, and STOP_MESSAGE_DELIVERY ends it. A program that has registered a
receiver has to be waiting for the callbacks to run, and the statement for that is
WAIT FOR MESSAGING CHANNELS UNTIL log_exp [UP TO sec SECONDS]. While the condition is false, the program waits. Each
message runs the RECEIVE method and the condition is checked again, so one wait can consume several messages until one of
them makes the condition true.
The listing is a trimmed version of the receiver example. It requires an SAP system; not run here.
CLASS lcl_receiver DEFINITION.
PUBLIC SECTION.
INTERFACES if_amc_message_receiver_text.
DATA last_text TYPE string.
ENDCLASS.
CLASS lcl_receiver IMPLEMENTATION.
METHOD if_amc_message_receiver_text~receive.
last_text = i_message.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(receiver) = NEW lcl_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(error).
WRITE / 'cannot subscribe'.
RETURN.
ENDTRY.
WAIT FOR MESSAGING CHANNELS
UNTIL receiver->last_text IS NOT INITIAL
UP TO 60 SECONDS.
WRITE / receiver->last_text.
Source: AMC - Receiving Messages and WAIT FOR MESSAGING CHANNELS
After WAIT FOR MESSAGING CHANNELS, sy-subrc tells how the wait ended: 0 when the condition is true, 4 when it is false
and no receiver is registered or all pending messages have been handled, and 8 when the time given with UP TO ran out. Receiving AMC messages is not possible during the update process, and the two methods START_MESSAGE_DELIVERY and
STOP_MESSAGE_DELIVERY both cause a database commit, which matters for the SAP LUW (see
SAP LUW, Update Tasks and Commit).
Point to point
Publish and subscribe sends to all receivers. When a sender must reach one specific session, AMC offers point-to-point
communication. The receiver calls the static method GET_CONSUMER_SESSION_ID of CL_AMC_CHANNEL_MANAGER to get the ID
of its session and passes it to the sender in some other way. The sender calls CREATE_MESSAGE_PRODUCER_BY_ID, and only
that session processes the message. The parameter I_COMMUNICATION_TYPE chooses synchronous sending, where the sender
waits for confirmation that the message was handed to the receiver session and gets an exception otherwise, or
asynchronous sending, which is the default.
Source: AMC - ABAP Messaging Channels
ABAP Push Channels (APC)
APC lets AS ABAP take part in WebSocket or TCP socket communication in several roles.
| Role | What it means |
|---|---|
APC server |
A browser or another client connects to the ABAP system. The server can be stateless or stateful. This page follows this role. |
APC client |
An ABAP program connects to some other server, and the handler gets |
Detached client |
An ABAP program opens a connection and detaches it at once, so that the same or another AS ABAP can attach to it later and the opener itself acts as a server. |
The push channel as a repository object
For an APC server, an ABAP push channel exists as a repository object, created like an AMC in the package context menu
under Connectivity, or in the Object Navigator for APCs that transaction SAPC opens. When you create it you choose the
connection type, WebSocket or TCP socket, and whether the server is stateless or stateful. On a stateful server the
attributes of the handler are kept every time the same client reaches it. Creating the channel generates two more things:
-
a node in the ICF service tree (the Internet Communication Framework, edited in transaction
SICF), which is the web address of the push channel. A browser opens its WebSocket at this address. The node can be activated and deactivated, but not tested directly; -
the APC handler class, a global class that you edit like any other. The system creates one instance of it, in a dedicated APC session, whenever a client reaches the channel.
For the WebSocket protocol an optional subprotocol can be assigned, and the one currently available is SAP’s own Push
Channel Protocol (PCP). PCP messages carry name and value fields and a body, and the matching AMC message type is PCP.
The handler class
The superclass of a handler class depends on the protocol and on the choice of stateless or stateful.
| Superclass | Used for |
|---|---|
|
WebSocket, stateless, no subprotocol |
|
WebSocket, stateless, PCP |
|
WebSocket, stateful, no subprotocol |
|
WebSocket, stateful, PCP |
|
TCP sockets |
All of them have the methods ON_START and ON_MESSAGE, which the handler class must redefine. The system runs ON_START
when the push channel is opened, and it is the place for start-up actions such as binding to an AMC (it can stay empty).
It runs ON_MESSAGE whenever the channel receives a message, and every reaction, including sending an answer, belongs
there. The optional methods ON_ACCEPT, ON_CLOSE and ON_ERROR react to the matching events, and ON_ACCEPT can decide
whether a connection is opened at all. For exact method signatures use the interface IF_APC_WSP_EXTENSION in your system
or the demo handler classes (CL_APC_WS_EXT_DEMO_APC and CL_APC_WSP_EXT_DEMO_APC_PCP), which the Keyword Documentation
names as the models.
A message handled in a handler class runs as APC processing in a separate APC session, and some statements behave differently there. Messages of types I, S and W are ignored, and messages of types A, E and X end the APC session (in a stateful server the connection is closed). In stateful processing the program runs in non-blocking mode, in which every statement that could stop incoming messages from being received is forbidden. A database commit happens each time a message is sent and each time the APC processing ends, except in an update.
Source: APC - ABAP Push Channels and Messages - APC Processing
An ABAP program as client
When an ABAP program is the client, it creates the connection with CL_APC_WSP_CLIENT_MANAGER (methods CREATE_BY_URL or
CREATE_BY_DESTINATION) for WebSocket or CL_APC_TCP_CLIENT_MANAGER for TCP sockets, opens it with CONNECT, and sends
through a message manager. Answers arrive in ON_MESSAGE of its handler object when the session is rolled in after a
change of work process, and the statement WAIT FOR PUSH CHANNELS UNTIL log_exp [UP TO sec SECONDS] makes the program wait
for them on purpose. It is the counterpart of WAIT FOR MESSAGING CHANNELS, and the two have additions that combine them.
Source: APC - ABAP Push Channels and WAIT FOR PUSH CHANNELS
Linking APC and AMC: a message from ABAP to the browser
On its own a WebSocket reaches exactly one application server instance, so a program on another instance has no way to send
to it. Linking the two removes the problem. In the handler class, typically in ON_START, the method
BIND_AMC_MESSAGE_CONSUMER of the interface IF_APC_WSP_BINDING_MANAGER binds the connection to a messaging channel of
a suitable message type. From then on every message sent through that channel is handled automatically as an APC message,
because the APC framework wraps the AMC methods that are needed. The only prerequisites are that the channel’s list of
authorized programs allows the handler class to bind a consumer, and that the push channel is linked only once.
UNBIND_AMC_MESSAGE_CONSUMER removes the link.
The demo of the Keyword Documentation shows the whole path. The push channel DEMO_APC (package SABAPDEMOS) is a
stateless WebSocket server without subprotocol, reached at the ICF path /sap/bc/abap/demo_apc. When it is opened with the
form field amc=x, it is linked to the messaging channel /demo_text. Every browser page connected to it then receives
every message sent through that channel, for example by the class CL_DEMO_SEND_AMC, and ON_MESSAGE answers with a
message that every connected page also gets. That is why, in the demo, the answer to one page is visible to the others.
Conversely, CL_DEMO_RECEIVE_AMC receives what the pages send. Before the demo can work, the protocols WEBSOCKET and
WEBSOCKETS must be active services in transaction SMICM.
The browser side is ordinary JavaScript, covered in
The Browser WebSocket API. The listing sketches a page that
connects to the demo push channel and shows what arrives. The path and the form fields sap-client and amc are those of
the demo; the host, port and client number are placeholders for your system. It requires an SAP system; not run here.
const url = 'wss://my-abap-host:44300/sap/bc/abap/demo_apc?sap-client=100&amc=x';
const socket = new WebSocket(url);
socket.onopen = () => socket.send('Hello from the browser');
socket.onmessage = (event) => {
document.getElementById('news').textContent = event.data;
};
socket.onclose = () => console.log('push channel closed');
If a handler needs the browser to use PCP, the page includes the JavaScript file sap-pcp-websocket.js that the system
provides in its MIME repository under /sap/public/bc/ur/sap-pcp-websocket.js. Without PCP any standard WebSocket code
works. Because the page calls the push channel from a page address, a browser page that comes from a different address or
domain than the ABAP system is only accepted when that origin is listed in the table APC_CROSS_ORIGIN, maintained with
transaction SAPC_CROSS_ORIGIN, and the user needs authorizations for the fields of the authorization object S_APC_ORIG.
Putting the parts together for the expense-claim story: the approving program calls the sender method of the listing above once per approval. Browsers of managers hold WebSockets to the push channel, whose handler is bound to the same messaging channel, so each of them receives the text at once, whichever application server it is connected to.
Source: APC - ABAP Push Channels
Tools and transactions
The documentation names these transactions for working with channels.
| Transaction | Use |
|---|---|
|
Object Navigator for ABAP messaging channels. |
|
Object Navigator for ABAP push channels. |
|
The ICF service tree, where the generated node of a push channel can be activated or deactivated. |
|
Internet Communication Manager monitor; the protocols |
|
Overview of all messaging channels for which AMC consumers are registered right now. |
|
Switch the AMC log on or off for chosen channels, and manage its entries. |
|
Overview of all APC connections of an AS ABAP; surplus connections can be closed here. |
|
Allowed origins for WebSocket pages from other addresses. |
The ABAP debugger, runtime analysis and performance trace work as usual, because messages are sent and received inside AS ABAP, and external breakpoints can be set for APC processing. Access to a messaging channel is controlled by the list of authorized programs, and both channel types can use a virus scan profile for the virus scan interface.
Common mistakes and limits
| Mistake or limit | What the documentation says |
|---|---|
Expecting a message to be stored |
The sender does not usually know whether receivers are registered, so do not rely on a particular receiver having seen a message. |
Echo to the sender |
With a linked channel, the sender’s own APC connection also receives the message. Pass |
Message larger than the limit |
About 1 MB by default; raise it with |
Database commit surprises |
|
Commit inside a client handler |
In the |
Linking twice |
A push channel can be bound to a messaging channel only once; a second bind, and the removal of a link that does not exist, raise an exception. |
TCP sockets and AMC |
The link between APC and AMC is not possible for TCP sockets. |
Updates |
Receiving AMC messages and linking APC with AMC are not possible during the update process. |
Summary
-
ABAP Channels let ABAP send messages by push. AMC connects ABAP programs in different sessions and application server instances; APC connects the ABAP system with browsers and other programs over WebSockets or TCP sockets.
-
An AMC is a repository object with a message type, a scope and a list of authorized programs. A sender creates a producer with
CREATE_MESSAGE_PRODUCERand sends. A receiver registers a receiver object withSTART_MESSAGE_DELIVERYand waits withWAIT FOR MESSAGING CHANNELS. -
An APC server is a repository object plus a handler class that redefines
ON_STARTandON_MESSAGE; stateful handlers keep their attributes between messages. -
Binding the handler to an AMC with
BIND_AMC_MESSAGE_CONSUMERmakes one published message reach every connected browser, whichever application server it is connected to. -
Channels are classic Standard ABAP: the documentation topic is marked out of maintenance, and ABAP Cloud has no topic for them. Most channel calls cause a database commit.
-
None of the listings could run in the local lab; they require an SAP system.