If you implement the model mbean interface yourself, rather than use the
existingimplementation, then you're in charge of calling load() and store() at
appropriate times -- there's no notion of a container in the JMX spec.

-- Juha



On Wed, 10 Mar 2004, Marco Mistroni wrote:

>  Hi all,
>       To get acquainted with ModelMBean and persistence, I  have
> written
> A very small ModelMBean that implements the PersistenceInterface, and I
> have
> Written dummy load() and store() to see when those two methods are
> called.
> My assumption is that load() was called when the MBean was deployed..
> I built a sar and dropped into the deploy directory.
> However, as I could see from the logs, the load() method is never
> called..
>
> Anyone can help me out?
>
> Here is the code of my ModelMbean, along with its descriptor..
>
> /*
>  * Created on Jun 25, 2003
>  *
>  * To change the template for this generated file go to
>  * Window>Preferences>Java>Code Generation>Code and Comments
>  */
> package com.marco.config;
> import javax.management.*;
> import java.util.Date;
> import java.util.Vector;
> import java.util.Set;
> import java.util.Hashtable;
> import java.util.Properties;
> import javax.management.timer.*;
> import java.lang.reflect.*;
> import org.jboss.mx.util.*;
> import javax.management.modelmbean.*;
> /**
>  * @author mistroni
>  *
>  * To change the template for this generated type comment go to
>  * Window>Preferences>Java>Code Generation>Code and Comments
>  */
> public class MyTimer extends org.jboss.system.ServiceMBeanSupport
> implements ModelMBean
>                                                         {
>
>
>       private MBeanServer server;
>       private ObjectName timer;
>       private int timeout;
>       private int repetitions;
>       protected ModelMBeanInfo modelMBeanInfo = null;
>       protected Hashtable attributes = new Hashtable();
>       protected Hashtable notifications = new Hashtable();
>       protected Hashtable constructors = new Hashtable();
>       protected Hashtable operations = new Hashtable();
>       protected String description = "Description of MBean";
>
>
>       public MyTimer() {
>               System.err.println("MyTimerService MBean created...");
>               try {
>                       addMBeanInfoData();
>
>               } catch(Exception e) {
>                       System.err.println("Got exception in intializing
> MyTimer\n" + e.toString());
>               }
>       }
>
>
>        /** Persistence Interface */
>
>       public void load() throws MBeanException,
> InstanceNotFoundException {
>          System.err.println("************ HERE WE SHOULD LOAD MBEAN
> DATA!");
>         }
>
>         public void store() throws
> MBeanException,InstanceNotFoundException {
>          System.err.println("************  HERE WE SHOULD STORE MBEAN
> DATA!");
>       }
>
>       /** ModelMBean interface */
>
>       public void setModelMBeanInfo(ModelMBeanInfo inModelMBeanInfo)
> throws MBeanException {
>               modelMBeanInfo = inModelMBeanInfo;
>       }
>
>       public void setManagedResource(Object mr, String mr_type) throws
> MBeanException {}
>
>       public ModelMBeanInfo getModelMBeanInfo() {
>               buildDynamicMBeanInfo();
>               return modelMBeanInfo;
>       }
>
>
>       // ModelMBeanBroadcaster interface
>
>       public void addNotificationListener(NotificationListener l,
>                                           NotificationFilter f,
>                                           Object hback) {}
>
>       public void removeNotificationListener(NotificationListener l)
> throws ListenerNotFoundException {}
>
>
>       public void
> addAttributeChangeNotificationListener(NotificationListener l,
>                                                          String
> attributeName,
>                                                          Object hback)
> throws MBeanException {}
>
>       public void
> removeAttributeChangeNotificationListener(NotificationListener l,
>                                                             String
> attributeName) throws MBeanException,
>
> ListenerNotFoundException {}
>
>       public void sendNotification(String message) throws
> MBeanException {}
>
> //    public void sendNotification(Notification notif) throws
> MBeanException {}
>
>       public void
> sendAttributeChangeNotification(AttributeChangeNotification notif)
> throws MBeanException {}
>
>       public void sendAttributeChangeNotification(Attribute oldValue,
> Attribute newValue) throws MBeanException {}
>
>       public MBeanNotificationInfo[] getNotificationInfo() {
>               return new MBeanNotificationInfo[]{};
>       }
>
> //    Here we add informations to be used in MBeanInfo.
>        // we have to add all operations, attributes and notifications
>        // that our MBeab will have.
>        private void addMBeanInfoData() {
>                System.err.println("Adding mbeaninfo data..");
>
>
>                 // Adding operation addNotification
>
>               Descriptor d = new DescriptorSupport();
>               d.setField("name","testPrint");
>               d.setField("descriptorType", "operation");
>               d.setField("role", "operation");
>
>
>               addMBeanOperation("testPrint", null,
>                                  null, null,
>                                  "testPrint", "void",
>                                  MBeanOperationInfo.ACTION,
>                                  d);
>
>
>        }
>
>
>        // Code below is generated automatically by MBeanGenerator.
>        // You may need to modify it according to your needs :-)
>
>        public Object invoke(String method, Object args[],
>                                                 String types[]) throws
> MBeanException, ReflectionException {
>                try {
>                        Class c = this.getClass();
>                        Class sig[] = null;
>                        if(types != null) {
>                                sig = new Class[types.length];
>                                for(int i=0; i < types.length; i++) {
>                                        sig[i] =
> Class.forName(types[i]);
>                                }
>                        }
>                        Method m = c.getDeclaredMethod(method, sig);
>                        Object returnObject = (Object)m.invoke(this,
> args);
>                        return returnObject;
>                } catch(Exception e) {
>                        System.err.println("Error in
> DynamicMBeanSupport.invoke():\n" + e.toString());
>                        return null;
>                }
>        }
>
>
>
>        public Object getAttribute(String name) throws MBeanException,
> ReflectionException {
>                try {
>                        Class c = this.getClass();
>                        Method m = c.getDeclaredMethod("get" + name,
> null);
>                        return m.invoke((Object)this, null);
>                } catch(Exception e) {
>                        System.err.println("Exception in
> DynamicMBeanSupport.getAttribute()\n" + e.toString());
>                        return null;
>                }
>        }
>
>        public void setAttribute(Attribute attribute) throws
> MBeanException, AttributeNotFoundException,
>
> ReflectionException, InvalidAttributeValueException {
>                String fname = attribute.getName();
>                Object fvalue = attribute.getValue();
>                try {
>                        Class c = this.getClass();
>                        String type = getType(fname, false, true);
>                        if(type == null)
>                                throw new
> AttributeNotFoundException(fname);
>
>                        Class[] types =  {Class.forName(type)};
>                        Method m = c.getDeclaredMethod("set" + fname,
> types);
>
>                        Object[] args = {fvalue};
>                        m.invoke((Object)this, args);
>
>                } catch(AttributeNotFoundException e) {
>                        System.err.println("Exception in
> DynamicMBeanSupport.setAttribute()\n" + e.toString());
>                        throw e;
>                } catch(Exception e) {
>                        System.err.println("Exception2 in
> DynamicMBeanSupport.setAttribute()\n" + e.toString());
>
>                }
>         }
>
>        public AttributeList setAttributes(AttributeList attributes) {
>                Attribute[] atts = (Attribute[])attributes.toArray();
>                AttributeList list = new AttributeList();
>                for(int i=0; i < atts.length; i++) {
>                        Attribute a = atts[i];
>                        try {
>                                this.setAttribute(a);
>                        } catch(Exception e) {
>                                System.err.println("Exception in
> DynamicMBeanSupport.setAttributes()\n" + e.toString());
>                        }
>                } // end for
>                return attributes;
>        }
>
>        public AttributeList getAttributes(String[] names) {
>                AttributeList list = new AttributeList();
>                for(int i=0; i < names.length; i++) {
>                        try {
>                                list.add(new Attribute(names[i],
>                                this.getAttribute(names[i])));
>                        } catch(Exception e) {
>                                System.err.println("Exception in
> DynamicMBeanSupport.getAttributes()\n" + e.toString());
>                        }
>                }
>                return list;
>        }
>
>
>
>        protected void addMBeanOperation(String name, String[]
> paramTypes,
>                                        String[] paramNames, String[]
> paramDescs,
>                                         String desc, String rtype, int
> type,Descriptor d) {
>                MBeanParameterInfo[] params = null;
>                if(paramTypes != null) {
>                        params = new
> MBeanParameterInfo[paramTypes.length];
>                        for(int i=0; i < paramTypes.length; i++) {
>                                params[i] = new
> MBeanParameterInfo(paramNames[i], paramTypes[i], paramDescs[i]);
>                        }
>                }
>                operations.put(name, new ModelMBeanOperationInfo(name,
> desc, params, rtype, type,d));
>
>
>        }
>
>        protected void addMBeanAttribute(String fname, String ftype,
>                                         boolean read, boolean write,
> boolean is,
>                                         String desc, Descriptor d) {
>                attributes.put(fname, new
> ModelMBeanAttributeInfo(fname,ftype, desc, read, write, is,d));
>        }
>
>        protected void addMBeanNotification(String type, String name,
> String description,Descriptor d) {
>                notifications.put(name, new
> ModelMBeanNotificationInfo(new String[] {type}, name, description,d));
>        }
>
>        protected void addMBeanConstructor(Constructor c, String
> desc,Descriptor d) {
>                this.constructors.put(c, new
> ModelMBeanConstructorInfo(desc, c,d));
>        }
>
>        private void copyInto(Object[] array, Hashtable table) {
>                Vector temp = new Vector(table.values());
>                temp.copyInto(array);
>        }
>
>        private String getType(String attName, boolean read, boolean
> write) {
>                boolean allowed = true;
>                if(attributes.containsKey(attName)) {
>                        MBeanAttributeInfo temp =
> (MBeanAttributeInfo)attributes.get(attName);
>                        if(read) {
>                                if(!temp.isReadable())
>                                        allowed = false;
>                                }
>                        if(write) {
>                                if(!temp.isWritable())
>                                        allowed = false;
>                        }
>
>                        if(!allowed)
>                                return null;
>                        else
>                                return temp.getType();
>                } else
>                        return null;
>        }
>
>        private void buildDynamicMBeanInfo()  {
>
>                try {
>                ModelMBeanOperationInfo[] ops = new
> ModelMBeanOperationInfo[operations.size()];
>                copyInto(ops, operations);
>
>                ModelMBeanAttributeInfo[] atts = new
> ModelMBeanAttributeInfo[attributes.size()];
>                copyInto(atts, attributes);
>
>                ModelMBeanConstructorInfo[] cons = new
> ModelMBeanConstructorInfo[constructors.size()];
>                copyInto(cons, constructors);
>
>                ModelMBeanNotificationInfo[] notifs = new
> ModelMBeanNotificationInfo[notifications.size()];
>                        copyInto(notifs, notifications);
>
>                Descriptor d = new DescriptorSupport();
>                d.setField("name", "MyTimer");
>                d.setField("descriptorType", "mbean");
>                d.setField("persistPolicy","OnTimer");
>                d.setField("persistPeriod", "1000");
>                modelMBeanInfo = new
> ModelMBeanInfoSupport(this.getClass().getName(), description, atts,
>                                                                  cons,
> ops,null,d);
>
>
>                } catch(Exception e) {
>                               System.err.println("Exception
> inbuildling modelmbean!");
>                               e.printStackTrace();
>                }
>        }
>
>
>
>       public MBeanInfo getMBeanInfo() {
>               buildDynamicMBeanInfo();
>               return (MBeanInfo)modelMBeanInfo;
>       }
>
>
>       /**
>        * @return the name of this MBean
>        */
>       public String getName() {
>               return "MyTimer";
>       }
>
>
>       public void testPrint() {
>               System.err.println("**** Here We Print!*****");
>         }
>
>       public int getState() {
>               return super.getState();
>       }
>
>       public String getStateString() {
>               return super.getStateString();
>       }
> }
>
> And here is the descriptor.....
>
> <?xml version="1.0" encoding="UTF-8"?>
> <server>
>
>   <mbean code="com.marco.config.MyTimer"
> name="WLSPortal:service=TimeBomb">
>    <description>sample for jboss xmbean.dtd</description>
>    <descriptors>
>     <persistence persistPolicy="OnTimer"
>          persistPeriod="2"/>
>
>     <state-action-on-update value="RESTART"/>
>     <descriptor name='testdescriptor' value='testvalue'/>
>    </descriptors>
>    <class>com.marco.config.MyTimer</class>
>    <operation impact="ACTION">
>       <description>testPrint</description>
>       <name>testPrint</name>
>    </operation>
>
>    </mbean>
> </server>
>
> thanx in advance and regards
>       marco
>
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: IBM Linux Tutorials
> Free Linux tutorial presented by Daniel Robbins, President and CEO of
> GenToo technologies. Learn everything from fundamentals to system
> administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> _______________________________________________
> JBoss-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/jboss-user
>

--
Juha Lindfors
Director of Training
http://www.jboss.com


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to