5

I have a requirement to write a java program to remotely start stop a jboss server on request. Can anyone please suggest how could it be done? One option could be invoke start/stop script but this java program(may be servlet or jsp) exists on different machine. We are using jboss server 7.

zerologiko
  • 1,993
  • 1
  • 18
  • 21
user458580
  • 51
  • 1
  • 2

1 Answers1

5

A simple method to start and stopping Jboss remotely can be done with the run.sh and shutdown.sh script, by pointing to the right host and port. If you are on Linux you can run:

rsh user@host /path/to/jboss/bin/run.sh
rsh user@host /path/to/jboss/bin/shutdown.sh

You can also execute a Shell command with Java, you can use Runtime exec mewthod:

 Runtime.getRuntime().exec("shell command here");

See this complete answer for more details on Java exec method.


A better alternative I would suggest, is to use JMX-console programmatically, you can stop/restart a Jboss intance by invoking the shutdown method on the Server MBean. JMX approach is more powerful because you can monitor and manage every aspect of the Jboss runinng instanace (like logging, memory or cpu). See this to start.

I've created a snippet to ease your start, see this working solution http://snipt.org/Ahhjh4

Remember:

  • create a Jboss user on the Jboss instance using add-user.sh (JBOSS_HOME/bin)
  • include the jboss-client.jar in your client class-path (the jar is in JBOSS_HOME/bin/client)

Good luck!

Community
  • 1
  • 1
zerologiko
  • 1,993
  • 1
  • 18
  • 21
  • Thanks for this post. I would prefer to do it through JMX connector. Once I get handle to JMXConnector and MBeanServerConnection of a running Jboss AS. How can I stop and restart AS. I appreciate your help on this. – user458580 Sep 30 '13 at 08:40
  • I edited the answer adding the full solution snippet. – zerologiko Sep 30 '13 at 09:38
  • Once again thanks for this nice example. My problem is I need to start and stop jboss server at different point of time. Once I stop the server, I may not be able to get the handle of Mbean to start it again. Is it possible to stop jboss with some of the Mbean running so it can be started again in future, of course remotely. – user458580 Sep 30 '13 at 10:39
  • When Jboss it's shudown, then well, it's shutdown.. only the run.sh command can start it. I think you can easily to use the first simple method to start the Jboss, and the JMX method to monitor, manage, restart and shutdown. – zerologiko Sep 30 '13 at 10:51
  • Have you succeeded or you need clarifications? – zerologiko Oct 01 '13 at 08:55