Skip to Content

Introduction

In the weblog i am going to present JAVA code to broadcast messages to users who are logged on to portal. This is very useful for broadcasting critical system messages to user.

image

JAR files required:


prtcoreservice.jar

Steps to create the component.

Step 1

Build an empty project ( i am calling it com.ust.broadcast). Create a portal service called “alertreceiver” which

implements
ITopicListener

using the NWDS or eclipse wizard. This is the service that is going to capture all the alerts that are broadcasted by system administrators.

This is what your alertreceiver.java and Ialertreceiver.java should look like. Copy the following code to your project

alertreceiver.java

/* Built By: Prakash Singh

  • Technical Consultant

  • Universal System Technologies, Inc

  • 2500 W. Lake Mary Blvd., Ste 212-A

  • Lake Mary, FL 32746 USA

  • M 407-474-2216

*/

package com.ust.broadcast;

import java.util.Enumeration;

import java.util.Timer;

import java.util.TimerTask;

import com.sapportals.portal.prt.runtime.PortalRuntime;

import com.sapportals.portal.prt.service.IServiceContext;

import com.sapportals.portal.prt.service.notification.INotificationService;

import com.sapportals.portal.prt.service.notification.ITopicListener;

import com.sapportals.portal.prt.service.notification.TopicData;

import com.sapportals.portal.prt.service.notification.TopicDataContainer;

public class alertreceiver implements Ialertreceiver, ITopicListener {

     private IServiceContext mm_serviceContext;

     private INotificationService notificationService;

     private final static String TOPIC_NAME = “USTBroadCastAlerts”;

     public String alertmessage =””;

     public String userid =””;

     private Timer timer;

     /**

     

  • Generic init method of the service. Will be called by the portal runtime.

     

  • @param serviceContext

     */

     public void init(IServiceContext serviceContext) {

          mm_serviceContext = serviceContext;

          this.subscribe();

     }

     /**

     

  • This method is called after all services in the portal runtime

     

  • have already been initialized.

     */

     public void afterInit() {

     }

     /**

     

  • configure the service

     

  • @param configuration

     

  • @deprecated

     */

     public void configure(

          com

               .sapportals

               .portal

               .prt

               .service

               .IServiceConfiguration configuration) {

     }

     /**

     

  • This method is called by the portal runtime

     

  • when the service is destroyed.

     */

     public void destroy() {

          timer = null;

     }

     /**

     

  • This method is called by the portal runtime

     

  • when the service is released.

     

  • @deprecated

     */

     public void release() {

     }

     /**

     

  • @return the context of the service, which was previously set

     

  • by the portal runtime

     */

     public IServiceContext getContext() {

          return mm_serviceContext;

     }

     

     public String GetMessage() {

          return alertmessage;

     }

     

     public String GetUserid() {

               return userid;

          }

     /**

     

  • This method should return a string that is unique to this service amongst all

     

  • other services deployed in the portal runtime.

     

  • @return a unique key of the service

     */

     public String getKey() {

          return KEY;

     }

     

     public void subscribe() {

            INotificationService notService = getNotificationInstance();

            notService.subscribe(TOPIC_NAME, this);

          }

     private INotificationService getNotificationInstance() {

          if (notificationService == null)

               notificationService =

                    (INotificationService) PortalRuntime

                         .getRuntimeResources()

                         .getService(

                         “com.sap.portal.runtime.system.notification.notification”);

          return notificationService;

     }

     

     public void handleTopic(TopicDataContainer topicDataContainer) {

            if ( timer != null)

            {

              timer.cancel();

              timer = null;

            }

            Enumeration enum = topicDataContainer.getTopicDataKeys();

            if (enum.hasMoreElements()) {

               timer = new Timer();

               TopicData topicData = topicDataContainer.getTopicDataValue(“duration”);

               Long duration = new Long(topicData.getTopicValue().trim());

               topicData = topicDataContainer.getTopicDataValue(“message”);

               alertmessage = topicData.getTopicValue();

               System.out.println(alertmessage);

               topicData = topicDataContainer.getTopicDataValue(“user”);

               userid = topicData.getTopicValue();

               System.out.println(userid);

               timer.schedule(new MessageClear(),duration.longValue()*1000);

                    

            }

       }

      

     public class MessageClear extends TimerTask {

             /* (non-Javadoc)

               

  • @see java.util.TimerTask#run()

               */

             public void run() {

                  alertmessage = “”;

                  userid =””;

               

             }

     }

Ialertreceiver.java

/* Built By: Prakash Singh

  • Technical Consultant

  • Universal System Technologies, Inc

  • 2500 W. Lake Mary Blvd., Ste 212-A

  • Lake Mary, FL 32746 USA

  • M 407-474-2216

*/

package com.ust.broadcast;

import com.sapportals.portal.prt.service.IService;

public interface Ialertreceiver extends IService

{

    public static final String KEY = “com.ust.broadcast.alertreceiver”;

     public String GetMessage();

     public String GetUserid();

}

Step 2

In the same project (com.ust.broadcast) build a DynPage component to publish messages to the user. Use the DynPage wizard to create component

publishmessage

. The code should look like following(cut and paste).

/* Built By: Prakash Singh

  • Technical Consultant

  • Universal System Technologies, Inc

  • 2500 W. Lake Mary Blvd., Ste 212-A

  • Lake Mary, FL 32746 USA

  • M 407-474-2216

*/

package com.ust.broadcast;

import java.io.ByteArrayInputStream;

import com.sap.security.api.IUser;

import com.sapportals.htmlb.Button;

import com.sapportals.htmlb.Form;

import com.sapportals.htmlb.GridLayout;

import com.sapportals.htmlb.Group;

import com.sapportals.htmlb.InputField;

import com.sapportals.htmlb.Label;

import com.sapportals.htmlb.TextEdit;

import com.sapportals.htmlb.TextView;

import com.sapportals.htmlb.enum.GroupDesign;

import com.sapportals.htmlb.event.Event;

import com.sapportals.htmlb.page.DynPage;

import com.sapportals.htmlb.page.PageException;

import com.sapportals.portal.htmlb.page.PageProcessorComponent;

import com.sapportals.portal.prt.component.IPortalComponentRequest;

import com.sapportals.portal.prt.runtime.PortalRuntime;

import com.sapportals.portal.prt.service.notification.INotificationService;

import com.sapportals.portal.prt.service.notification.StreamData;

import com.sapportals.portal.prt.service.notification.TopicData;

import com.sapportals.portal.prt.service.notification.TopicDataContainer;

public class publishmessage extends PageProcessorComponent {

     public DynPage getPage() {

          return new publishPage();

     }

     public static class publishPage extends DynPage {

          private final static int INITIAL_STATE = 0;

          private final static int PUBLISH_STATE = 1;

          private int state = INITIAL_STATE;

          private final static String TOPIC_NAME = “USTBroadCastAlerts”;

          private static String message = “”;

          private static String duration = “”;

          public void doInitialization() {

               // setup the bean which contains all state information

          }

          public void doProcessAfterInput() throws PageException {

          }

          public void doProcessBeforeOutput() throws PageException {

               IPortalComponentRequest request =

                    (IPortalComponentRequest) this.getRequest();

               IUser user = request.getUser();

               Form myForm = getForm();

               Group myGroup = new Group();

               myGroup.setWidth(“350”);

               myGroup.setDesign(GroupDesign.SAPCOLOR);

               GridLayout gl = new GridLayout();

               myGroup.addComponent(gl);

               switch (state) {

                    case PUBLISH_STATE :

                         myGroup.setTitle(“Broadcast”);

                         //get the notification service

                         INotificationService notService =

                              (INotificationService) PortalRuntime

                                   .getRuntimeResources()

                                   .getService(

                                   “com.sap.portal.runtime.system.notification.notification”);

                         //Define a data container with information that will be send to the receiver

                         TopicDataContainer container =

                              new TopicDataContainer(TOPIC_NAME);

                         // add a message

                       StreamData  messageStream = new StreamData(new ByteArrayInputStream(message.getBytes()));

                         TopicData durationdata =

                              new TopicData(TopicDataContainer.STRING, duration);

                         TopicData userdata =

                              new TopicData(TopicDataContainer.STRING, user.getUniqueName());

                         container.addTopicData(“message”, messageStream);

                         container.addTopicData(“duration”, duration);

                         container.addTopicData(“user”, userdata);

                         notService.publish(TOPIC_NAME, container);

                         TextView success =

                              new TextView(“Message was succesfully broadcasted.”);

                         gl.addComponent(1, 1, success);

                         Button buttonback = new Button(“Back”);

                         buttonback.setText(“Back”);

                         buttonback.setOnClick(“back”);

                         gl.addComponent(2, 1, buttonback);

                         break;

                    case INITIAL_STATE :

                        myGroup.setTitle(“Broadcast Message”);

                         Label lblmessage = new Label(“Message to be broadcasted”);

                         lblmessage.setRequired(true);

                         lblmessage.setLabeledComponentId(“Message”);

                         gl.addComponent(1, 1, lblmessage);

                         TextEdit message = new TextEdit(“Message”);

                         message.setId(“Message”);

                         message.setCols(50);

                         message.setRows(5);

                         gl.addComponent(2, 1, message);

                         Label lblduration = new Label(“Duration(seconds)”);

                         lblduration.setRequired(true);

                         lblduration.setLabeledComponentId(“Duration”);

                         gl.addComponent(3, 1, lblduration);

                         InputField duration = new InputField(“Duration”);

                         duration.setId(“Duration”);

                         duration.setSize(5);

                         duration.setMaxlength(4);

                         gl.addComponent(4, 1, duration);

                         Button button = new Button(“Publish”);

                         button.setText(“Publish”);

                         button.setOnClick(“publish”);

                         gl.addComponent(5, 1, button);

                    default :

                         break;

               }

               myForm.addComponent(myGroup);

          }

          public void onPublish(Event event) throws PageException {

               state = PUBLISH_STATE;

               TextEdit messagefld = (TextEdit) getComponentByName(“Message”);

               InputField durationfld =

                    (InputField) getComponentByName(“Duration”);

               message = messagefld.getText();

               duration = durationfld.getValueAsDataType().toString();

          }

          public void onBack(Event event) throws PageException {

               state = INITIAL_STATE;

          }

     }

}

Step 3

In the same project (com.ust.broadcast) build a JSPDynPage component that gets alert and displays it as a popup by calling a dynpage component(defined in step4). Use the JSPDynPage wizard to create component called

display

. The code should look like following(cut and paste).

Display.Java

/* Built By: Prakash Singh

  • Technical Consultant

  • Universal System Technologies, Inc

  • 2500 W. Lake Mary Blvd., Ste 212-A

  • Lake Mary, FL 32746 USA

  • M 407-474-2216

*/

package com.ust.broadcast;

import com.sapportals.htmlb.page.DynPage;

import com.sapportals.htmlb.page.PageException;

import com.sapportals.portal.htmlb.page.JSPDynPage;

import com.sapportals.portal.htmlb.page.PageProcessorComponent;

import com.sapportals.portal.prt.component.IPortalComponentContext;

import com.sapportals.portal.prt.component.IPortalComponentRequest;

import com.sapportals.portal.prt.component.IPortalComponentURI;

import com.sapportals.portal.prt.runtime.PortalRuntime;

import com.ust.broadcast.bean.alertinfo;

//import bean.ContextBean;

public class display extends PageProcessorComponent {

     public DynPage getPage() {

          return new displayDynPage();

     }

     public static class displayDynPage extends JSPDynPage {

          private alertinfo bean;

          private final static String BEAN_KEY = “myBean”;

          public void doInitialization() {

               bean = null;

               IPortalComponentRequest request =

                    (IPortalComponentRequest) this.getRequest();

               IPortalComponentContext context = request.getComponentContext();

               IPortalComponentURI uri = request.createPortalComponentURI();

               uri.setContextName(“com.ust.broadcast.popup”);

               

               Ialertreceiver receiver =

                    (Ialertreceiver) PortalRuntime

                         .getRuntimeResources()

                         .getService(

                         Ialertreceiver.KEY);

               if (!receiver.GetMessage().equals(“”)) {

                    receiver.GetMessage();

               }

               Object o = context.getValue(BEAN_KEY);

               if (o instanceof alertinfo) {

                    bean = (alertinfo) o;

               }

               if (bean == null) {

                    // no bean or probably wrong class.

                    // We simply instantiate a new bean and put

                    // it into the session

                    bean = new alertinfo();

               }

               bean.url = uri.toString();

               if (!receiver.GetMessage().equals(“”))

                    bean.no_message = “false”;

               else

                    bean.no_message = “true”;

     

               context.putValue(BEAN_KEY, bean);

          }

          public void doProcessAfterInput() throws PageException {

          }

          public void doProcessBeforeOutput() throws PageException {

               this.setJspName(“displayalert.jsp”);

          }

     }

}

/* Built By: Prakash Singh

  • Technical Consultant

  • Universal System Technologies, Inc

  • 2500 W. Lake Mary Blvd., Ste 212-A

  • Lake Mary, FL 32746 USA

  • M 407-474-2216

*/

package com.ust.broadcast.bean;

public class alertinfo

{

     public String url;

     public String no_message;

          

     public String geturl() {

          return url;

     }

     

     public String no_message() {

          return no_message;

     }

  

}

Step 4

In the same project (com.ust.broadcast) build a DynPage component called

popupup

that displays the message

Source code for popup.java. (Cut and Paste)

/* Built By: Prakash Singh

  • Technical Consultant

  • Universal System Technologies, Inc

  • 2500 W. Lake Mary Blvd., Ste 212-A

  • Lake Mary, FL 32746 USA

  • M 407-474-2216

*/

package com.ust.broadcast;

import com.sapportals.htmlb.Form;

import com.sapportals.htmlb.GridLayout;

import com.sapportals.htmlb.Group;

import com.sapportals.htmlb.Label;

import com.sapportals.htmlb.TextEdit;

import com.sapportals.htmlb.TextView;

import com.sapportals.htmlb.enum.GroupDesign;

import com.sapportals.htmlb.enum.TextViewDesign;

import com.sapportals.htmlb.page.DynPage;

import com.sapportals.htmlb.page.PageException;

import com.sapportals.portal.htmlb.page.PageProcessorComponent;

import com.sapportals.portal.prt.runtime.PortalRuntime;

public class popup extends PageProcessorComponent {

     public DynPage getPage() {

          return new popupDynPage();

     }

     public static class popupDynPage extends DynPage {

          /**

          

  • Initialization code executed once per user.

           */

          public void doInitialization() {

          }

          /**

          

  • Input handling code. In general called the first time with the second page request from the user.

           */

          public void doProcessAfterInput() throws PageException {

          }

          /**

          

  • Create output. Called once per request.

           */

          public void doProcessBeforeOutput() throws PageException {

               Form myForm = this.getForm(); // get the form from DynPage

               Ialertreceiver receiver =

                    (Ialertreceiver) PortalRuntime

                         .getRuntimeResources()

                         .getService(

                         Ialertreceiver.KEY);

               Group myGroup = new Group();

               myGroup.setTitle(“System Message”);

               myGroup.setWidth(“450”);

               myGroup.setDesign(GroupDesign.SAPCOLOR);

               GridLayout gl = new GridLayout();

               myGroup.addComponent(gl);

               Label lblauthor = new Label(“Author”);

            gl.addComponent(1,1,lblauthor);

            TextView author = new TextView(“Author”);

            author.setText(receiver.GetUserid());

            author.setDesign(TextViewDesign.HEADER2);

            gl.addComponent(2,1,author);

               Label lblmessage = new Label(“Message”);

               gl.addComponent(3,1,lblmessage);

               TextEdit alert = new TextEdit(“message”);

               alert.setCols(80);

               alert.setRows(6);

               alert.setText(receiver.GetMessage());

               gl.addComponent(4,1,alert);

               myForm.addComponent(myGroup);

               // create your GUI here….

          }

     }

}

image

The setup of this component in portal is explained in the part2 of this weblog

To report this post you need to login first.

39 Comments

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

      1. Krishna ch
        Dear Prakash,
        you had published  wonderful blog on Broad casting messages almost 5 years ago.till now I haven’t seen any updated blog on SDN,it shows your unique talent. I got a req to send messages to portal users about system updates.

        I tried to implement your code and  was able to deploy your code but not able to get display iview working.even popup is receiving the data from Publish view but some how the comunication between Display and popup is mising.
        was it probelm with Portal 7.0?.
        Could you please send me Jar file or any hint for fixing this.

        Thanks
        Gopi

        (0) 
  1. Good work Prakash. Thanks for sharing it, am going to try.

    I am just curious to know whether this code can be customised further so that portal users can chat with another user in the portal.

    (0) 
    1. Prakash Singh Post author
      Hi Rem,
          Thank you so much. My current solution is not truely realtime because it captures broadcasted messages only when the user navigates between pages. You can add a server side timer component(that looks for message after some interval) to make it real time. You can extend this to do chat as well but SAP already provides very good chat tool via Collaboration.
      Prakash Singh
      (0) 
      1. Raj Joshi
        Prakash,

        Could yo uplease send me the .par file. We are planning to use the pop up broadcast message for our server at DOI.

        Thanks,
        Raj

        (0) 
    1. Prakash Singh Post author
      Hi Jorge,
          Thank you for the feedback. Yes, you can  modify the code to send it a particular group. If enough people generate interest , i will modify my code and post it as weblog.
      Prakash Singh
      (0) 
  2. Michael Scholz
    Hi,
    great work prakash.
    I’m trying to use it, but all ‘import’s
    with ‘notification’ (com.sapportals.portal.prt.service.notification…) can’t be solved.
    I’m using Portal from NW04 SP10,
    any idea, where I can get these ?
    (0) 
    1. Prakash Singh Post author
      Hi Michael,
         Thank you for the feedback. The class is  inside prtcoreservice.jar . Search under your portal install directory. If you don’t find it then email me at psingh@ust.net. I will send it to you.
      (0) 
      1. Michael Scholz
        Hi Prakash,
        thanks for your reply, I could find the class and deploy the project. I created the IViews and modified the Desktop Framework as described. But if I want to run the ‘publish’ IView, I get an error message. In the Log File I can see messages ‘error in init method’ and ‘java.lang.ClassNotFoundException’. Do you have any idea ?
        Regards
        Michael Scholz
        (0) 
    2. Radhika Kuthiala
      Hi,
      iam trying to make the application but  but all ‘import’s
      with ‘notification’ (com.sapportals.portal.prt.service.notification…) can’t be solved.
      Iv got the jar ptcoreservice,but do not know how to get classes.
      (0) 
      1. Brian Lane
        Hi Prakash,
        I have been looking into Portal messenging and have come across your Blog. It is excellent work. I am not a programmer, sorry, so I am wondering can you send me the par file for your portal broadcast messages that will work on NW2004s?
        Regards
        Brian Lane
        (0) 
  3. Nilesh Patil Gayakwad
    Hi Prakash,

    Nice work. I have some question? If i want to make it realtime, what I’ll need to do? You suggested to use a Server Side Timer Component, but can you please explain how to use it?

    Regards,
    Nilz

    (0) 
  4. Stefan Kuster
    Hi, followed this blog part for part.

    Generelly it’s working but I have two strange behaviors.

    1. Popup ist not popping up. I made a popup-iview. Is this correct? When I add this popup-iview to the default framework it shows me the broadcastet texts. But it’s not popping up.

    2. The normal display-iview isn’t displaying anything.

    Could you help me? What seems to be wrong?

    Regards,
    Stefan

    (0) 
    1. Sonal Mangla
      Hi Stefan

      Nothing is wrong. Just you made a small mistake.
      You need to build iView on Popup Component.
      You have to make the iView on Display Component.

      Display component automatically calls Popup component to generate the alert.

      Put the display iView in Desktop Innerpage

      Hope this solves your problem

      Regards
      Sonal Mangla

      (0) 
  5. Sonal Mangla
    Hi Prakash
    That was wonderfully framed.
    But if we put the Display iView in Desktop Innerpage, everytime user clicks on any workset the popup appears.
    If I want the popup just after login only, what should be done.

    Thanks in advance

    Regards
    Sonal Mangla

    (0) 
  6. kumar D
    This is Excelent weblog.When i’m trying to use this,I got error when deploying popup component.we’re using EP 7.0.Is there any changes for the ep 7.0.
    If posible send par file to mymail kumar.portal@k.st
    Thanks,
    Kumar
    (0) 
  7. Swathi Rao Padma
    It doesnt seem to work in EP7.0.
    I have followed the same and deployed it in EP7.0.
    But i dont get the result and any popup ,neither i get any exceptions.

    I doesnt seem to respond.I just see a displayview wher i can type in..but no popup..

    So anyone please tell ,,will it work in EP7.0 ??\

    Swathi

    (0) 
  8. Praveen Saini
    Hi Prakash,

    I tried to implement your application for broadcast messages.
    After successfull deployment when I logged into portal it gives runtime exception.

    Below is the exception details from log & trace :

    Caused by: com.sapportals.portal.prt.core.broker.PortalComponentInstantiationException: Could not instantiate implementation class com.MessageBroadcast.display of Portal Component

    Caused by: com.sapportals.portal.prt.core.broker.PortalApplicationNotFoundException: Could not find portal application com.MessageBroadcast

    I am using EP 7.01 & NWDS 7.0.17

    Please suggest, how to resolve this.

    Thanks,
    Praveen

    (0) 
  9. Michael Voss
    Hello!

    I loved to read about the possibility of sending messages to all logged on users, but currently I’m not able to follow the steps describes in this blog. We’re using NWDS 7.0.17, an I’m not sure what you mean with a “portal service”. I cannot find such a project or DC type in my NWDS, as well as I do not know what you mean with a DynPage or a JSPDynPage. Can you help me in translating that into my somewhat wellknown world of NWDS 7.0 ?

    Thanks a lot!

    (0) 
  10. Mohammed Shahahmad

    Hello,

    I’m try to implement this blog in our system could you please explain step-by-step how to :

    1- Create a portal service called “alertreceiver” which  

    implements
    ITopicListener

    using the NWDS

    2- Down load the

       JAR files required:

      prtcoreservice.jar

    or if you share the PAR file I would be very thankfull.

    mohamed373@gmail.com

    Regards,

    (0) 
  11. Vijay Budati

    Hi Prakash,

    Great Blog and thanks for sharing it with the community.

    Could you please send me the entire PAR file that I can deploy in our portal system and test it? Please email to blvijaykumar @ gmail.com    

    (0) 

Leave a Reply