1 Reply Latest reply on Feb 5, 2013 3:40 AM by forum.khan

    Start Stop War-Ear programmatically in JBoss-5

    forum.khan

      Hi,

         Below is the script to start and stop the war/ear in Jboss programmatically. With this script am facing the below issue. Can anyone please let me know the changes that need to be done.

       

      Issues:

      1. When EAR is running, if i again try to start it will be automatically undeployed. Couldnt find the way to know that the EAR is running so that i dont need to run the start command. Deployed and Running are different which can be monitored from the JBoss JMX console.

      2. In JMX console the ear's will display the service and Mbean. These are displayed in alphabetical order. In case if the Mbean is displayed before the service it fails to stop the ear with a message, as the command are executed on the mbean.

      "ERROR [Twiddle] Command failure org.jboss.console.twiddle.command.CommandException: MBean has no such operation named 'stop' with signature compatible with: [] at org.jboss.console.twiddle.command.InvokeCommand.invoke(InvokeCommand.java:217) at org.jboss.console.twiddle.command.InvokeCommand.execute(InvokeCommand.java:291) at org.jboss.console.twiddle.Twiddle.main(Twiddle.java:306) Invoke an operation on an MBean usage: invoke [options] <query> <operation> (<arg>)* options: -q, --query-type[=<type>] Treat object name as a query -- Stop processing options query type: f[irst] Only invoke on the first matching name [default] a[ll] Invoke on all matching names tools>"

      3. In script we need to use Jboss twiddle.sh file. Please let me know how to achieve the below operation(start/stop) in java without using twiddle.sh file as even Jboss is in Java.

       

         start-stop war: http://nirvikalpa.wordpress.com/2010/08/25/start-stop-web-application-on-jboss-twiddle/

       

         start-stop ear:

          export JBOSS_HOME="D:/jboss-as"

          export TWIDDLE="$JBOSS_HOME/bin/twiddle.sh"

          echo $JBOSS_HOME

          echo $TWIDDLE

       

           if test $# -lt 4

           then

                 echo "Please enter the user,password,application ear name and the start/stop op"

           else

                 app=$($TWIDDLE -s 127.0.0.1 -u $1 -p $2 query 'jboss.j2ee:*' | grep $3)

                 app=`echo $app |  sed 's/\r//g'`

       

           if [ "${app}" == "" ]

              then

              echo "Application doesnt exit"

              exit -1

           fi

       

           if [ "$4" == "start" ]

           then

              echo "Starting ............"

                 doop=$($TWIDDLE -s 127.0.0.1 -u $1 -p $2 invoke jboss.j2ee:ear=$3,* $4  )

           fi   

       

           if [ "$4" == "stop" ]

           then

                echo "Stopping ............"

                doop=$($TWIDDLE -s 127.0.0.1 -u $1 -p $2 invoke jboss.j2ee:ear=$3,* $4 )

           fi         

       

           fi

        • 1. Re: Start Stop War-Ear programmatically in JBoss-5
          forum.khan

          Finally able to achieve how to Start & Stop the WAR-EAR programmatically in JBOSS.

          Can anyone let me know how to achieve the same functionality using shell script.

           

          import java.util.Hashtable;
          import javax.management.InstanceNotFoundException;
          import javax.management.MBeanInfo;
          import javax.management.MBeanServerConnection;
          import javax.management.ObjectName;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;

           

          public class StartStopService
          {
              static MBeanServerConnection mServer = null;

           

              private static MBeanInfo checkEarServices(
                      final String earServiceInfo,
                      final String earInfo,

                      final String warInfo,
                      final String startStopArg)
              {
                  // check if service exists
                  ObjectName object;
                  MBeanInfo serviceInfo = null;
                  try
                  {
                      object = new ObjectName(earServiceInfo);
                      serviceInfo = mServer.getMBeanInfo(object);
                  }
                  catch (final InstanceNotFoundException e)
                  {
                      if (startStopArg.equalsIgnoreCase("start"))
                      {
                          try
                          {
                              object = new ObjectName(earInfo);
                              mServer.invoke(object, "start", null, null);

                              object = new ObjectName(warInfo);
                              mServer.invoke(object, "start", null, null);

           

                          }
                          catch (final Exception e1)
                          {
                              e1.printStackTrace();
                          }
                      }
                      else if (startStopArg.equalsIgnoreCase("stop"))
                      {
                          System.out.println("Application not deployed to stop ....");
                      }
                  }
                  catch (final Exception e)
                  {
                      e.printStackTrace();
                  }

                  return serviceInfo;
              }

           

              private static void getServerConnection() throws NamingException
              {
                  final Hashtable<String, String> ht = new Hashtable<String, String>();
                  ht.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
                  ht.put(Context.PROVIDER_URL, "localhost:1099");
                  ht.put(Context.SECURITY_PRINCIPAL, "admin");
                  ht.put(Context.SECURITY_CREDENTIALS, ".....your password ........");

           

                  System.out.println("nt 1- Gotting InitialContext...... ");
                  final Context ctx = new InitialContext(ht);

           

                  System.out.println("nt 2- Got InitialContext: " + ctx);
                  mServer = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");
              }

           

              public static void main(final String[] args)
              {
                  final String startStopArg = "stop"; // start/stop i.e arg[0]
           
                 // check JMX console to see the details of your WAR & EAR.......

                // jboss.j2ee
                  final String earInfo = "jboss.j2ee:ear=one.ear,jar=one-service.jar,service=EJB3";
                  final String earServiceInfo =
                          "jboss.j2ee:ear=one.ear,jar=one-service.jar,name=oneService,service=EJB3";

                 // jobss.web.deployment

           

           

                 String warInfo = "jboss.web.deployment:war=/my/crud/services";

           

                  MBeanInfo serviceInfo = null;
                  ObjectName object;

                  try
                  {
                      getServerConnection();
                      if (mServer != null)
                      {
                          serviceInfo = checkEarServices(earServiceInfo, earInfo, warInfo, startStopArg);

                          if (serviceInfo != null)
                          {
                              if (startStopArg.equalsIgnoreCase("start"))
                              {
                                  System.out.println("Application is already running ....");
                              }
                              else if (startStopArg.equalsIgnoreCase("stop"))
                              {
                                  try
                                  {
                                      object = new ObjectName(earInfo);
                                      mServer.invoke(object, "stop", null, null);

                                      object = new ObjectName(warInfo);
                                      mServer.invoke(object, "stop", null, null);
                                  }
                                  catch (final Exception e1)
                                  {
                                      e1.printStackTrace();
                                  }
                              }
                          }
                      }
                  }
                  catch (final NamingException e)
                  {
                      e.printStackTrace();
                  }
              }
          }