Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ lib-cov
*.pid
*.gz
coverage.html
/dev
package-lock.json
.vscode/
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This file is used to list changes made in each version of `jmx` Node.js module.

## 0.8.0 (2020-03-20)

* Update NPM modules
* All APIs now return Promise, except the API inherited from EventEmitter
* Add new API "queryNamesWithObjectName" w.r.t. "queryNames" in "MBeanServerConnection", with only "ObjectName" is support.

## 0.7.0 (2018-03-25)

* Update `java` dependency to `~0.9.0` ([issue #16](https://github.com/zuazo/node-jmx/issues/16), thanks [DarkSorrow](https://github.com/DarkSorrow)).
Expand Down
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# node-jmx

This is a fork of node-jmx modified by Kay Chan. Please checkout "0.8.0" in change log for the changes of this fork.

[![NPM version](https://badge.fury.io/js/jmx.svg)](http://badge.fury.io/js/jmx)
[![Code Climate](https://img.shields.io/codeclimate/maintainability-percentage/zuazo/node-jmx.svg)](https://codeclimate.com/github/zuazo/node-jmx)
[![Build Status](http://img.shields.io/travis/zuazo/node-jmx.svg)](https://travis-ci.org/zuazo/node-jmx)
Expand Down Expand Up @@ -28,30 +30,28 @@ client = jmx.createClient({
});

client.connect();
client.on("connect", function() {

client.getAttribute("java.lang:type=Memory", "HeapMemoryUsage", function(data) {
var used = data.getSync('used');
console.log("HeapMemoryUsage used: " + used.longValue);
// console.log(data.toString());
});
client.on("connect", run);

client.setAttribute("java.lang:type=Memory", "Verbose", true, function() {
console.log("Memory verbose on"); // callback is optional
});
async function run () {
let data = await client.getAttribute("java.lang:type=Memory", "HeapMemoryUsage");
var used = data.getSync('used');
console.log("HeapMemoryUsage used: " + used.longValue);

client.invoke("java.lang:type=Memory", "gc", [], function(data) {
console.log("gc() done");
});
await client.setAttribute("java.lang:type=Memory", "Verbose", true);
console.log("Memory verbose on");

});
await client.invoke("java.lang:type=Memory", "gc", []);
console.log("gc() done");
}
```

Client can be created by service URL instead:
```js
client = jmx.createClient({
service: "service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi"
});
```

You can check the [node-java documentation](https://github.com/nearinfinity/node-java/blob/master/README.md#quick-examples) to learn how to work with java objects in node.js.

## Documentation
Expand Down Expand Up @@ -80,41 +80,41 @@ Connects to the JMX server. Emits `connect` event when done.

Disconnects from the JMX server. Emits `disconnect` event when done.

### Client.getAttribute(mbean, attribute, callback)
### Client.getAttribute(mbean, attribute)

Returns an attribute from a MBean.

* `mbean` - MBean query address as string. For example "java.lang:type=Memory".
* `attribute` - Attribute name as string.
* `callback(attrValue)`

### Client.getAttributes(mbean, attributes, callback)
### Client.getAttributes(mbean, attributes)

Returns an attribute list from a MBean.

* `mbean` - MBean query address as string. For example "java.lang:type=Memory".
* `attributes` - Attribute names as an array of strings.
* `callback(attrValue)`

### Client.getDefaultDomain(callback)
### Client.getDefaultDomain()

Returns the default domain as string.

* `callback(domainName)`

### Client.getDomains(callback)
### Client.getDomains()

Returns an array of domain names.

* `callback(domainsArray)`

### Client.getMBeanCount(callback)
### Client.getMBeanCount()

Returns total the number of MBeans.

* `callback(mbeanCount)`

### Client.invoke(mbean, methodName, params, [signature,] [callback])
### Client.invoke(mbean, methodName, params, [signature])

Invokes a MBean operation.

Expand All @@ -124,21 +124,25 @@ Invokes a MBean operation.
* `signature` (optional) - An array with the signature of the *params*. Sometimes may be necessary to use this if class names are not correctly detected (gives a *NoSuchMethodException*). For example `[ "int", "java.lang.Integer", "java.lang.String" ]`.
* `callback(returnedValue)`

### Client.listMBeans(callback)
### Client.listMBeans()

Lists server MBeans. Callback returns an array of strings containing MBean names.

### Client.on(event, callback)

Adds a listener for the especified event.

### Client.queryNamesWithObjectName(objectNameSearchString)

Search names with object name query, referring to [MBeanServerConnection.queryNames](https://docs.oracle.com/javase/7/docs/api/javax/management/MBeanServerConnection.html#queryNames)

#### events

* `connect`
* `disconnect`
* `error` - Passes the error as first parameter to the callback function.

### Client.setAttribute(mbean, attribute, value, [className,] [callback])
### Client.setAttribute(mbean, attribute, value, [className])

Changes an attribute value of the MBean.

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Client = require("./lib/client"),
var Client = require("./lib/client2"),
assert = require("assert");

function createClient(options) {
Expand Down
16 changes: 16 additions & 0 deletions lib/adapters/mbeanServerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ MBeanServerConnection.prototype.getAttribute = function(name, attribute, callbac
);
};

// Gets the names of MBeans by ObjectName without query
MBeanServerConnection.prototype.queryNamesWithObjectName = function(name, callback) {
// TODO: confirm about ObjectMame
this.javaReflection.invokeMethod(
this.mbeanServerConnection,
'queryNames',
[ "javax.management.ObjectName", "javax.management.QueryExp" ], // methodParamsClass
[ name, null ], // methodParams
function (data) {
// convert data to string array
const result = data.toArraySync().map(d => d.toStringSync());
callback(result);
}
);
};

MBeanServerConnection.prototype.getDefaultDomain = function(callback) {
this.javaReflection.invokeMethod(
this.mbeanServerConnection,
Expand Down
4 changes: 4 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Client.prototype.getAttribute = function(mbean, attribute, callback) {
this.javaJmx.getAttribute(mbean, attribute, callback);
};

Client.prototype.queryNamesWithObjectName = function(attribute, callback) {
this.javaJmx.queryNamesWithObjectName(attribute, callback);
};

Client.prototype.getDefaultDomain = function(callback) {
this.javaJmx.getDefaultDomain(callback);
};
Expand Down
132 changes: 132 additions & 0 deletions lib/client2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
var Client = require("./client")

{
let tmp = Client.prototype.connect
Client.prototype.connect = tmp
}

{
let tmp = Client.prototype.disconnect
Client.prototype.disconnect = tmp
}

{
let tmp = Client.prototype.getAttributes
Client.prototype.getAttributes = function(mbean, attributes) {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [mbean, attributes, resolve])
} catch (e) {
reject(e)
}
})
}
}

{
let tmp = Client.prototype.getAttribute
Client.prototype.getAttribute = function(mbean, attributes) {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [mbean, attributes, resolve])
} catch (e) {
reject(e)
}
})
}
}

{
let tmp = Client.prototype.queryNamesWithObjectName
Client.prototype.queryNamesWithObjectName = function(attribute) {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [attribute, resolve])
} catch (e) {
reject(e)
}
})
}
}

// TODO:
{
let tmp = Client.prototype.getDefaultDomain
Client.prototype.getDefaultDomain = function() {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [resolve])
} catch (e) {
reject(e)
}
})
}
}

{
let tmp = Client.prototype.getDomains
Client.prototype.getDomains = function() {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [resolve])
} catch (e) {
reject(e)
}
})
}
}

{
let tmp = Client.prototype.getMBeanCount
Client.prototype.getMBeanCount = function() {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [resolve])
} catch (e) {
reject(e)
}
})
}
}

{
let tmp = Client.prototype.invoke
Client.prototype.invoke = function(mbean, methodName, params, signatureOrCallback) {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [mbean, methodName, params, signatureOrCallback, resolve])
} catch (e) {
reject(e)
}
})
}
}

{
let tmp = Client.prototype.listMBeans
Client.prototype.listMBeans = function() {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [resolve])
} catch (e) {
reject(e)
}
})
}
}

{
let tmp = Client.prototype.setAttribute
Client.prototype.setAttribute = function(mbean, attribute, value, classNameOrCallback) {
return new Promise((resolve, reject) => {
try {
tmp.apply(this, [mbean, attribute, value, classNameOrCallback, resolve])
} catch (e) {
reject(e)
}
})
}
}


module.exports = Client;
6 changes: 6 additions & 0 deletions lib/javaJmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ JavaJmx.prototype.getAttribute = function(mbean, attributeName, callback) {
});
};

JavaJmx.prototype.queryNamesWithObjectName = function(objectNameStr, callback) {
var self = this;
var objectName = java.newInstanceSync("javax.management.ObjectName", objectNameStr);
self.mbeanServerConnection.queryNamesWithObjectName(objectName, callback);
}

JavaJmx.prototype.getDefaultDomain = function(callback) {
this.mbeanServerConnection.getDefaultDomain(callback);
};
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
"node": ">=0.10.0"
},
"dependencies": {
"async": "~1.5.0",
"java": "~0.9.0"
"async": "^3.2.0",
"java": "~0.12.1"
},
"devDependencies": {
"devDependencies": {
"mocha": "~2.0.1",
"jscoverage" : "~0.5.9",
"mocha-lcov-reporter" : "0.0.1",
"jscoverage": "~0.5.9",
"mocha-lcov-reporter": "0.0.1",
"coveralls": "2.11.2"
}
}