@Test public void changeExportedFolderRemotely() throws MalformedURLException, IOException, MalformedObjectNameException, IntrospectionException { // connect through RMI and get the proxy JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server"); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); ObjectName agentName = ObjectName.getInstance("myDomain:name=Alice,type=tutorial"); /*DynamicMBean proxy = JMX.newMBeanProxy(mbs, agentName, DynamicMBean.class); */ DynamicProxy proxy = new DynamicProxy(agentName, mbsc); // convert the proxy to standard Folder Folder jmxFolder = new DynamicMBeanToFolderAdapter(proxy); //jmxFolder.getProperty("name").setValue("Cecil Corn"); String newName = "Cecil Corn"; jmxFolder.getFolder("knows").getProperty("name").setValue(newName); //mbsc.setAttribute(agentName, new Attribute("name", "Bob")); // waitForEnterPressed(); assertTrue(person.knows.name.equals(newName)); System.out.println("---/// TEST OK ///---"); }
public AgentJMXProxy(String agentJmxAddress) { // connect through RMI and get the proxy String[] strs = agentJmxAddress.split("\\" + AgentJMXComponents.JMX_SERVER_AGENT_NAME_DELIM); String jmxService = strs[0]; String objectName = strs[1]; try { JMXServiceURL url = new JMXServiceURL(jmxService); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); mbsc = jmxc.getMBeanServerConnection(); agentName = ObjectName.getInstance(objectName); proxy = new DynamicProxy(agentName, mbsc); agentFlag = new FlagJMXProxy<IAgentState>(agentName, mbsc, AgentMBeanAdapter.AGENT_STATE_FLAG_NAME); agentId = new AgentIdJMXProxy(this); } catch (Exception e) { throw new PogamutJMXException("Can't create AgentJMXProxy.", e, this); } }
private static MBeanServerConnection initMBeanServerConnection() { try { String ip = Config.getProperty("jmx_ip"); String port = Config.getProperty("jmx_port"); String jmxURL = "service:jmx:rmi:///jndi/rmi://" + ip + ":" + port + "/jmxrmi"; JMXServiceURL serviceURL = new JMXServiceURL(jmxURL); Map map = new HashMap(); String[] credentials = Config.getProperty("credentials").split(","); map.put("jmx.remote.credentials", credentials); JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map); mbsconnector = connector.getMBeanServerConnection(); } catch (IOException e) { logger.error("get connector error" + e); } return mbsconnector; }
MBeanWrapper( int jvmPort ) throws MonitorConfigurationException { this.jvmPort = jvmPort; try { JMXConnector connector = JMXConnectorFactory.newJMXConnector(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + this.jvmPort + "/jmxrmi"), null); connector.connect(); this.connection = connector.getMBeanServerConnection(); } catch (Exception e) { final String msg = "Error initializing the JMV monitor. Unable to connect to JVM at port " + this.jvmPort; log.error(msg, e); throw new MonitorConfigurationException(msg, e); } }
@Test public void t3testStartSingleReader() throws IOException { //create a test case file TestCaseHelper.createTestCaseFile(testSpaceDir,"test-file.test",10000,100); //connect to client final JMXConnector connection = getConnection(); final MBeanServerConnection mbs = getMbeanServer(connection); final ObjectName readerObject = getReaderObjectName(mbs); //set params JmxClientReaderHelper.setFilePath(mbs,readerObject,testSpaceDir); JmxClientReaderHelper.setFileName(mbs,readerObject,"test-file.test"); JmxClientReaderHelper.setChunkSize(mbs,readerObject,16000); JmxClientReaderHelper.setTakeMeasurements(mbs,readerObject,false); JmxClientReaderHelper.setMeasurementVolume(mbs,readerObject,-1); JmxClientReaderHelper.setReaderType(mbs,readerObject,ReaderType.rafr.getType()); JmxClientReaderHelper.startSingleReader(mbs,readerObject); ThreadManager.nap(2500);//let the reader read a bit :) Assert.assertEquals(1,JmxClientReaderHelper.getReaderPoolSize(mbs,readerObject)); final String response = JmxClientReaderHelper.stopServicePool(mbs,readerObject); log.log(Level.INFO, response); }
/** * read standard attributes at first! * @throws IOException ioe */ @Test public void t1testClientNetworkStandardAttributes() throws IOException { //connect to server final JMXConnector connection = JmxConnectionHelper.buildJmxMPConnector(JMXSERVERIP,clientObj.getConnectorSystemPort()); MBeanServerConnection mbs = JmxServerHelper.getMBeanServer(connection); Assert.assertNotSame(0,mbs.getMBeanCount()); ObjectName network = JmxServerHelper.findObjectName(mbs,"de.b4sh.byter","Network"); Assert.assertNotNull(network); //assert the attributes final boolean executorStatus = JmxClientNetworkHelper.getExecutorServiceStatus(mbs,network); Assert.assertEquals(false,executorStatus); final int networkBufferSize = JmxClientNetworkHelper.getNetworkBufferSize(mbs,network); Assert.assertEquals(8192,networkBufferSize); final String hostaddress = JmxClientNetworkHelper.getServerHostAddress(mbs,network); Assert.assertEquals("localhost",hostaddress); final int hostport = JmxClientNetworkHelper.getServerHostPort(mbs,network); Assert.assertEquals(0,hostport); final int targetPregenChunkSize = JmxClientNetworkHelper.getTargetPregenChunkSize(mbs,network); Assert.assertEquals(8192,targetPregenChunkSize); final long transmitTarget = JmxClientNetworkHelper.getTransmitTarget(mbs,network); Assert.assertEquals(8192,transmitTarget); }
/** * create a new JMX Connection with auth when username and password is set. */ public static MBeanServerConnection createJMXConnection(String url, String host, String port, String username, String password) throws MalformedURLException, IOException { String urlForJMX; if (url != null) urlForJMX = url; else urlForJMX = JMX_SERVICE_PREFIX + host + ":" + port + JMX_SERVICE_SUFFIX; Map<String, String[]> environment = null; if (username != null && password != null) { String[] credentials = new String[2]; credentials[0] = username; credentials[1] = password; environment = new HashMap<String, String[]>(); environment.put(JMXConnector.CREDENTIALS, credentials); } return JMXConnectorFactory.connect(new JMXServiceURL(urlForJMX), environment).getMBeanServerConnection(); }
private static void connect(String pid, String address) throws Exception { if (address == null) { throw new RuntimeException("Local connector address for " + pid + " is null"); } System.out.println("Connect to process " + pid + " via: " + address); JMXServiceURL url = new JMXServiceURL(address); JMXConnector c = JMXConnectorFactory.connect(url); MBeanServerConnection server = c.getMBeanServerConnection(); System.out.println("Connected."); RuntimeMXBean rt = newPlatformMXBeanProxy(server, RUNTIME_MXBEAN_NAME, RuntimeMXBean.class); System.out.println(rt.getName()); // close the connection c.close(); }
private static void tryConnect(int port, boolean shouldSucceed) throws Exception { String jmxUrlStr = String.format( "service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port); JMXServiceURL url = new JMXServiceURL(jmxUrlStr); HashMap<String, ?> env = new HashMap<>(); boolean succeeded; try { JMXConnector c = JMXConnectorFactory.connect(url, env); c.getMBeanServerConnection(); succeeded = true; } catch(Exception ex) { succeeded = false; } if (succeeded && !shouldSucceed) { throw new Exception("Could connect to agent, but should not have been possible"); } if (!succeeded && shouldSucceed) { throw new Exception("Could not connect to agent"); } }
public static void main(String[] args) throws Exception { MBeanServer mbs = MBeanServerFactory.newMBeanServer(); Sender sender = new Sender(); mbs.registerMBean(sender, testObjectName); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://"); JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); cs.start(); JMXServiceURL addr = cs.getAddress(); JMXConnector cc = JMXConnectorFactory.connect(addr); try { test(mbs, cs, cc); } finally { cc.close(); cs.stop(); } }
@Test public void testRoutine() throws IOException { //connect to server final JMXConnector connection = JmxConnectionHelper.buildJmxMPConnector(JMXSERVERIP,clientObj.getConnectorSystemPort()); MBeanServerConnection mbs = JmxServerHelper.getMBeanServer(connection); Assert.assertNotSame(0,mbs.getMBeanCount()); ObjectName disc = JmxServerHelper.findObjectName(mbs,"de.b4sh.byter","Disc"); Assert.assertNotNull(disc); //set up env. JmxClientDiscHelper.setByteTarget(mbs,disc, (long) (10* TransformValues.MEGABYTE)); JmxClientDiscHelper.setChunkSize(mbs,disc,64000); JmxClientDiscHelper.setFileName(mbs,disc, StringGenerator.nextRandomString(5)); JmxClientDiscHelper.setOutputPath(mbs,disc,testSpaceDir); JmxClientDiscHelper.setWriterBufferSize(mbs,disc,64000); JmxClientDiscHelper.setWriterImplementation(mbs,disc, WriterType.BufferedWriter.getKey()); //start runtime JmxClientDiscHelper.invokeDiscTest(mbs,disc); }
private void doTest(JMXConnector connector) throws IOException, MalformedObjectNameException, ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException { MBeanServerConnection mbsc = connector.getMBeanServerConnection(); ObjectName objName = new ObjectName("com.redhat.test.jmx:type=NameMBean"); System.out.println("DEBUG: Calling createMBean"); mbsc.createMBean(Name.class.getName(), objName); System.out.println("DEBUG: Calling setAttributes"); AttributeList attList = new AttributeList(); attList.add(new Attribute("FirstName", ANY_NAME)); attList.add(new Attribute("LastName", ANY_NAME)); mbsc.setAttributes(objName, attList); }
private static void dotest(JMXServiceURL url, MBeanServer mbs) throws Exception { JMXConnectorServer server = null; JMXConnector client = null; server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); server.start(); JMXServiceURL outputAddr = server.getAddress(); System.out.println("Server started ["+ outputAddr+ "]"); client = JMXConnectorFactory.newJMXConnector(outputAddr, null); client.connect(); System.out.println("Client connected"); MBeanServerConnection connection = client.getMBeanServerConnection(); System.out.println(connection.getDefaultDomain()); }
public static void main(String[] args) throws Exception { if (Platform.isDebugBuild()) { System.out.println("Running on a debug build. Performance test not applicable. Skipping."); return; } MBeanServer mbs = MBeanServerFactory.newMBeanServer(); Sender sender = new Sender(); mbs.registerMBean(sender, testObjectName); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://"); JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); cs.start(); JMXServiceURL addr = cs.getAddress(); JMXConnector cc = JMXConnectorFactory.connect(addr); try { test(mbs, cs, cc); } finally { cc.close(); cs.stop(); } }
/** * Instantiates a new JMXExecutor targeting the VM indicated by the given host/port combination or a full JMX * Service URL * * @param target a host/port combination on the format "host:port" or a full JMX Service URL of the target VM */ public JMXExecutor(String target) { String urlStr; if (target.matches("^\\w[\\w\\-]*(\\.[\\w\\-]+)*:\\d+$")) { /* Matches "hostname:port" */ urlStr = String.format("service:jmx:rmi:///jndi/rmi://%s/jmxrmi", target); } else if (target.startsWith("service:")) { urlStr = target; } else { throw new IllegalArgumentException("Could not recognize target string: " + target); } try { JMXServiceURL url = new JMXServiceURL(urlStr); JMXConnector c = JMXConnectorFactory.connect(url, new HashMap<>()); mbs = c.getMBeanServerConnection(); } catch (IOException e) { throw new CommandExecutorException("Could not initiate connection to target: " + target, e); } }
private List<JMXConnector> buildClientConnectors(final List<ClientConnection> clientList){ final List<JMXConnector> connectors = new ArrayList<>(); for(ClientConnection clientConnection: clientList){ final JMXConnector con; if(!this.junitRun){ if(!clientConnection.getUsername().contentEquals("null")){ con = this.buildCLientConnectorWithLogin(clientConnection); }else{ con = this.buildCLientConnector(clientConnection); } }else{ con = this.buildTestEnvironmentConnector(this.clientPort); } connectors.add(con); } return connectors; }
/** * Creates the jmxConnector with passed parameters. * @param host host address * @param port port of the host * @param user username (null possible) * @param pass password (null possible) * @return connected JMXConnector * @throws IOException IOException */ public static JMXConnector buildJmxMPConnector(final String host, final int port, final String user, final String pass) throws IOException { try { final JMXServiceURL serviceURL = new JMXServiceURL("jmxmp", host,port); if("null".equals(user) || "null".equals(pass) || user == null || pass == null){ return JMXConnectorFactory.connect(serviceURL); } final Map<String, Object> environment = new HashMap<>(); environment.put("jmx.remote.credentials", new String[]{user,pass}); environment.put(Context.SECURITY_PRINCIPAL, user); environment.put(Context.SECURITY_CREDENTIALS, pass); return JMXConnectorFactory.connect(serviceURL,environment); } catch (MalformedURLException e) { log.log(Level.WARNING, "Malformed ServiceURL in buildJmxConnector"); return null; } }
/** * @return Connection to a remote MBeanServer * @throws cz.cuni.amis.utils.exception.PogamutException */ public MBeanServerConnection getMBeanServerConnection() throws PogamutException { // connect through RMI and get the proxy try { if (mbsc == null) { JMXServiceURL url = getMBeanServerURL(); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); mbsc = jmxc.getMBeanServerConnection(); } return mbsc; } catch (IOException iOException) { throw new PogamutException("IO exception occured while creating remote MBeanServer connector.", iOException); } }
/** * change attributes and read them back afterwards. * @throws IOException ioe */ @Test public void t2testClientNetworkChangeAttributes() throws IOException { //connect to server final JMXConnector connection = JmxConnectionHelper.buildJmxMPConnector(JMXSERVERIP,clientObj.getConnectorSystemPort()); MBeanServerConnection mbs = JmxServerHelper.getMBeanServer(connection); Assert.assertNotSame(0,mbs.getMBeanCount()); ObjectName network = JmxServerHelper.findObjectName(mbs,"de.b4sh.byter","Network"); Assert.assertNotNull(network); //test things here final int networkBufferSizeOld = JmxClientNetworkHelper.getNetworkBufferSize(mbs,network); JmxClientNetworkHelper.changeNetworkBufferSize(mbs,network,12345); final int networkBufferSizeNew = JmxClientNetworkHelper.getNetworkBufferSize(mbs,network); Assert.assertNotEquals(networkBufferSizeNew,networkBufferSizeOld); Assert.assertEquals(networkBufferSizeNew,12345); final String hostaddressOld = JmxClientNetworkHelper.getServerHostAddress(mbs,network); JmxClientNetworkHelper.changeServerHostAddress(mbs,network,"127.0.0.1"); final String hostaddressNew = JmxClientNetworkHelper.getServerHostAddress(mbs,network); Assert.assertNotEquals(hostaddressNew,hostaddressOld); Assert.assertEquals(hostaddressNew,"127.0.0.1"); final int hostportOld = JmxClientNetworkHelper.getServerHostPort(mbs,network); JmxClientNetworkHelper.changeServerHostPort(mbs,network,80); final int hostportNew = JmxClientNetworkHelper.getServerHostPort(mbs,network); Assert.assertNotEquals(hostportNew,hostportOld); Assert.assertEquals(80,hostportNew); final int targetPregenChunkSizeOld = JmxClientNetworkHelper.getTargetPregenChunkSize(mbs,network); JmxClientNetworkHelper.changePregenChunkSize(mbs,network,12345); final int targetPregenChunkSizeNew = JmxClientNetworkHelper.getTargetPregenChunkSize(mbs,network); Assert.assertNotEquals(targetPregenChunkSizeOld,targetPregenChunkSizeNew); Assert.assertEquals(12345,targetPregenChunkSizeNew); final long transmitTargetOld = JmxClientNetworkHelper.getTransmitTarget(mbs,network); JmxClientNetworkHelper.changeTransmitTarget(mbs,network,12345); final long transmitTargetNew = JmxClientNetworkHelper.getTransmitTarget(mbs,network); Assert.assertNotEquals(transmitTargetNew,transmitTargetOld); Assert.assertEquals(transmitTargetNew,12345); }
/** * JMXConnectionInfo的初始化动作 * @param connector * @param desc * @return * @throws IOException */ private JMXConnectionInfo initJMXConnectionInfo(JMXConnector connector,VirtualMachineDescriptor desc) throws IOException { JMXConnectionInfo jmxConnectionInfo = new JMXConnectionInfo(); jmxConnectionInfo.setJmxConnector(connector); jmxConnectionInfo.setCacheKeyId(desc.id()); jmxConnectionInfo.setConnectionServerName(serverName); jmxConnectionInfo.setConnectionQualifiedServerName(desc.displayName()); jmxConnectionInfo.setMBeanServerConnection(connector.getMBeanServerConnection()); jmxConnectionInfo.setValid(true,null); jmxConnectionInfo.setPid(Integer.parseInt(desc.id())); CONNECT_CACHE_LIBRARY.put(serverName + desc.id(),jmxConnectionInfo); return jmxConnectionInfo; }
/** * JMXConnectionInfo的初始化动作 * @param connector * @param commandInfo * @return * @throws IOException */ private JMXConnectionInfo initJMXConnectionInfo(JMXConnector connector,JavaExecCommandInfo commandInfo) throws IOException { JMXConnectionInfo jmxConnectionInfo = new JMXConnectionInfo(); jmxConnectionInfo.setJmxConnector(connector); jmxConnectionInfo.setCacheKeyId(commandInfo.getAppName()); jmxConnectionInfo.setConnectionServerName(commandInfo.getAppName()); jmxConnectionInfo.setConnectionQualifiedServerName(commandInfo.getAppName()); jmxConnectionInfo.setMBeanServerConnection(connector.getMBeanServerConnection()); jmxConnectionInfo.setValid(true,null); jmxConnectionInfo.setPid(-1); CONNECT_CACHE_LIBRARY.put(commandInfo.getAppName(),jmxConnectionInfo); return jmxConnectionInfo; }
/** * JMX连接 * @param url * JMX连接地址 * @param jmxUser * JMX授权用户 null为无授权用户 * @param jmxPassword * JMX授权密码 null为无授权密码 * @param timeout * 超时时间 * @param unit * 超时单位 * @return * @throws IOException */ public static JMXConnector connectWithTimeout( final JMXServiceURL url,String jmxUser,String jmxPassword, long timeout, TimeUnit unit) throws Exception { final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1); ExecuteThreadUtil.execute(() -> { try { JMXConnector connector; if(jmxUser != null && jmxPassword != null){ Map<String,Object> env = new HashMap<>(); String[] credentials = new String[] { jmxUser, jmxPassword }; env.put(JMXConnector.CREDENTIALS, credentials); connector = JMXConnectorFactory.connect(url,env); }else{ connector = JMXConnectorFactory.connect(url,null); } if (!blockingQueue.offer(connector)) connector.close(); } catch (Throwable t) { blockingQueue.offer(t); } }); Object result = BlockingQueueUtil.getResult(blockingQueue,timeout,unit); blockingQueue.clear(); if (result instanceof JMXConnector){ return (JMXConnector) result; }else if (result == null){ throw new SocketTimeoutException("Connect timed out: " + url); }else if(result instanceof Throwable){ throw new IOException("JMX Connect Failed : " + url,((Throwable) result)); } return null; }
public void connect() { close(); // JMXServiceURL jmxUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + _HostPortString + "/jmxrmi"); JmxConnectionDescriptor descriptor = getDescriptor(); JMXServiceURL jmxUrl = descriptor.getJmxServiceUrl(); String userName = descriptor.getUserName(); String password = descriptor.getPassword(); Map<String, String[]> env = null; if (userName != null) { env = new HashMap<String, String[]>(); env.put(JMXConnector.CREDENTIALS, new String[] { userName, password }); } try { _JMXConnector = JMXConnectorFactory.connect(jmxUrl, env); _MBeanServerConnection = _JMXConnector.getMBeanServerConnection(); _Connected = true; addConnectionListener(); addMBeanServerDelegateListener(); // System.out.println("JMX connection opened"); fireConnectionStateChanged(null); } catch (IOException e) { } }
public static void main(String[] args) { try { String host = "localhost"; host = "csseredapp-dev-03"; String port = "8356"; //port = "8356"; String mbeanName = "java.lang:type=Memory"; String attributeName = "HeapMemoryUsage"; JMXServiceURL jmxUrl = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi" ); // jmxUrl = new JMXServiceURL( // "service:jmx:rmi://localhost/jndi/rmi://" + host + ":" + port + "/jmxrmi" ); logger.info("Target: " + jmxUrl ) ; JMXConnector jmxConnection = JMXConnectorFactory.connect( jmxUrl ); logger.info("Got connections") ; CompositeData resultData = (CompositeData) jmxConnection.getMBeanServerConnection() .getAttribute( new ObjectName(mbeanName), attributeName) ; logger.log(Level.INFO, "Got mbean: heapUsed: {0}", resultData.get( "used")) ; Thread.sleep( 5000 ); } catch ( Exception ex ) { logger.log( Level.SEVERE, "Failed connection", ex ); } }
/** * Test if there are all functions available. */ @Test public void testStockAvailableOperations() throws IOException, IntrospectionException, InstanceNotFoundException, ReflectionException { //connect to server final JMXConnector connection = JmxConnectionHelper.buildJmxMPConnector(JMXSERVERIP,serverObj.getConnectorSystemPort()); //get MBeanServerConnection MBeanServerConnection mbsConnection = JmxServerHelper.getMBeanServer(connection); //check if mbeans are registered Assert.assertNotSame(0,mbsConnection.getMBeanCount()); //do actual test ObjectName networkManagerOn = JmxServerHelper.findObjectName(mbsConnection,"de.b4sh.byter","NetworkManager"); final List<MBeanOperationInfo> functions = JmxServerHelper.getOperations(mbsConnection,networkManagerOn); Assert.assertNotEquals(0, functions.size()); }
/** * Creates lazy proxies for the {@code JMXConnector} and {@code MBeanServerConnection} */ private void createLazyConnection() { this.connectorTargetSource = new JMXConnectorLazyInitTargetSource(); TargetSource connectionTargetSource = new MBeanServerConnectionLazyInitTargetSource(); this.connector = (JMXConnector) new ProxyFactory(JMXConnector.class, this.connectorTargetSource).getProxy(this.beanClassLoader); this.connection = (MBeanServerConnection) new ProxyFactory(MBeanServerConnection.class, connectionTargetSource).getProxy(this.beanClassLoader); }
@Test public void testMBeanServer() throws IOException { //connect to server final JMXConnector connection = JmxConnectionHelper.buildJmxMPConnector(JMXSERVERIP,serverObj.getConnectorSystemPort()); //do test MBeanServerConnection mbsConnection = JmxServerHelper.getMBeanServer(connection); Assert.assertNotNull(mbsConnection); if(mbsConnection != null){ log.log(Level.INFO,"Registered MBeanCount on MBS: " + mbsConnection.getMBeanCount()); } log.log(Level.FINEST, "DONE: testMBeanServer"); }
public JMXConnector newJMXConnector(JMXServiceURL serviceURL, Map<String,?> environment) throws IOException { if (!serviceURL.getProtocol().equals("iiop")) { throw new MalformedURLException("Protocol not iiop: " + serviceURL.getProtocol()); } return new RMIConnector(serviceURL, environment); }
public ActiveMQJmxMonitor(String jmxUrl, String brokerName, String[] credentials) throws IOException, MalformedObjectNameException { Map<String, String[]> env = new HashMap<>(); if (credentials != null) { env.put(JMXConnector.CREDENTIALS, credentials); } JMXServiceURL jmxServiceUrl = new JMXServiceURL(jmxUrl); JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceUrl, env); mBeanConnection = jmxConnector.getMBeanServerConnection(); initObjectNames(brokerName); }
protected static String getMemberId(final String jmxManagerHost, final int jmxManagerPort, final String memberName) throws Exception { JMXConnector connector = null; try { connector = JMXConnectorFactory.connect(new JMXServiceURL(String.format( "service:jmx:rmi://%1$s/jndi/rmi://%1$s:%2$d/jmxrmi", jmxManagerHost, jmxManagerPort))); MBeanServerConnection connection = connector.getMBeanServerConnection(); ObjectName objectNamePattern = ObjectName.getInstance("GemFire:type=Member,*"); QueryExp query = Query.eq(Query.attr("Name"), Query.value(memberName)); Set<ObjectName> objectNames = connection.queryNames(objectNamePattern, query); assertNotNull(objectNames); assertFalse(objectNames.isEmpty()); assertEquals(1, objectNames.size()); // final ObjectName objectName = ObjectName.getInstance("GemFire:type=Member,Name=" + // memberName); ObjectName objectName = objectNames.iterator().next(); // System.err.printf("ObjectName for Member with Name (%1$s) is %2$s%n", memberName, // objectName); return ObjectUtils.toString(connection.getAttribute(objectName, "Id")); } finally { IOUtils.close(connector); } }
private void connectAndValidateAsJmxClient(final int jmxPort, final String serverHostName, final boolean useSSL, final boolean useMulti) throws Exception { // JMX RMI Map<String, Object> environment = new HashMap(); if (useSSL) { System.setProperty("javax.net.ssl.keyStore", useMulti ? getMultiKeyKeystore() : getSimpleSingleKeyKeystore()); System.setProperty("javax.net.ssl.keyStoreType", "JKS"); System.setProperty("javax.net.ssl.keyStorePassword", "password"); System.setProperty("javax.net.ssl.trustStore", useMulti ? getMultiKeyTruststore() : getSimpleSingleKeyKeystore()); System.setProperty("javax.net.ssl.trustStoreType", "JKS"); System.setProperty("javax.net.ssl.trustStorePassword", "password"); environment.put("com.sun.jndi.rmi.factory.socket", new SslRMIClientSocketFactory()); } JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + serverHostName + ":" + jmxPort + "/jndi/rmi://" + serverHostName + ":" + jmxPort + "/jmxrmi"); JMXConnector jmxConnector = JMXConnectorFactory.connect(url, environment); try { MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection(); ObjectName mbeanName = new ObjectName("GemFire:service=System,type=Distributed"); // Get MBean proxy instance that will be used to make calls to registered MBean DistributedSystemMXBean distributedSystemMXBean = JMX.newMBeanProxy(mbeanServerConnection, mbeanName, DistributedSystemMXBean.class, true); assertEquals(1, distributedSystemMXBean.getMemberCount()); assertEquals(1, distributedSystemMXBean.getLocatorCount()); } finally { jmxConnector.close(); } }
@Override public JMXConnector newJMXConnector(final JMXServiceURL url, final Map<String, ?> environment) throws MalformedURLException { final String protocol = url.getProtocol(); if (!SimpleJmx.PROTOCOL.equals(protocol)) { throw new MalformedURLException( "Invalid protocol '" + protocol + "' for provider " + this.getClass().getName()); } return new ClientConnector(url, environment); }
@Override public @NotNull JMXConnector connect() throws IOException { final Map<String, Object> environment = environment(); if (ssl) { environment.put(Context.SECURITY_PROTOCOL, "ssl"); final SslRMIClientSocketFactory clientSocketFactory = new SslRMIClientSocketFactory(); environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, clientSocketFactory); environment.put("com.sun.jndi.rmi.factory.socket", clientSocketFactory); } final JMXServiceURL address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + address() + "/jmxrmi"); return JMXConnectorFactory.connect(address, environment); }
/** * Test if there are Controller and NetworkManager registered to MBeanServer */ @Test public void testRegisteredObjects() throws IOException { //connect to server final JMXConnector connection = JmxConnectionHelper.buildJmxMPConnector(JMXSERVERIP,serverObj.getConnectorSystemPort()); //get MBeanServerConnection MBeanServerConnection mbsConnection = JmxServerHelper.getMBeanServer(connection); //do test Assert.assertNotSame(0,mbsConnection.getMBeanCount()); //check if there are beans registered //look for all standard mbeans List<ObjectName> standardMbeans = JmxServerHelper.findObjectNames(mbsConnection,"de.b4sh.byter"); Assert.assertNotEquals(0,standardMbeans.size()); //see if there is a network manager registered to ObjectName networkManager = null; for(ObjectName on: standardMbeans){ if(on.getCanonicalName().contains("NetworkManager")){ networkManager = on; } } Assert.assertNotNull(networkManager); }
@Override public @NotNull JMXConnector connect() throws IOException { final JMXServiceURL address = new JMXServiceURL("service:jmx:jmxmp://" + address()); final javax.management.remote.jmxmp.JMXMPConnector connector = new javax.management.remote.jmxmp.JMXMPConnector(address, environment()); connector.connect(); return connector; }
public JMXConnector newJMXConnector(JMXServiceURL serviceURL, Map<String,?> environment) throws IOException { if (!serviceURL.getProtocol().equals("rmi")) { throw new MalformedURLException("Protocol not rmi: " + serviceURL.getProtocol()); } return new RMIConnector(serviceURL, environment); }