To access IBM®
Streams information by using the JMX API, you must supply the necessary environment parameter values,
establish a JMX connection, obtain access to an IBM
Streams JMX bean proxy, and call bean methods through the proxy.
Procedure
- Supply the necessary environment parameter value. The package name for the IBM
Streams connector client provider must be provided as an environment parameter. The connector client
provider is in the IBM
Streams product.
The package name is com.ibm.streams.management, and it must be
supplied as the value for the jmx.remote.protocol.provider.pkgs property.
For example, the following Java™ code sets the
environment
parameter:HashMap<String,Object> env = new HashMap<String,Object>();
env.put("jmx.remote.protocol.provider.pkgs", "com.ibm.streams.management");
- Complete the setup for authenticating to the JMX server.
To establish a JMX connection, you must authenticate either by supplying an IBM
Streams user ID and password, or by providing a client certificate.
- If required, specify the cryptographic protocol to use for the JMX connection. This step is
required only if the jmx.sslOption configuration property is set to a value
other than the default value.
To specify the cryptographic protocol to use, supply a value for the
jmx.remote.tls.enabled.protocols environment property. The specified value must
match the value of the jmx.sslOption property. To retrieve this value, run the
streamtool getdomainproperty jmx.sslOption command.
The following example shows how to supply this value in a Java
application:HashMap<String,Object> env = new HashMap<String,Object>();
env.put("jmx.remote.tls.enabled.protocols", "SSL_TLSv2");
- Optional: Specify the locale that determines the language in which messages
are returned from JMX calls. If you do not specify a locale, messages are returned in the language
of the JMX process locale.
To specify the locale, supply either a java.util.Locale object or a string representation of the
locale for the jmx.remote.locale environment property. If you use a string representation, it must
be in the format that is documented for the java.util.Locale.toString()
method.
The following example shows how to supply this value in a Java application:
HashMap<String,Object> env = new HashMap<String,Object>();
env.put("jmx.remote.locale", Locale.ENGLISH);
Supported
languages:
- Brazilian Portuguese (pt_BR)
- English (en)
- French (fr)
- German (de)
- Italian (it)
- Japanese (ja)
- Korean (ko)
- Russian (ru)
- Simplified Chinese (zh_CN)
- Spanish (es)
- Traditional Chinese (zh_TW)
Note: If you start an instance on IBM
Streams that is earlier than version 4.2.1, messages from those instances will be returned in English
regardless of the locale that you specify.
- Establish a JMX connection.
The IBM
Streams JMX connector is part of the management API service. To establish a connection to the JMX connector, call the
JMXConnectorFactory.connect() method. You must supply the environment parameters
that are described in the previous steps and the JMX URL.
You can determine the JMX URL by using the streamtool getjmxconnect command.
After you establish a connection to the JMX connector, retrieve a connection to the managed bean
server by calling the getMBeanServerConnection() method on the returned
JMXConnector object.
For example, the following Java code establishes a
JMX
connection:JMXConnector jmxc = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrlString), env);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
Note: If the
management API service restarts, the JMX URL might no longer be valid for establishing a new connection. If the
jmx.port domain property is set to find an available port when the service
starts, a different port might be used each time that the service starts. The service might also
start on a different resource. Use the
streamtool getjmxconnect command to
determine the new JMX URL after the service starts. Alternatively, specify a port number for the
jmx.port domain property, so that the same port is used each time that the
service starts. To designate which resources you want to run the service, use the following tag:
jmx.
For more information, see Assigning tags to
resources in a domain.
- Obtain access to an IBM
Streams JMX bean proxy.
The easiest way to access the attributes and operations of a JMX bean is through a proxy object.
To obtain the proxy object for a JMX bean, you need to know the class name of the bean (for example
DomainMXBean.class) and the object name for the specific bean to access. You can
use the com.ibm.streams.management.ObjectNameBuilder class to help construct the
object name. Alternatively, you can use the MBeanServerConnection.queryMBeans()
method to query the object name.
For example, the following Java code uses the
com.ibm.streams.management.ObjectNameBuilder class to construct the object name and
access a JMX bean
proxy:ObjectName domainObjName = ObjectNameBuilder.domain(domainId);
DomainMXBean domain = JMX.newMXBeanProxy(mbsc, domainObjName, DomainMXBean.class, true);
- Call the bean methods through the proxy. After you obtain a proxy for the bean, you can access the bean attributes and operations by
calling methods on the proxy. For example, the following Java code uses methods to
obtain the domain status and the list of instances in the domain:
System.out.println("Status: " + domain.getStatus());
System.out.println("Instances: " + domain.getInstances());
Example
The following Java code example uses the
IBM
Streams JMX API to retrieve the domain status and the instance names within the domain.
import java.util.HashMap;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import com.ibm.streams.management.domain.DomainMXBean;
import com.ibm.streams.management.ObjectNameBuilder;
// javac -cp com.ibm.streams.management.jmxmp.jar:com.ibm.streams.management.mx.jar Client.java
// java -cp .;com.ibm.streams.management.jmxmp.jar:com.ibm.streams.management.mx.jar:jmxremote_optional.jar
// Client service:jmx:jmxmp://server:9975 domainA user password
// Note: It is important to include com.ibm.streams.management.jmxmp.jar in the class path before jmxremote_optional.jar
public class Client {
public static void main(String [] args) {
try {
String jmxUrl = args[0]; // use streamtool getjmxconnect to find
String domainName = args[1];
String user = args[2]; // use streamtool setacl or streamtool setdomainacl to assign required permissions
String password = args[3];
HashMap<String,Object> env = new HashMap<String,Object>();
String [] creds = {user, password};
env.put("jmx.remote.credentials", creds);
env.put("jmx.remote.protocol.provider.pkgs", "com.ibm.streams.management");
JMXConnector jmxc = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrl), env);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName objName = ObjectNameBuilder.domain(domainName);
DomainMXBean domain = JMX.newMXBeanProxy(mbsc, objName, DomainMXBean.class, true);
System.out.println("Status: " + domain.getStatus());
System.out.println("Instances: " + domain.getInstances());
}
catch (Exception e) {
e.printStackTrace();
}
}
}