3 Replies Latest reply on May 31, 2006 10:01 AM by Yair Zaslavsky

    More on thread pool MBean...what am I doing wrong?

    Yair Zaslavsky Newbie

      Good day.
      I deployed the EJB3 trailblazer (JBoss 4.04 RC1) including the calculator MBean and managed to write a client that activates the calculator.
      Now, I decided to take one step futher. In my tester class i wrote a static class called MyTask that implements Runnable and Serializable and looks like this:

      public static class MyTask implements Runnable, Serializable {

      private static final long serialVersionUID = -1575628286339248259L;

      public void run()
      {
      System.out.println("hello!");
      }
      }


      I then wrote the following code to get a thread pool (I found there are two thread pools by browsing with JBoss JMX-Console)

      Object o = ctx.lookup("jmx/invoker/RMIAdaptor");
      MBeanServerConnection mbsc = (MBeanServerConnection)o;
      ObjectName objName = new ObjectName("jboss.system:service=ThreadPool");


      if (mbsc.isRegistered(objName))
      {
      ObjectInstance objInstance = mbsc.getObjectInstance(objName);
      if (objInstance != null)
      {
      System.out.println("got object instance");
      System.out.println(objInstance.getClassName());
      System.out.println(objInstance.getObjectName());

      o = MBeanServerInvocationHandler.newProxyInstance(mbsc,
      objName,ThreadPool.class,false);


      if (o != null)
      {
      System.out.println("created proxy!");

      ThreadPool threadPool = (ThreadPool)o;
      threadPool.run(new MyTask());
      }
      }
      }


      I get the following exception:

      java.lang.IllegalStateException: getArguments failed
      at org.jboss.invocation.MarshalledInvocation.getArguments(MarshalledInvocation.java:513)
      at org.jboss.jmx.connector.invoker.InvokerAdaptorService.invoke(InvokerAdaptorService.java:235)
      at sun.reflect.GeneratedMethodAccessor62.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      .....

      My question is - what am I doing wrong here? How can I use the thread pool to execute one task from the client side? is this because the client is not running on the same VM ?

        • 1. Re: More on thread pool MBean...what am I doing wrong?
          Scott Stark Master

          thread pools are not usable outside of the server jvm. Use an mdb, jms, or ejb3 asynch extension to run a task on the server.

          • 2. Re: More on thread pool MBean...what am I doing wrong?
            Yair Zaslavsky Newbie

            Thank you for the comment,
            but besides this, is my code correct?

            • 3. Re: More on thread pool MBean...what am I doing wrong?
              Yair Zaslavsky Newbie

              Ok, I fixed the code

              @Service (objectName="trail:service=TestThreadPool")
              @Management(TestThreadPool.class)
              @Depends ("jboss.system:service=ThreadPool")
              public class TestThreadPoolMBean implements TestThreadPool {

              static public class MyTask implements Runnable
              {
              public void run()
              {
              try
              {
              System.out.println("hello world!!!!!!");
              FileOutputStream fos = new FileOutputStream("c:\bla.txt");
              fos.write("hello world!".getBytes());
              fos.close();
              }
              catch (Exception e)
              {
              e.printStackTrace();
              }
              }
              }

              ThreadPool pool;
              ThreadPoolMBean poolMBean;



              public int add(int a,int b)
              {
              return a+b;
              }


              public void doSomething()
              {
              //pool.run(new MyTask());

              pool = poolMBean.getInstance();

              if (pool == null)
              System.out.println("null thread pool!");
              else
              pool.run(new MyTask());


              }


              public void create() throws Exception {
              // TODO Auto-generated method stub


              //pool = new BasicThreadPool("MyPool");


              try
              {
              MBeanServer server = MBeanServerLocator.locate();

              poolMBean = (ThreadPoolMBean) MBeanProxyExt.create(
              ThreadPoolMBean.class,
              "jboss.system:service=ThreadPool",
              server);

              if (poolMBean != null)
              System.out.println("succeeded in looking up the thread pool mbean");

              }
              catch (Exception e)
              {
              e.printStackTrace();
              throw e;
              }



              }


              public void destroy() throws Exception {
              // TODO Auto-generated method stub

              }


              public void stop() throws Exception {
              // TODO Auto-generated method stub

              }


              public void start() throws Exception {
              // TODO Auto-generated method stub

              }