Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Sample Java class for creating a command line interface to start/stop VHost using JMX

  1. #1

    Default Sample Java class for creating a command line interface to start/stop VHost using JMX

    Here is a quick and dirty Java class that utilizes the JMX interface to implement virtual, appInstance and MediaCaster stream startup/shutdown from the command line. Here is the code:

    Code:
    import java.util.*;
    import javax.management.*;
    import javax.management.remote.*;
    
    public class JMXVHost
    {
    	public static class AppConextName
    	{
    		String vhostName = "_defaultVHost_";
    		String appName = "_defapp_";
    		String appInstName = "_definst_";
    		
    		public AppConextName(String fullname)
    		{
    			int qloc = fullname.indexOf(":");
    			if (qloc >= 0)
    			{
    				vhostName = fullname.substring(0, qloc);
    				fullname = fullname.substring(qloc+1);
    			}
    			
    			appName = fullname;
    			int sloc = fullname.indexOf("/");
    			if (sloc >= 0)
    			{
    				appName = fullname.substring(0, qloc);
    				appInstName = fullname.substring(qloc+1);
    			}
    		}
    	}
    	public static void main(String[] args)
    	{
    		String username = "admin";
    		String password = "admin";
    		String jmxURL = "service:jmx:rmi://localhost:8084/jndi/rmi://localhost:8085/jmxrmi";
    
    		try
    		{
    			// create a connection URL
    			JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
    
    			// create a environment hash with username and password
    			Map<String, Object> env = new HashMap<String, Object>();
    			String[] creds = {username, password};
    			env.put(JMXConnector.CREDENTIALS, creds);
    
    			// connect to the server
    			JMXConnector connector = JMXConnectorFactory.connect(serviceURL, env);      
    			MBeanServerConnection connection = connector.getMBeanServerConnection();
    
    			// Call start/stop vhost through the Server object
    
    			if (args[0].equals("startVHost"))
    			{
    				String connectsName = "WowzaMediaServerPro:name=Server";
    				ObjectName connectsObjName = new ObjectName(connectsName);
    
    				System.out.println(args[0]+" "+args[1]);
    				Object[] arguments = {args[1]};
    				String[] signature = {"java.lang.String"};
    				connection.invoke(connectsObjName, "startVHost", arguments, signature);
    			}
    			else if (args[0].equals("stopVHost"))
    			{
    				String connectsName = "WowzaMediaServerPro:name=Server";
    				ObjectName connectsObjName = new ObjectName(connectsName);
    
    				System.out.println(args[0]+" "+args[1]);
    				Object[] arguments = {args[1]};
    				String[] signature = {"java.lang.String"};
    				connection.invoke(connectsObjName, "stopVHost", arguments, signature);
    			}
    			else if (args[0].equals("reloadVHostConfig"))
    			{
    				String connectsName = "WowzaMediaServerPro:name=Server";
    				ObjectName connectsObjName = new ObjectName(connectsName);
    
    				System.out.println(args[0]);
    				Object[] arguments = {};
    				String[] signature = {};
    				connection.invoke(connectsObjName, "reloadVHostConfig", null, null);
    			}
    			else if (args[0].equals("startAppInstance"))
    			{
    				System.out.println(args[0]+" "+args[1]);
    				
    				AppConextName context = new AppConextName(args[1]);
    				
    				String connectsName = "WowzaMediaServerPro:vHosts=VHosts,vHostName="+context.vhostName+",name=VHost";
    				ObjectName connectsObjName = new ObjectName(connectsName);
    				
    				Object[] arguments = {context.appName, context.appInstName};
    				String[] signature = {"java.lang.String", "java.lang.String"};
    				connection.invoke(connectsObjName, "startApplicationInstance", arguments, signature);
    			}
    			else if (args[0].equals("shutdownAppInstance"))
    			{
    				System.out.println(args[0]+" "+args[1]);
    				
    				AppConextName context = new AppConextName(args[1]);
    				
    				String connectsName = "WowzaMediaServerPro:vHosts=VHosts,vHostName="+context.vhostName+",applications=Applications,applicationName="+context.appName+",name=Application";
    				ObjectName connectsObjName = new ObjectName(connectsName);
    				
    				Object[] arguments = {context.appInstName};
    				String[] signature = {"java.lang.String"};
    				connection.invoke(connectsObjName, "shutdownAppInstance", arguments, signature);
    			}
    			else if (args[0].equals("startMediaCasterStream"))
    			{
    				System.out.println(args[0]+" "+args[1]+" "+args[2]+" "+args[3]);
    				
    				AppConextName context = new AppConextName(args[1]);
    
    				String connectsName = "WowzaMediaServerPro:vHosts=VHosts,vHostName="+context.vhostName+",applications=Applications,applicationName="+context.appName+",applicationInstances=ApplicationInstances,applicationInstanceName="+context.appInstName+",modules=Modules,module=ModuleMediaCasterStreamManager,name=Instance";
    				ObjectName connectsObjName = new ObjectName(connectsName);
    				
    				Object[] arguments = {args[2], args[3]};
    				String[] signature = {"java.lang.String", "java.lang.String"};
    				connection.invoke(connectsObjName, "startMediaCasterStream", arguments, signature);
    			}
    			else if (args[0].equals("stopMediaCasterStream"))
    			{
    				System.out.println(args[0]+" "+args[1]+" "+args[2]);
    				
    				AppConextName context = new AppConextName(args[1]);
    
    				String connectsName = "WowzaMediaServerPro:vHosts=VHosts,vHostName="+context.vhostName+",applications=Applications,applicationName="+context.appName+",applicationInstances=ApplicationInstances,applicationInstanceName="+context.appInstName+",modules=Modules,module=ModuleMediaCasterStreamManager,name=Instance";
    				ObjectName connectsObjName = new ObjectName(connectsName);
    				
    				Object[] arguments = {args[2]};
    				String[] signature = {"java.lang.String"};
    				connection.invoke(connectsObjName, "stopMediaCasterStream", arguments, signature);
    			}
    			else
    				System.out.println("ERROR: Command not recognized: "+args[0]);
    		}
    
    		catch (Exception e)
    		{
    			System.out.println("ERROR: "+e.toString());
    		}
    	}
    }
    Compile using the command:

    Code:
    javac JMXVHost.java
    It should generate the class file JMXVHost.class. To use the command the format is:

    Code:
    java -cp . JMXVHost [command] [param1] [param2] ...
    Here are all the support commands:

    Code:
    Start virtual host:
    java -cp . JMXVHost startVHost [vhost-name]
    
    Stop virtual host:
    java -cp . JMXVHost stopVHost [vhost-name]
    
    Reload the virtual host VHosts.xml file:
    java -cp . JMXVHost reloadVHostConfig
    
    Start appInstance
    java -cp . JMXVHost startAppInstance [app-instance-path]
    
    Shutdown appInstance
    java -cp . JMXVHost shutdownAppInstance [app-instance-path]
    
    Start MediaCaster stream
    java -cp . JMXVHost startMediaCasterStream [app-instance-path] [stream-name] [mediacaster-type]
    
    Stop MediaCaster stream
    java -cp . JMXVHost stopMediaCasterStream [app-instance-path] [stream-name]
    Where:
    • [app-instance-path]: is a full path to an application instance in the form [vhost-name]:[app-name]/[appInstance-name] where if unspecified [vhost-name] defaults to _defaultVHost_ and [appInstance-name] defaults to _definst_. For example to start the an application instance named live using the default appInstance name the [app-instance-path] would be just live.
    • [mediacaster-type]: Valid [mediacaster-type] are rtp, rtp-record, shoutcast, shoutcast-record and liverepeater.


    Obviously the JMX url, username and password are hard coded in the code. This could be pulled out as parameters passed into the command.

    You can see how this methodology could be used to create a pretty robust remote command line interface to Wowza Media Server.

    Note: To use the startMediaCasterStream and stopMediaCasterStream commands you must have the most recent MediaCasterStreamManager package installed properly in the [install-dir]/conf/[application]/Application.xml to which you want to start the stream.

    Charlie

  2. #2

    Default

    How about the "reloadVHostConfig" command? How could I integrate that command into this code? I tried adding it myself, but I don't know Java

  3. #3

    Default

    Very nice, thank you!

  4. #4

    Default

    Hi Charlie,

    I try to use this, but I have a little problem.

    If I run this command work well:
    java -cp . JMXVHost startAppInstance rtplive

    But if I run this don't do anything:
    java -cp . JMXVHost startAppInstance _defaultVHost_:rtplive/testing

    thanks in advance for any help.
    Alejandro

  5. #5

    Default

    The code is there. Can you see what might be wrong?

    Charlie

  6. #6

    Default

    Hi Charlie,

    I found the error, the Parser don't work well.

    This is my first time working with java, I don't guarantee this code

    For fix this I rewrite the code of the parser.

    The code is more large that the Forum allow, for this reason I have posted this here:
    http://pastebin.com/d3930bbc2

    Regards,
    Ale

  7. #7

    Default

    Great, I will update the code.

    Charlie

  8. #8

    Default

    Thanks for the info but I'm cannot get startMediaCasterStream or stop MediaCasterStream to work properly.

    In StartupStreams.xml I have;
    <StartupStream>
    <Application>live/_definst_</Application>
    <MediaCasterType>rtp</MediaCasterType>
    <StreamName>avn256.stream</StreamName>
    </StartupStream>
    If I restart Wowza all works fine but if I try to stop or start the stream as below;

    java -cp . JMXVHost startMediaCasterStream live avn256.stream rtp

    I get;
    startMediaCasterStream live avn256.stream rtp
    ERROR: javax.management.InstanceNotFoundException: WowzaMediaServerPro:vHosts=VHosts,vHostName=_defaultVHost_,applications=Applications,applicationName =live,applicationInstances=ApplicationInstances,applicationInstanceName=_definst_,modules=Modules,mo dule=ModuleMediaCasterStreamManager,name=Instance

    Thanks
    Austin

  9. #9
    Join Date
    Dec 2007
    Posts
    27,910

    Default

    Looks like you are putting application name where app-instance-path is needed:

    Code:
    Start MediaCaster stream
    java -cp . JMXVHost startMediaCasterStream [app-instance-path] [stream-name] [mediacaster-type]
    
    Stop MediaCaster stream
    java -cp . JMXVHost stopMediaCasterStream [app-instance-path] [stream-name]
    Richard

  10. #10

    Default

    For now it is required that you have this package configured for your application:

    http://www.wowzamedia.com/forums/showthread.php?t=4533


    Charlie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •