1 Reply Latest reply on Sep 6, 2006 12:12 PM by Antoine Herzog

    Sharing a data among different browser instances

    Krishan Fernando Newbie

      Hi,

      I am using JBoss portal 2.4,

      I have defined a singleton class as follows and copied into the "jboss/server/default/lib" to be used for all the other portlets,

      package testcom;

      public class Communication {
      private static Communication instance = null;
      private static String sName = "Krishan";

      private Communication(String sName)
      {
      this.sName =sName;
      }

      public String getName()
      {
      return this.sName;
      }

      public void setName(String pName)
      {
      this.sName = pName;
      }

      public static Communication getInstance(String pCaller) {
      if(instance == null) {
      String sureName ="Fernando";
      instance = new Communication(sureName);
      System.out.println("********************************instance is null");
      }

      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~pCaller::sName:"+sName+"::"+pCaller);

      return instance;
      }
      }


      In my first senario:

      When I access it for the very first time
      as http://localhost:8080/portal, it created a new "Communication" instance.

      Communication cm = Communication.getInstance("MyPortlet");
      writer.write("cm.getName():"+cm.getName());
      cm.setName("Pradeep");
      writer.write("cm.getName():"+cm.getName());


      I got the Name as I expected:
      cm.getName():Fernando
      cm.getName():Pradeep

      and also when I open another browser instance, I got the following result as expected.
      cm.getName():Pradeep
      cm.getName():Pradeep

      But in my second scenario, When I click on the different portlet Url Tab in the same browser instance which is also suppose to display the cm.getName(),

      it created the new Communication instance and give me the following result,
      cm.getName():Fernando

      But I was expected following results with out getting creating a new Communication instance ;
      cm.getName():Pradeep

      I noticed that the url I clicked is contain with the session id values as follows,

      http://localhost:8080/portal/portal/default/IPC;jsessionid=E5FE54B2057947FC8E77F0D57066F16F

      So could you please tell me how can I get the expected data value without get creating another instance of a Communication object.(I want it to behave as a singleton object)

      Thanks,
      Krishan


        • 1. Re: Sharing a data among different browser instances
          Antoine Herzog Master

          It is because the singleton class is used inside different class loaders.
          so there are actually multiple "singleton" working on the plateform.

          you have to put it in a lib, as you have done, but also make sure to call the instance from the same class loader.

          I would do it using a JMX service of JBoss.
          you build a service with your communication class, and get what you want.
          At least, the service may only be a factory, that provide to any one calling it the same instance of the singleton.
          or more feature as a service.
          look at the JMX documentation and tutorials.
          and, as an example, look at the code of the CMSPortlet and how it uses the CMSService.

          or may be an EJB...

          for inter portlet communication, look also for "IPC" in the forum, wiki, and at the Michelle Osmond web pages and proposed library : it works well and do most of the job.
          I built a JMX service above this library to access it above multiple war and 'isolated' portlets. The reste is done by the librairy.