0 Replies Latest reply on Apr 4, 2003 2:41 PM by Alexander Rabinowitz

    Why is not my persistence implementation working correctly?

    Alexander Rabinowitz Newbie

      I am working with JBoss 3.0.6

      I am working with model mbeans, and I am trying to implement my own persistance.

      My main class PulsarStandard () constructor says
      persistence = new ModelPersistanceSupport ();
      which is supposed to be my own persistance class.

      All my descriptors set persistantPolicy "OnUpdate".

      My load () and store () methods contain debug printout which is never printed - that means they are never called, even when I update my mbean via HTTP adapter (call methods stopPulse or startPulse or change the value of PulsePeriodinMills).

      here is my console output. As you see, it does not print "load" or "store".
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      C:\build\GUI2\ram\run_agent>java -cp .;PeerdirectAgent.jar;PeerdirectJmx.jar;Pee
      rdirectMasterServer.jar;sonic_client.jar;sonic_xa.jar;jboss-jmx.jar;jboss-jmx.ja
      r;jmxtools.jar;jbossall-client.jar;jboss-j2ee.jar;jnp-client.jar;jnet.jar;log4j.
      jar com.peerdirect.rmapplications.agent.MasterAgent

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      Creating an instance of MasterAgent...
      getMBeanInfo
      PulsarStandard
      isSupportedResourceType
      startPulse
      run
      Instantiating an HTML protocol adaptor with default port...
      Instantiating an RMI connector server with default port...
      Instantiating a JMS connector server with default port...

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      Now, you can point your browser to http://ARABINOW:8082/
      to view this agent through the HTML protocol adaptor.

      Or, in another window, start a client application that connects
      to this agent by using an RMI connector client

      Press to stop the agent...

      getAttribute
      getAttribute
      setAttribute
      getAttribute
      getAttribute
      invoke
      stopPulse
      getAttribute
      getAttribute
      invoke
      stopPulse
      getAttribute
      getAttribute
      invoke
      startPulse
      invoke
      startPulse
      invoke
      startPulse
      invoke
      startPulse
      invoke
      startPulse
      invoke
      startPulse
      invoke
      startPulse
      getAttribute
      getAttribute
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      Here is my class ModelPersistanceSupport.java (for persistance support).
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      package com.peerdirect.rmapplications.master.jmx;

      import javax.management.modelmbean.*;
      import java.io.*;
      import org.jboss.mx.persistence.PersistenceManager;
      import javax.management.MBeanInfo;
      import javax.management.*;
      import org.jboss.mx.modelmbean.*;

      public class ModelPersistanceSupport implements PersistenceManager
      {
      // PersistentMBean interface
      public void load (MBeanInfo metadata)
      {
      System.out.println ("load");
      // throw new UnsupportedOperationException();
      if (metadata == null)
      return;
      System.out.println ("load1");
      try
      {
      Descriptor d = ((ModelMBeanInfo)metadata).getMBeanDescriptor();
      String dir = (String) d.getFieldValue(ModelMBeanConstants.PERSIST_LOCATION);
      String file = (String) d.getFieldValue(ModelMBeanConstants.PERSIST_NAME);
      System.out.println ("load2");
      if (file != null)
      {
      System.out.println ("load3");
      File f = new File(dir, file);
      FileInputStream fis = new FileInputStream(f);
      ObjectInputStream ois = new ObjectInputStream(fis);
      metadata = (ModelMBeanInfoSupport) ois.readObject();
      System.out.println ("load4");
      }
      }
      catch (Exception e)
      {
      e.printStackTrace();
      System.out.println("Error loading MBean state");
      }
      }

      public void store (MBeanInfo metadata)
      {
      try
      {
      System.out.println ("store");
      Descriptor d = ((ModelMBeanInfo)metadata).getMBeanDescriptor();
      String dir = (String) d.getFieldValue(ModelMBeanConstants.PERSIST_LOCATION);
      String file = (String) d.getFieldValue(ModelMBeanConstants.PERSIST_NAME);

      File f = new File(dir, file);
      FileOutputStream fos = new FileOutputStream(f);
      ObjectOutputStream oos = new ObjectOutputStream(fos);

      oos.writeObject(metadata);
      System.out.println ("store1");
      }
      catch (Exception e)
      {
      e.printStackTrace();
      System.out.println ("Error in persisting MBean.");
      }
      //throw new UnsupportedOperationException();
      }
      }
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      My main class PulsarStandard.java looks like the following. It perfectly works via http adaptor, just does not support persistence
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      package com.peerdirect.rmapplications.master.jmx;

      import com.peerdirect.rmapplications.master.jms.StatusNotificationClient;

      import javax.management.*;
      import javax.management.modelmbean.*;
      import org.jboss.mx.modelmbean.*;

      public class PulsarStandard extends ModelBase
      implements Runnable
      {
      // support for generic notification listeners
      private NotificationBroadcasterSupport notifier = new NotificationBroadcasterSupport();

      // sequence numbers for notifications
      protected long notifierSequence = 0;
      protected long attrNotifierSequence = 0;

      // mgmt attribute name constants
      final private static String PULSE_PERIOD = "PulsePeriodInMills";
      final private static String IF_STARTED = "Started";

      // mgmt operation name constants
      final private static String START_PULSE = "startPulse";
      final private static String STOP_PULSE = "stopPulse";

      // fields for attribute values
      final private static int DEFAULT_PULSE_PERIOD = 1000;
      final private static boolean DEFAULT_STARTED= false;

      private int mPulsePeriod = 0;
      private boolean mStarted = false;
      private StatusNotificationClient mSNC = null;
      private Thread mThread = null;
      private static final String DIRECTORY = "c:\\aa";

      public boolean isSupportedResourceType(String resourceType)
      {
      System.out.println("isSupportedResourceType");
      return true;
      }

      // getAttribute implementation

      public PulsarStandard () throws MBeanException
      {
      super (getModelMBeanInfo());
      System.out.println ("PulsarStandard");
      persistence = new ModelPersistanceSupport ();
      try
      {
      setManagedResource (this, ModelMBeanConstants.OBJECT_REF);
      }
      catch (Exception e)
      {
      e.printStackTrace();
      }
      mPulsePeriod = DEFAULT_PULSE_PERIOD;
      mStarted = DEFAULT_STARTED;
      mSNC = new StatusNotificationClient ();
      mThread = new Thread (this);
      startPulse () ;
      }

      public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException
      {
      System.out.println ("getAttribute");
      if (attribute == null || attribute.equals(""))
      throw new IllegalArgumentException("null or empty attribute name");

      // map the named attributes to fields

      if (attribute.equals(PULSE_PERIOD))
      return new Integer (mPulsePeriod);
      else if (attribute.equals(IF_STARTED))
      return new Boolean (mStarted);

      throw new AttributeNotFoundException("Attribute " + attribute + " not found.");
      }

      // setAttribute implementation

      public void setAttribute (Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException,
      MBeanException, ReflectionException
      {
      System.out.println ("setAttribute");
      if (attribute == null) throw new
      AttributeNotFoundException("null attribute");

      // map attributes to fields
      try
      {
      if (attribute.getName().equals(PULSE_PERIOD))
      this.mPulsePeriod = ((Integer)attribute.getValue()).intValue();
      else if (attribute.getName().equals(IF_STARTED))
      this.mStarted = ((Boolean)attribute.getValue()).booleanValue();
      else
      throw new AttributeNotFoundException("attribute " + attribute.getName() + " not found.");
      }
      catch (ClassCastException e)
      {
      throw new InvalidAttributeValueException("Invalid attribute type " + attribute.getValue().getClass().getName());
      }
      }

      // invoke implementation

      public Object invoke(String actionName,
      Object[] params,
      String[] signature) throws MBeanException, ReflectionException {
      System.out.println ("invoke");
      if (actionName == null || actionName.equals(""))
      throw new IllegalArgumentException("no operation");

      // map operation names to methods
      if (actionName.equals(START_PULSE))
      {
      startPulse();
      return null;
      }
      else if (actionName.equals(STOP_PULSE))
      {
      stopPulse ();
      return null;
      }
      else
      throw new UnsupportedOperationException("unknown operation " + actionName);
      }


      // getMBeanInfo implementation
      public MBeanInfo getMBeanInfo()
      {
      return info;
      }

      public static ModelMBeanInfo getModelMBeanInfo()
      {
      System.out.println ("getMBeanInfo");

      // MBean class and description
      //String className = getClass().getName();
      String className = PulsarStandard.class.getName();
      String description =
      "User resource with model management interface";

      // build 'PulsePeriod' read-write attribute

      ModelDescriptorSupport descrStartPulseAttribute =
      new ModelDescriptorSupport (PULSE_PERIOD,
      ModelMBeanConstants.ATTRIBUTE_DESCRIPTOR,
      PULSE_PERIOD,
      ModelMBeanConstants.ON_UPDATE,
      DIRECTORY,
      className + ".txt");

      // meta data for 'PULSE_PERIOD' attribute.
      ModelMBeanAttributeInfo mPulsePeriod = new ModelMBeanAttributeInfo(
      PULSE_PERIOD, // name
      int.class.getName(), // type
      "Pulse Period", // description
      ModelMBeanConstants.IS_READABLE, ModelMBeanConstants.IS_WRITABLE, !ModelMBeanConstants.IS_IS, // access
      descrStartPulseAttribute
      );

      ModelDescriptorSupport descrIfStartedAttribute =
      new ModelDescriptorSupport (IF_STARTED,
      ModelMBeanConstants.ATTRIBUTE_DESCRIPTOR,
      IF_STARTED,
      ModelMBeanConstants.ON_UPDATE,
      DIRECTORY,
      className + ".txt");

      // meta data for 'IF_STARTED' attribute.
      ModelMBeanAttributeInfo mStarted = new ModelMBeanAttributeInfo(
      IF_STARTED, // name
      boolean.class.getName(), // type
      "If the Pulser has Started", // description
      ModelMBeanConstants.IS_READABLE, !ModelMBeanConstants.IS_WRITABLE, !ModelMBeanConstants.IS_IS, // access
      descrIfStartedAttribute
      );

      ModelDescriptorSupport descrStartPulseOperation =
      new ModelDescriptorSupport (START_PULSE,
      ModelMBeanConstants.OPERATION_DESCRIPTOR,
      START_PULSE,
      ModelMBeanConstants.ON_UPDATE,
      DIRECTORY,
      className + ".txt");

      // meta data for 'startPulse' operation
      ModelMBeanOperationInfo startPulse = new ModelMBeanOperationInfo(
      START_PULSE, // name
      "Start the pulsar", // description
      null, // signature
      void.class.getName(), // return type
      MBeanOperationInfo.ACTION, // impact
      descrStartPulseOperation
      );

      ModelDescriptorSupport descrStopPulseOperation =
      new ModelDescriptorSupport (STOP_PULSE,
      ModelMBeanConstants.OPERATION_DESCRIPTOR,
      STOP_PULSE,
      ModelMBeanConstants.ON_UPDATE,
      DIRECTORY,
      className + ".txt");

      // meta data for 'stopPulse' operation
      ModelMBeanOperationInfo stopPulse = new ModelMBeanOperationInfo(
      STOP_PULSE, // name
      "Stop the pulsar", // description
      null, // signature
      void.class.getName(), // return type
      MBeanOperationInfo.ACTION, // impact
      descrStopPulseOperation
      );

      // mbean constructors
      ModelMBeanConstructorInfo defaultConstructor =
      new ModelMBeanConstructorInfo(
      "Default Constructor",
      "Creates a new Pulsar", null
      );

      // attribute, constructor and operation lists
      ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[] {mPulsePeriod, mStarted};

      ModelMBeanConstructorInfo[] constructors = new ModelMBeanConstructorInfo[] {defaultConstructor};

      ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[] {startPulse, stopPulse};

      // return the MBeanInfo
      return new ModelMBeanInfoSupport (className, description, attributes, constructors, operations, null);
      }

      public void startPulse ()
      {
      System.out.println ("startPulse");
      mThread.start ();
      try
      {
      mSNC.connect();
      mStarted = true ;
      }
      catch (Exception e)
      {
      mStarted = false ;
      }
      }

      public void stopPulse ()
      {
      System.out.println ("stopPulse");
      mThread.stop ();
      mSNC.disconnect();
      mStarted = false ;
      }

      public void run ()
      {
      System.out.println ("run");
      try
      {
      while (true)
      {
      mThread.sleep (mPulsePeriod);
      if (mStarted)
      {
      //System.out.println("Pulsing");
      mSNC.sendPulse();
      }
      }
      }
      catch (Exception ex)
      {
      ex.printStackTrace();
      }
      }
      }
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      My helper class ModelDescriptorSupport.java looks in the following way. It just creates a descriptor, and sets persistPolicy from the constructor.
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      package com.peerdirect.rmapplications.master.jmx;

      import javax.management.modelmbean.*;
      import org.jboss.mx.modelmbean.*;

      public class ModelDescriptorSupport extends DescriptorSupport
      {
      public ModelDescriptorSupport (String name,
      String descriptorType,
      String displayName,
      String persistPolicy,
      String persistLocation,
      String persistName)
      {
      super ();
      setField (ModelMBeanConstants.NAME, name);
      setField (ModelMBeanConstants.DESCRIPTOR_TYPE, descriptorType);
      setField (ModelMBeanConstants.DISPLAY_NAME, displayName);
      setField (ModelMBeanConstants.PERSIST_POLICY, persistPolicy);
      setField (ModelMBeanConstants.PERSIST_LOCATION, persistLocation);
      setField (ModelMBeanConstants.PERSIST_NAME, persistName);
      if (descriptorType.equals(ModelMBeanConstants.OPERATION_DESCRIPTOR))
      setField (ModelMBeanConstants.ROLE, ModelMBeanConstants.OPERATION_DESCRIPTOR);
      }
      }
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////