My team and I are using wowza to handle some DVR and video recording work within a larger enterprise application written in Java. I am in the process of prototyping a control app that will enable us to programmatically control Wowza through java. The commands we are most interested in are starting and stopping streams using previously configured and defined Applications and defining an output directory for the video recordings.
So far I’ve been using this sample JMX code that I discovered on the server: https://www.wowza.com/docs/how-to-set-up-a-command-line-interface-to-wowza-api-to-start-stop-vhost-using-jmx
It’s worked well and I’ve been able to authenticate with JMX which is configured to be available on the system. I have been operating under the (potentially incorrect) assumption that if I have applications configured and the server is running, then at least a default instance of the applications should exist, however I am getting a javax.management.InstanceNotFoundException. What does this mean and how can I remedy it?
Thanks in advance,
- Adam
Detailed Info below:
I’ve modified some of the code to give me more error information during the doInvoke call and it seems that during its ‘doInvoke’ method, its throwing a javax.management.InstanceNotFoundException. Here is my (very slightly) modified version of the class from the link above:
package jmxcommandline;
import java.util.HashMap;
import java.util.Map;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class JMXCommandLine {
public static final String STREAMINGTYPES_TOTAL = "total";
public static final String STREAMINGTYPES_CUPERTINO = "cupertino";
public static final String STREAMINGTYPES_SMOOTH = "smooth";
public static final String STREAMINGTYPES_RTMP = "rtmp";
public static final String STREAMINGTYPES_RTSP = "rtsp";
public static final String[][] countStrs = {
{"Connections", STREAMINGTYPES_TOTAL},
{"ConnectionsHTTPCupertino", STREAMINGTYPES_CUPERTINO},
{"ConnectionsHTTPSmooth", STREAMINGTYPES_SMOOTH},
{"ConnectionsRTMP", STREAMINGTYPES_RTMP},
{"ConnectionsRTP", STREAMINGTYPES_RTSP},
};
public static final String[][] ioStrs = {
{"IOPerformance", STREAMINGTYPES_TOTAL},
{"IOPerformanceHTTPCupertino", STREAMINGTYPES_CUPERTINO},
{"IOPerformanceHTTPSmooth", STREAMINGTYPES_SMOOTH},
{"IOPerformanceRTMP", STREAMINGTYPES_RTMP},
{"IOPerformanceRTP", STREAMINGTYPES_RTSP},
};
public static final int SESSIONPROTOCOL_SMOOTHSTREAMING = 0;
public static final int SESSIONPROTOCOL_CUPERTINOSTREAMING = 1;
public static final String MBEANNAME = "WowzaMediaServerPro";
public static final String CMD_GETSERVERVERSION = "getServerVersion";
public static final String CMD_STARTVHOST = "startVHost";
public static final String CMD_STOPVHOST = "stopVHost";
public static final String CMD_RELOADVHOSTCONFIG = "reloadVHostConfig";
public static final String CMD_STARTAPPINSTANCE = "startAppInstance";
public static final String CMD_TOUCHAPPINSTANCE = "touchAppInstance";
public static final String CMD_SHUTDOWNAPPINSTANCE = "shutdownAppInstance";
public static final String CMD_STARTMEDIACASTERSTREAM = "startMediaCasterStream";
public static final String CMD_STOPMEDIACASTERSTREAM = "stopMediaCasterStream";
public static final String CMD_RESETMEDIACASTERSTREAM = "resetMediaCasterStream";
public static final String CMD_GETCONNECTIONCOUNTS = "getConnectionCounts";
public static final String CMD_GETIOOUTBYTERATE = "getIOOutByteRate";
public static final String CMD_GETIOINBYTERATE = "getIOInByteRate";
public static final String DEFAULT_VHOST = "_defaultVHost_";
public static final String DEFAULT_APPLICATION = "_defapp_";
public static final String DEFAULT_APPINSTANCE = "_definst_";
public static class AppConextName
{
String vhostName = null;
String appName = null;
String appInstName = null;
public AppConextName()
{
}
public AppConextName(String fullname, boolean startWithDefaults)
{
if (startWithDefaults)
{
vhostName = DEFAULT_VHOST;
appName = DEFAULT_APPLICATION;
appInstName = DEFAULT_APPINSTANCE;
}
int qloc = fullname.indexOf(":");
if (qloc >= 0) {
vhostName = fullname.substring(0, qloc);
fullname = fullname.substring(qloc + 1);
}
else {
vhostName = DEFAULT_VHOST;
}
if (fullname.length() > 0) {
appName = fullname;
int sloc = fullname.indexOf("/");
if (sloc >= 0) {
appName = fullname.substring(0, sloc);
appInstName = fullname.substring(sloc + 1);
}
}
}
String getObjName() {
String ret = "";
while(true) {
if (vhostName == null) {
break;
}
ret += "vHosts=VHosts,vHostName=" + vhostName;
if (appName == null){
break;
}
ret += ",applications=Applications,applicationName=" + appName;
if (appInstName == null){
break;
}
ret += ",applicationInstances=ApplicationInstances,applicationInstanceName=" + appInstName;
break;
}
return ret;
}
}
public static void printUsage() {
System.out.println("");
System.out.println("Usage:");
System.out.println("");
System.out.println("[command] -[switch [value]...] [command] [params...]");
System.out.println("");
System.out.println("Switches:");
System.out.println("");
System.out.println(" -jmx [jmx-url]");
System.out.println(" -user [jmx-username]");
System.out.println(" -pass [jmx-password]");
System.out.println("");
System.out.println("Commands:");
System.out.println("");
System.out.println(" " + CMD_GETSERVERVERSION);
System.out.println(" " + CMD_STARTVHOST + " [vhost]");
System.out.println(" " + CMD_STOPVHOST + " [vhost]");
System.out.println(" " + CMD_RELOADVHOSTCONFIG + "");
System.out.println(" " + CMD_STARTAPPINSTANCE + " [vhost:application/appInstance]");
System.out.println(" " + CMD_TOUCHAPPINSTANCE + " [vhost:application/appInstance]");
System.out.println(" " + CMD_SHUTDOWNAPPINSTANCE + " [vhost:application/appInstance]");
System.out.println(" " + CMD_STARTMEDIACASTERSTREAM + " [vhost:application/appInstance] [stream-name] [mediacaster-type]");
System.out.println(" " + CMD_STOPMEDIACASTERSTREAM + " [vhost:application/appInstance] [stream-name]");
System.out.println(" " + CMD_RESETMEDIACASTERSTREAM + " [vhost:application/appInstance] [stream-name]");
System.out.println(" " + CMD_GETCONNECTIONCOUNTS);
System.out.println(" " + CMD_GETCONNECTIONCOUNTS + " [vhost:application/appInstance]");
System.out.println(" " + CMD_GETCONNECTIONCOUNTS + " [vhost:application/appInstance] [stream-name]");
System.out.println(" " + CMD_GETIOOUTBYTERATE);
System.out.println(" " + CMD_GETIOOUTBYTERATE + " [vhost:application/appInstance]");
System.out.println(" " + CMD_GETIOINBYTERATE);
System.out.println(" " + CMD_GETIOINBYTERATE + " [vhost:application/appInstance]");
System.out.println("");
}
public static long objToLong(Object valueObj) {
long ret = 0;
if (valueObj != null) {
try {
ret = Long.parseLong(valueObj.toString());
}
catch(Exception e) {
}
}
return ret;
}
public static Object doInvoke(MBeanServerConnection connection, ObjectName connectsObjName, String cmdStr, Object[] arguments, String[] signature) {
Object returnObj = null;
try {
returnObj = connection.invoke(connectsObjName, cmdStr, arguments, signature);
} catch(Exception e) {
System.out.println("\n\nERROR: during method: doInvoke.");
e.printStackTrace();
StringBuilder bldr = new StringBuilder("\n\nArguments Provided: \n");
for (Object obj : arguments){
if (obj instanceof String){
String str = (String)obj;
bldr.append(str + "\n");
} else {
bldr.append("Non-string object in arguments.\n");
}
}
System.out.println(bldr.toString());
}
return returnObj;
}
public static Object doGetAttribute(MBeanServerConnection connection, ObjectName connectsObjName, String attributeName) {
Object returnObj = null;
try {
returnObj = connection.getAttribute(connectsObjName, attributeName);
} catch (Exception e) {
System.out.println("ERROR: doGetAttribute: " + e.toString());
}
return returnObj;
}
public static void main(String[] args) {
try {
for(int i=0;i<args.length;i++)
args[i] = args[i].trim();
String host = "localhost";
String username = null;
String password = null;
String jmxURL = "service:jmx:rmi://" + host + ":8084/jndi/rmi://" + host + ":8085/jmxrmi";
int argOffset = 0;
while(true) {
if (argOffset >= args.length) {
break;
}
if (!args[argOffset].startsWith("-")) {
break;
}
if (args[argOffset].startsWith("-host")) {
argOffset++;
host = args[argOffset];
jmxURL = "service:jmx:rmi://" + host + ":8084/jndi/rmi://" + host + ":8085/jmxrmi";
}
else if (args[argOffset].startsWith("-jmx")) {
argOffset++;
jmxURL = args[argOffset];
}
else if (args[argOffset].startsWith("-user")) {
argOffset++;
username = args[argOffset];
}
else if (args[argOffset].startsWith("-pass")) {
argOffset++;
password = args[argOffset];
}
argOffset++;
}
if (argOffset >= args.length) {
printUsage();
return;
}
// 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>();
if (username != null && password != null) {
String[] creds = {username, password};
env.put(JMXConnector.CREDENTIALS, creds);
}
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, env);
MBeanServerConnection connection = connector.getMBeanServerConnection();
if (connection == null) {
System.out.println("ERROR: Cannot connect to JMX interface." + jmxURL);
return;
}
if (args[argOffset].equalsIgnoreCase(CMD_GETSERVERVERSION)) {
String connectsName = MBEANNAME + ":name=Server";
ObjectName connectsObjName = new ObjectName(connectsName);
Object attrObj = doGetAttribute(connection, connectsObjName, "version");
if (attrObj != null) {
System.out.println("Attribute object: " + attrObj);
}
else {
System.out.println("Null attribute object retrieved?");
}
}
else if (args[argOffset].equalsIgnoreCase(CMD_STARTVHOST)) {
String connectsName = MBEANNAME + ":name=Server";
ObjectName connectsObjName = new ObjectName(connectsName);
System.out.println(args[argOffset] + " " + args[argOffset + 1]);
Object[] arguments = {args[argOffset + 1]};
String[] signature = {"java.lang.String"};
doInvoke(connection, connectsObjName, "startVHost", arguments, signature);
}
else if (args[argOffset].equalsIgnoreCase(CMD_STOPVHOST)) {
String connectsName = MBEANNAME + ":name=Server";
ObjectName connectsObjName = new ObjectName(connectsName);
System.out.println(args[argOffset] + " " + args[argOffset + 1]);
Object[] arguments = {args[argOffset + 1]};
String[] signature = {"java.lang.String"};
doInvoke(connection, connectsObjName, "stopVHost", arguments, signature);
}
else if (args[argOffset].equalsIgnoreCase(CMD_RELOADVHOSTCONFIG)) {
String connectsName = MBEANNAME + ":name=Server";
ObjectName connectsObjName = new ObjectName(connectsName);
System.out.println(args[argOffset]);
Object[] arguments = {};
String[] signature = {};
doInvoke(connection, connectsObjName, "reloadVHostConfig", null, null);
}
else if (args[argOffset].equalsIgnoreCase(CMD_STARTAPPINSTANCE)) {
System.out.println(args[argOffset] + " " + args[argOffset + 1]);
AppConextName context = new AppConextName(args[argOffset + 1], true);
String connectsName = MBEANNAME + ":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"};
doInvoke(connection, connectsObjName, "startApplicationInstance", arguments, signature);
}
else if (args[argOffset].equalsIgnoreCase(CMD_TOUCHAPPINSTANCE)) {
System.out.println(args[argOffset] + " " + args[argOffset + 1]);
AppConextName context = new AppConextName(args[argOffset + 1], true);
String connectsName = MBEANNAME + ":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"};
doInvoke(connection, connectsObjName, "touchApplicationInstance", arguments, signature);
}
else if (args[argOffset].equalsIgnoreCase(CMD_SHUTDOWNAPPINSTANCE)) {
System.out.println(args[argOffset] + " " + args[argOffset + 1]);
AppConextName context = new AppConextName(args[argOffset + 1], true);
String connectsName = MBEANNAME + ":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"};
doInvoke(connection, connectsObjName, "shutdownAppInstance", arguments, signature);
}
else if (args[argOffset].equalsIgnoreCase(CMD_STARTMEDIACASTERSTREAM)) {
System.out.println("Start Stream Command: " + args[argOffset] + " " + args[argOffset + 1] + " " + args[argOffset + 2] + " " + args[argOffset + 3]);
AppConextName context = new AppConextName(args[argOffset + 1], true);
String connectsName = MBEANNAME + ":vHosts=VHosts,vHostName=" + context.vhostName + ",applications=Applications,applicationName=" + context.appName + ",applicationInstances=ApplicationInstances,applicationInstanceName=" + context.appInstName + ",name=ApplicationInstance";
ObjectName connectsObjName = new ObjectName(connectsName);
Object[] arguments = {args[argOffset + 2], args[argOffset + 3]};
String[] signature = {"java.lang.String", "java.lang.String"};
doInvoke(connection, connectsObjName, CMD_STARTMEDIACASTERSTREAM, arguments, signature);
}
else if (args[argOffset].equalsIgnoreCase(CMD_STOPMEDIACASTERSTREAM)) {
System.out.println("Stop Stream Command: " + args[argOffset] + " " + args[argOffset + 1] + " " + args[argOffset + 2]);
AppConextName context = new AppConextName(args[argOffset + 1], true);
String connectsName = MBEANNAME + ":vHosts=VHosts,vHostName=" + context.vhostName + ",applications=Applications,applicationName=" + context.appName + ",applicationInstances=ApplicationInstances,applicationInstanceName=" + context.appInstName + ",name=ApplicationInstance";
ObjectName connectsObjName = new ObjectName(connectsName);
Object[] arguments = {args[argOffset + 2]};
String[] signature = {"java.lang.String"};
doInvoke(connection, connectsObjName, "stopMediaCasterStream", arguments, signature);
}
else if (args[argOffset].equalsIgnoreCase(CMD_RESETMEDIACASTERSTREAM)) {
System.out.println(args[argOffset] + " " + args[argOffset + 1] + " " + args[argOffset + 2]);
AppConextName context = new AppConextName(args[argOffset + 1], true);
String connectsName = MBEANNAME + ":vHosts=VHosts,vHostName=" + context.vhostName + ",applications=Applications,applicationName=" + context.appName + ",applicationInstances=ApplicationInstances,applicationInstanceName=" + context.appInstName + ",name=ApplicationInstance";
ObjectName connectsObjName = new ObjectName(connectsName);
Object[] arguments = {args[argOffset + 2]};
String[] signature = {"java.lang.String"};
doInvoke(connection, connectsObjName, "resetMediaCasterStream", arguments, signature);
}
else if (args[argOffset].equalsIgnoreCase(CMD_GETCONNECTIONCOUNTS)) {
StringBuffer outputBuf = new StringBuffer();
String streamNamesStr = null;
AppConextName context = null;
if (args.length > (argOffset + 2))
{
context = new AppConextName(args[argOffset + 1], true);
streamNamesStr = args[argOffset + 2];
}
else if (args.length > (argOffset + 1)) {
context = new AppConextName(args[argOffset + 1], false);
}
else {
context = new AppConextName();
}
if (streamNamesStr != null) {
String[] streamNames = streamNamesStr.split("[|]");
String contextStr = context.getObjName();
if (contextStr.length() > 0) {
contextStr += ",";
}
String connectsName = MBEANNAME + ":" + contextStr + "name=ApplicationInstance";
ObjectName connectsObjName = new ObjectName(connectsName);
long total = 0;
long cupertinoTotal = 0;
long smoothTotal = 0;
long rtspTotal = 0;
long rtmpTotal = 0;
for(int i=0; i<streamNames.length; i++) {
String streamName = streamNames[i].trim();
{
Object[] arguments = {SESSIONPROTOCOL_CUPERTINOSTREAMING, streamName};
String[] signature = {"int", "java.lang.String"};
Object attrObj = doInvoke(connection, connectsObjName, "getHTTPStreamerSessionCount", arguments, signature);
cupertinoTotal += objToLong(attrObj);
}
{
Object[] arguments = {SESSIONPROTOCOL_SMOOTHSTREAMING, streamName};
String[] signature = {"int", "java.lang.String"};
Object attrObj = doInvoke(connection, connectsObjName, "getHTTPStreamerSessionCount", arguments, signature);
smoothTotal += objToLong(attrObj);
}
{
Object[] arguments = {streamName};
String[] signature = {"java.lang.String"};
Object attrObj = doInvoke(connection, connectsObjName, "getPlayStreamCount", arguments, signature);
rtmpTotal += objToLong(attrObj);
}
{
Object[] arguments = {streamName};
String[] signature = {"java.lang.String"};
Object attrObj = doInvoke(connection, connectsObjName, "getRTPSessionCount", arguments, signature);
rtspTotal += objToLong(attrObj);
}
}
total = cupertinoTotal + smoothTotal + rtmpTotal + rtspTotal;
System.out.println(STREAMINGTYPES_TOTAL + ":" + total + " " + STREAMINGTYPES_CUPERTINO + ":" + cupertinoTotal + " " + STREAMINGTYPES_SMOOTH + ":" + smoothTotal + " " + STREAMINGTYPES_RTMP + ":" + rtmpTotal + " " + STREAMINGTYPES_RTSP + ":" + rtspTotal);
}
else {
for(int i=0;i<countStrs.length;i++) {
String contextStr = context.getObjName();
if (contextStr.length() > 0) {
contextStr += ",";
}
String connectsName = MBEANNAME + ":" + contextStr + "name=" + countStrs[i][0];
ObjectName connectsObjName = new ObjectName(connectsName);
Object attrObj = doGetAttribute(connection, connectsObjName, "current");
String valueStr = attrObj==null?"0":attrObj.toString();
if (outputBuf.length() > 0) {
outputBuf.append(" ");
}
outputBuf.append(countStrs[i][1] + ":" + valueStr);
}
}
System.out.println(outputBuf.toString());
}
else if (args[argOffset].equalsIgnoreCase(CMD_GETIOOUTBYTERATE) || args[argOffset].equalsIgnoreCase(CMD_GETIOINBYTERATE)) {
StringBuffer outputBuf = new StringBuffer();
AppConextName context = null;
if (args.length > (argOffset + 1)){
context = new AppConextName(args[argOffset + 1], false);
}
else {
context = new AppConextName();
}
String attrValue = args[argOffset].equalsIgnoreCase(CMD_GETIOOUTBYTERATE)?"messagesOutBytesRate":"messagesInBytesRate";
for(int i=0;i<ioStrs.length;i++) {
String contextStr = context.getObjName();
if (contextStr.length() > 0) {
contextStr += ",";
}
String connectsName = MBEANNAME + ":"+contextStr+"name=" + ioStrs[i][0];
ObjectName connectsObjName = new ObjectName(connectsName);
Object attrObj = doGetAttribute(connection, connectsObjName, attrValue);
String valueStr = attrObj==null?"0":attrObj.toString();
if (outputBuf.length() > 0) {
outputBuf.append(" ");
}
outputBuf.append(countStrs[i][1] + ":" + valueStr);
}
System.out.println(outputBuf.toString());
}
else {
System.out.println("ERROR: Command not recognized: " + args[argOffset]);
}
}
catch (Exception e) {
System.out.println("ERROR: " + e.toString());
}
}
}
Here is the command I am running at the CLI:
-user <username> -pass <userpass> startMediaCasterStream videorecording mpegts.stream rtp
Here is the output I’m getting at the command line:
ERROR: doInvoke: javax.management.InstanceNotFoundException: WowzaMediaServerPro:vHosts=VHosts,vHostName=_defaultVHost_,applications=Applications,applicationName=videorecording,applicationInstances=ApplicationInstances,applicationInstanceName=_definst_,name=ApplicationInstance
javax.management.InstanceNotFoundException: WowzaMediaServerPro:vHosts=VHosts,vHostName=_defaultVHost_,applications=Applications,applicationName=videorecording,applicationInstances=ApplicationInstances,applicationInstanceName=_definst_,name=ApplicationInstance
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknown Source)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getClassLoaderFor(Unknown Source)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getClassLoaderFor(Unknown Source)
at com.sun.jmx.remote.security.MBeanServerAccessController.getClassLoaderFor(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl$5.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javax.management.remote.rmi.RMIConnectionImpl.getClassLoaderFor(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at com.sun.jmx.remote.internal.PRef.invoke(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl_Stub.invoke(Unknown Source)
at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.invoke(Unknown Source)
at jmxcommandline.JMXCommandLine.doInvoke(JMXCommandLine.java:181)
at jmxcommandline.JMXCommandLine.main(JMXCommandLine.java:361)
Arguments Provided:
mpegts.stream
rtp