Hi, this is my first blog.
There are many great blogs about APC. Here I just want to show how to get notification in a bsp application using APC without refreshing the web page.
to understand APC better please go through the following series of blogs by masoud.aghadavoodijolfaei
first
TRY.
i_context->get_binding_manager(
)->bind_amc_message_consumer(
i_application_id = 'ZAMC_TEST'
i_channel_id = '/ping' ).
CATCH cx_apc_error INTO DATA(exc).
MESSAGE exc->get_text( ) TYPE 'X'.
ENDTRY.
TRY.
DATA(lv_msg) = i_message->get_text( ).
CAST if_amc_message_producer_text(
cl_amc_channel_manager=>create_message_producer(
i_application_id = 'ZAMC_TEST'
i_channel_id = '/ping' )
)->send( i_message = lv_msg ) ##NO_TEXT.
CATCH cx_amc_error cx_apc_error INTO DATA(lref_exc).
MESSAGE lref_exc->get_text( ) TYPE 'X'.
ENDTRY.
Create a report for sending the content to bsp application:
PARAMETER:p_bar1 TYPE string,
p_bar2 TYPE string,
p_bar3 TYPE string.
DATA: lv_text TYPE string.
CLASS lcl_demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS lcl_demo IMPLEMENTATION.
METHOD main.
TRY.
CONCATENATE p_bar1 p_bar2 p_bar3 INTO lv_text SEPARATED BY '~'.
CAST if_amc_message_producer_text(
cl_amc_channel_manager=>create_message_producer(
i_application_id = 'ZAMC_TEST'
i_channel_id = '/ping' )
)->send( i_message = lv_text ) ."|Static text| ).
CATCH cx_amc_error INTO DATA(lref_text_exc).
cl_demo_output=>display( lref_text_exc->get_text( ) ).
ENDTRY.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
lcl_demo=>main( ).
Create a BSP application:
then from the context menu of the bsp application, create a page.
<%@page language="abap"%>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://rawgit.com/lampieg/LiveGraph/master/jquery.livegraph.js"></script>
<script type="text/javascript">
<%
CONSTANTS lv_path TYPE string VALUE `/sap/bc/apc/sap/zapc_test`.
DATA(lv_location) = cl_http_server=>get_location( application = lv_path ).
IF lv_location CS 'https'.
REPLACE FIRST OCCURRENCE OF 'https' IN lv_location WITH 'wss'.
ELSE.
REPLACE FIRST OCCURRENCE OF 'http' IN lv_location WITH 'ws'.
ENDIF.
* CONCATENATE lv_location lv_path '?amc=x' INTO DATA(lv_url).
CONCATENATE lv_location lv_path INTO DATA(lv_url).
CONDENSE lv_url.
%>
var gv_values;
var ws ;
function WebSocketDemo(para)
{
if ("WebSocket" in window)
{
if (para == "open" )
{
ws = new WebSocket("<%=lv_url%>");
};
if (para == "send" )
{
ws.send( document.getElementById("messageInput").value );
};
if (para == "close" )
{
ws.close( );
};
ws.onmessage = function (evt)
{
var inputMessage = evt.data;
gs_values = inputMessage.split("~");
var lv_tab1 = Number(gs_values[0]);
var lv_tab2 = Number(gs_values[1]);
var lv_tab3 = Number(gs_values[2]);
var updateData = {
tb1: {
value: lv_tab1
},
tb2:{
value: lv_tab2
},
tb3:{
value: lv_tab3
}
};
$('#page').liveGraph('update', updateData);
};
ws.onclose = function()
{
alert("WebSocket closed");
};
}
else
{
alert("Browser does not support WebSocket!");
}
}
</script>
<style>
div[bar="tb3"] span {
background-color: #fff877;
} /* ONLY MATCHES THE FIRST BAR */
div[bar="tb2"] span {
background-color: #9356ff;
} /* ONLY MATCHES THE SECOND BAR */
div[bar="tb1"] span {
background-color: #c3ff4e;
}
</style>
</head>
<body style="font-family:arial;font-size:80%;" onload = "WebSocketDemo('open')">
<div id="page"></div>
<script type="text/javascript">
$(document).ready(function() {
var originalData = {
tb1: {
label: "Bar 1",
value: 50
},
tb2: {
label: "Bar 2",
value: 20
},
tb3: {
label: "Bar 3",
value: 90
}
};
$('#page').liveGraph({
height : 350,
barWidth : 100,
barGapSize : 2,
data : originalData
});
});
</script>
</body>
</html>
Now we are almost done we just need to create a report to run the bsp application. It is possible to execute the bsp application directly but it open the bsp application in the internet explorer, but we have to run the bsp application in chrome browser. so make sure that your default browser is chrome.
Creating a report to run the bsp application:
DATA: lv_url TYPE string,
lv_urlc(4096) TYPE c,
lt_parms TYPE tihttpnvp.
*-- Create URL to BSP Application
CALL METHOD cl_http_ext_webapp=>create_url_for_bsp_application
EXPORTING
bsp_application = 'ZAPC_TEST'
bsp_start_page = 'START.HTM'
bsp_start_parameters = lt_parms
IMPORTING
abs_url = lv_url.
*-- Call the BSP Application in the default Browser
lv_urlc = lv_url.
CALL FUNCTION 'CALL_BROWSER'
EXPORTING
url = lv_urlc
window_name = 'BSP'
new_window = ' '
EXCEPTIONS
frontend_not_supported = 1
frontend_error = 2
prog_not_found = 3
no_batch = 4
unspecified_error = 5
OTHERS = 6.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
I hope that helps. I'm open for your comments, suggestions, question and all. Please leave your comments.
Thanks,
Santosh.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
3 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |