下面列出了怎么用javax.management.remote.JMXConnectionNotification的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* If the handback object passed is an AgentImpl, updates the JMX client count
*
* @param notification
* JMXConnectionNotification for change in client connection status
* @param handback
* An opaque object which helps the listener to associate information
* regarding the MBean emitter. This object is passed to the MBean
* during the addListener call and resent, without modification, to
* the listener. The MBean object should not use or modify the
* object. (NOTE: copied from javax.management.NotificationListener)
*/
@SuppressFBWarnings(value="BC_UNCONFIRMED_CAST", justification="Only JMXConnectionNotification instances are used.")
public void handleNotification(Notification notification, Object handback) {
if (handback instanceof AgentImpl) {
AgentImpl agent = (AgentImpl) handback;
JMXConnectionNotification jmxNotifn =
(JMXConnectionNotification) notification;
LogWriterI18n logWriter = agent.getLogWriterI18n();
logWriter.fine("Connection notification for connection id : '" +
jmxNotifn.getConnectionId() + "'");
agent.updateRmiClientsCount();
}
}
private MBeanServerConnection establishMBeanServerConnection() throws IOException {
if (jmxUser != null ^ jmxPassword != null) {
throw new ParameterException(commandSpec.commandLine(), "Both --jmx-user and --jmx-password are required when either is used.");
}
Map<String, String[]> jmxEnvironment = new HashMap<>();
if (jmxUser != null && jmxPassword != null) {
jmxEnvironment.put(JMXConnector.CREDENTIALS, new String[]{jmxUser, jmxPassword});
}
final JMXConnector connector = JMXConnectorFactory.connect(jmxServiceURL, jmxEnvironment);
final MBeanServerConnection mBeanServerConnection = connector.getMBeanServerConnection();
connector.addConnectionNotificationListener((notification, handback) -> {
if (notification.getType().equals(JMXConnectionNotification.CLOSED)) {
logger.error("JMX connection to {} closed.", jmxServiceURL);
Runtime.getRuntime().exit(-1);
}
}, null, null);
return mBeanServerConnection;
}
/**
* If the handback object passed is an AgentImpl, updates the JMX client count
*
* @param notification
* JMXConnectionNotification for change in client connection status
* @param handback
* An opaque object which helps the listener to associate information
* regarding the MBean emitter. This object is passed to the MBean
* during the addListener call and resent, without modification, to
* the listener. The MBean object should not use or modify the
* object. (NOTE: copied from javax.management.NotificationListener)
*/
@SuppressFBWarnings(value="BC_UNCONFIRMED_CAST", justification="Only JMXConnectionNotification instances are used.")
public void handleNotification(Notification notification, Object handback) {
if (handback instanceof AgentImpl) {
AgentImpl agent = (AgentImpl) handback;
JMXConnectionNotification jmxNotifn =
(JMXConnectionNotification) notification;
LogWriterI18n logWriter = agent.getLogWriterI18n();
logWriter.fine("Connection notification for connection id : '" +
jmxNotifn.getConnectionId() + "'");
agent.updateRmiClientsCount();
}
}
protected void lostNotifs(String message, long number) {
final String notifType = JMXConnectionNotification.NOTIFS_LOST;
final JMXConnectionNotification n =
new JMXConnectionNotification(notifType,
RMIConnector.this,
connectionId,
clientNotifCounter++,
message,
Long.valueOf(number));
sendNotification(n);
}
protected void doStart() throws IOException {
// Get RMIServer stub from directory or URL encoding if needed.
RMIServer stub;
try {
stub = (rmiServer!=null)?rmiServer:
findRMIServer(jmxServiceURL, env);
} catch (NamingException ne) {
throw new IOException("Failed to get a RMI stub: "+ne);
}
// Connect IIOP Stub if needed.
stub = connectStub(stub,env);
// Calling newClient on the RMIServer stub.
Object credentials = env.get(CREDENTIALS);
connection = stub.newClient(credentials);
// notif issues
final ClientListenerInfo[] old = rmiNotifClient.preReconnection();
reconnectNotificationListeners(old);
connectionId = getConnectionId();
Notification reconnectedNotif =
new JMXConnectionNotification(JMXConnectionNotification.OPENED,
this,
connectionId,
clientNotifSeqNo++,
"Reconnected to server",
null);
sendNotification(reconnectedNotif);
}
private void handle(Notification n, Object h) {
if (!(n instanceof JMXConnectionNotification)) {
System.out.println("LostListener received strange notif: " +
notificationString(n));
result.failed = true;
result.notifyAll();
return;
}
JMXConnectionNotification jn = (JMXConnectionNotification) n;
if (!jn.getType().equals(jn.NOTIFS_LOST)) {
System.out.println("Ignoring JMXConnectionNotification: " +
notificationString(jn));
return;
}
final String msg = jn.getMessage();
if ((!msg.startsWith("Dropped ")
|| !msg.endsWith("classes were missing locally"))
&& (!msg.startsWith("Not serializable: "))) {
System.out.println("Surprising NOTIFS_LOST getMessage: " +
msg);
}
if (!(jn.getUserData() instanceof Long)) {
System.out.println("JMXConnectionNotification userData " +
"not a Long: " + jn.getUserData());
result.failed = true;
} else {
int lost = ((Long) jn.getUserData()).intValue();
result.lostCount += lost;
if (result.lostCount == NNOTIFS*2)
result.notifyAll();
}
}
public void handleNotification(Notification n, Object h) {
/* Connectors can handle unserializable notifications in
one of two ways. Either they can arrange for the
client to get a NotSerializableException from its
fetchNotifications call (RMI connector), or they can
replace the unserializable notification by a
JMXConnectionNotification.NOTIFS_LOST (JMXMP
connector). The former case is handled by code within
the connector client which will end up sending a
NOTIFS_LOST to our LostListener. The logic here
handles the latter case by converting it into the
former.
*/
if (n instanceof JMXConnectionNotification
&& n.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
lostListener.handleNotification(n, h);
return;
}
synchronized (result) {
if (!n.getType().equals("interesting")
|| !n.getUserData().equals("known")) {
System.out.println("TestListener received strange notif: "
+ notificationString(n));
result.failed = true;
result.notifyAll();
} else {
result.knownCount++;
if (result.knownCount == NNOTIFS)
result.notifyAll();
}
}
}
public void handleNotification(Notification n, Object o) {
if (n instanceof JMXConnectionNotification) {
JMXConnectionNotification jn = (JMXConnectionNotification)n;
if (JMXConnectionNotification.FAILED.equals(jn.getType())) {
failed = true;
}
}
}
public void handleNotification(Notification n, Object hb) {
if (n instanceof JMXConnectionNotification) {
if (JMXConnectionNotification.FAILED.equals(n.getType()) || JMXConnectionNotification.CLOSED.equals(n.getType())) {
markAsDead();
}
}
}
public void handleNotification(Notification n, Object hb) {
// treat the client notif to know the end
if (n instanceof JMXConnectionNotification) {
if (!JMXConnectionNotification.NOTIFS_LOST.equals(n.getType())) {
clientState = n.getType();
System.out.println(
">>> The client state has been changed to: "+clientState);
synchronized(lock) {
lock.notifyAll();
}
}
return;
}
System.out.println(">>> Do sleep to make reconnection.");
synchronized(lock) {
try {
lock.wait(listenerSleep);
} catch (Exception e) {
// OK
}
}
}
protected void lostNotifs(String message, long number) {
final String notifType = JMXConnectionNotification.NOTIFS_LOST;
final JMXConnectionNotification n =
new JMXConnectionNotification(notifType,
RMIConnector.this,
connectionId,
clientNotifCounter++,
message,
Long.valueOf(number));
sendNotification(n);
}
public void handleNotification(Notification n, Object o) {
if (n instanceof JMXConnectionNotification) {
JMXConnectionNotification jn = (JMXConnectionNotification)n;
if (JMXConnectionNotification.FAILED.equals(jn.getType())) {
failed = true;
}
}
}
public void handleNotification(Notification n, Object o) {
if (n instanceof JMXConnectionNotification) {
JMXConnectionNotification jn = (JMXConnectionNotification)n;
if (JMXConnectionNotification.FAILED.equals(jn.getType())) {
failed = true;
}
}
}
@Override
public void handleNotification(Notification notification, Object handback) {
if (JMXConnectionNotification.class.isInstance(notification)) {
JMXConnectionNotification connNotif = (JMXConnectionNotification)notification;
if (JMXConnectionNotification.CLOSED.equals(connNotif.getType()) ||
JMXConnectionNotification.FAILED.equals(connNotif.getType())) {
this.invoker.isConnected.set(false);
this.invoker.resetClusterId();
if (!this.invoker.isSelfDisconnect.get()) {
Gfsh.getCurrentInstance().notifyDisconnect(this.invoker.toString());
}
}
}
}
public void handleNotification(Notification n, Object hb) {
// treat the client notif to know the end
if (n instanceof JMXConnectionNotification) {
if (!JMXConnectionNotification.NOTIFS_LOST.equals(n.getType())) {
clientState = n.getType();
System.out.println(
">>> The client state has been changed to: "+clientState);
synchronized(lock) {
lock.notifyAll();
}
}
return;
}
System.out.println(">>> Do sleep to make reconnection.");
synchronized(lock) {
try {
lock.wait(listenerSleep);
} catch (Exception e) {
// OK
}
}
}
public void handleNotification(Notification n, Object h) {
/* Connectors can handle unserializable notifications in
one of two ways. Either they can arrange for the
client to get a NotSerializableException from its
fetchNotifications call (RMI connector), or they can
replace the unserializable notification by a
JMXConnectionNotification.NOTIFS_LOST (JMXMP
connector). The former case is handled by code within
the connector client which will end up sending a
NOTIFS_LOST to our LostListener. The logic here
handles the latter case by converting it into the
former.
*/
if (n instanceof JMXConnectionNotification
&& n.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
lostListener.handleNotification(n, h);
return;
}
synchronized (result) {
if (!n.getType().equals("interesting")
|| !n.getUserData().equals("known")) {
System.out.println("TestListener received strange notif: "
+ notificationString(n));
result.failed = true;
result.notifyAll();
} else {
result.knownCount++;
if (result.knownCount == NNOTIFS)
result.notifyAll();
}
}
}
private void handle(Notification n, Object h) {
if (!(n instanceof JMXConnectionNotification)) {
System.out.println("LostListener received strange notif: " +
notificationString(n));
result.failed = true;
result.notifyAll();
return;
}
JMXConnectionNotification jn = (JMXConnectionNotification) n;
if (!jn.getType().equals(jn.NOTIFS_LOST)) {
System.out.println("Ignoring JMXConnectionNotification: " +
notificationString(jn));
return;
}
final String msg = jn.getMessage();
if ((!msg.startsWith("Dropped ")
|| !msg.endsWith("classes were missing locally"))
&& (!msg.startsWith("Not serializable: "))) {
System.out.println("Surprising NOTIFS_LOST getMessage: " +
msg);
}
if (!(jn.getUserData() instanceof Long)) {
System.out.println("JMXConnectionNotification userData " +
"not a Long: " + jn.getUserData());
result.failed = true;
} else {
int lost = ((Long) jn.getUserData()).intValue();
result.lostCount += lost;
if (result.lostCount == NNOTIFS*2)
result.notifyAll();
}
}
public void handleNotification(Notification n, Object o) {
if (n instanceof JMXConnectionNotification) {
JMXConnectionNotification jn = (JMXConnectionNotification)n;
if (JMXConnectionNotification.FAILED.equals(jn.getType())) {
failed = true;
}
}
}
private static boolean
mustBeConnectionNotification(Notification notif,
String requiredConnId,
String requiredType) {
if (!(notif instanceof JMXConnectionNotification)) {
System.out.println("Should have been a " +
"JMXConnectionNotification: " +
notif.getClass());
return false;
}
JMXConnectionNotification cnotif = (JMXConnectionNotification) notif;
if (!cnotif.getType().equals(requiredType)) {
System.out.println("Wrong type notif: is \"" + cnotif.getType() +
"\", should be \"" + requiredType + "\"");
return false;
}
if (!cnotif.getConnectionId().equals(requiredConnId)) {
System.out.println("Wrong connection id: is \"" +
cnotif.getConnectionId() + "\", should be \"" +
requiredConnId);
return false;
}
return true;
}
public void handleNotification(Notification n, Object h) {
/* Connectors can handle unserializable notifications in
one of two ways. Either they can arrange for the
client to get a NotSerializableException from its
fetchNotifications call (RMI connector), or they can
replace the unserializable notification by a
JMXConnectionNotification.NOTIFS_LOST (JMXMP
connector). The former case is handled by code within
the connector client which will end up sending a
NOTIFS_LOST to our LostListener. The logic here
handles the latter case by converting it into the
former.
*/
if (n instanceof JMXConnectionNotification
&& n.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
lostListener.handleNotification(n, h);
return;
}
synchronized (result) {
if (!n.getType().equals("interesting")
|| !n.getUserData().equals("known")) {
System.out.println("TestListener received strange notif: "
+ notificationString(n));
result.failed = true;
result.notifyAll();
} else {
result.knownCount++;
if (result.knownCount == NNOTIFS)
result.notifyAll();
}
}
}
/**
* Invoked before sending the specified notification to the listener.
* Returns whether the given notification is to be sent to the listener.
*
* @param notification
* The notification to be sent.
* @return true if the notification has to be sent to the listener, false
* otherwise.
*/
public boolean isNotificationEnabled(Notification notification) {
boolean isThisNotificationEnabled = false;
if (notification.getType().equals(JMXConnectionNotification.OPENED) ||
notification.getType().equals(JMXConnectionNotification.CLOSED) ||
notification.getType().equals(JMXConnectionNotification.FAILED) ) {
isThisNotificationEnabled = true;
}
return isThisNotificationEnabled;
}
protected void doStart() throws IOException {
// Get RMIServer stub from directory or URL encoding if needed.
RMIServer stub;
try {
stub = (rmiServer!=null)?rmiServer:
findRMIServer(jmxServiceURL, env);
} catch (NamingException ne) {
throw new IOException("Failed to get a RMI stub: "+ne);
}
// Connect IIOP Stub if needed.
stub = connectStub(stub,env);
// Calling newClient on the RMIServer stub.
Object credentials = env.get(CREDENTIALS);
connection = stub.newClient(credentials);
// notif issues
final ClientListenerInfo[] old = rmiNotifClient.preReconnection();
reconnectNotificationListeners(old);
connectionId = getConnectionId();
Notification reconnectedNotif =
new JMXConnectionNotification(JMXConnectionNotification.OPENED,
this,
connectionId,
clientNotifSeqNo++,
"Reconnected to server",
null);
sendNotification(reconnectedNotif);
}
protected void doStart() throws IOException {
// Get RMIServer stub from directory or URL encoding if needed.
RMIServer stub;
try {
stub = (rmiServer!=null)?rmiServer:
findRMIServer(jmxServiceURL, env);
} catch (NamingException ne) {
throw new IOException("Failed to get a RMI stub: "+ne);
}
// Connect IIOP Stub if needed.
stub = connectStub(stub,env);
// Calling newClient on the RMIServer stub.
Object credentials = env.get(CREDENTIALS);
connection = stub.newClient(credentials);
// notif issues
final ClientListenerInfo[] old = rmiNotifClient.preReconnection();
reconnectNotificationListeners(old);
connectionId = getConnectionId();
Notification reconnectedNotif =
new JMXConnectionNotification(JMXConnectionNotification.OPENED,
this,
connectionId,
clientNotifSeqNo++,
"Reconnected to server",
null);
sendNotification(reconnectedNotif);
}
public void handleNotification(Notification n, Object h) {
/* Connectors can handle unserializable notifications in
one of two ways. Either they can arrange for the
client to get a NotSerializableException from its
fetchNotifications call (RMI connector), or they can
replace the unserializable notification by a
JMXConnectionNotification.NOTIFS_LOST (JMXMP
connector). The former case is handled by code within
the connector client which will end up sending a
NOTIFS_LOST to our LostListener. The logic here
handles the latter case by converting it into the
former.
*/
if (n instanceof JMXConnectionNotification
&& n.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
lostListener.handleNotification(n, h);
return;
}
synchronized (result) {
if (!n.getType().equals("interesting")
|| !n.getUserData().equals("known")) {
System.out.println("TestListener received strange notif: "
+ notificationString(n));
result.failed = true;
result.notifyAll();
} else {
result.knownCount++;
if (result.knownCount == NNOTIFS)
result.notifyAll();
}
}
}
private void handle(Notification n, Object h) {
if (!(n instanceof JMXConnectionNotification)) {
System.out.println("LostListener received strange notif: " +
notificationString(n));
result.failed = true;
result.notifyAll();
return;
}
JMXConnectionNotification jn = (JMXConnectionNotification) n;
if (!jn.getType().equals(jn.NOTIFS_LOST)) {
System.out.println("Ignoring JMXConnectionNotification: " +
notificationString(jn));
return;
}
final String msg = jn.getMessage();
if ((!msg.startsWith("Dropped ")
|| !msg.endsWith("classes were missing locally"))
&& (!msg.startsWith("Not serializable: "))) {
System.out.println("Surprising NOTIFS_LOST getMessage: " +
msg);
}
if (!(jn.getUserData() instanceof Long)) {
System.out.println("JMXConnectionNotification userData " +
"not a Long: " + jn.getUserData());
result.failed = true;
} else {
int lost = ((Long) jn.getUserData()).intValue();
result.lostCount += lost;
if (result.lostCount == NNOTIFS*2)
result.notifyAll();
}
}
public void handleNotification(Notification n, Object o) {
if (n instanceof JMXConnectionNotification) {
JMXConnectionNotification jn = (JMXConnectionNotification)n;
if (JMXConnectionNotification.FAILED.equals(jn.getType())) {
failed = true;
}
}
}
public void handleNotification(Notification n, Object hb) {
// treat the client notif to know the end
if (n instanceof JMXConnectionNotification) {
if (!JMXConnectionNotification.NOTIFS_LOST.equals(n.getType())) {
clientState = n.getType();
System.out.println(
">>> The client state has been changed to: "+clientState);
synchronized(lock) {
lock.notifyAll();
}
}
return;
}
System.out.println(">>> Do sleep to make reconnection.");
synchronized(lock) {
try {
lock.wait(listenerSleep);
} catch (Exception e) {
// OK
}
}
}
protected void lostNotifs(String message, long number) {
final String notifType = JMXConnectionNotification.NOTIFS_LOST;
final JMXConnectionNotification n =
new JMXConnectionNotification(notifType,
RMIConnector.this,
connectionId,
clientNotifCounter++,
message,
Long.valueOf(number));
sendNotification(n);
}
protected void doStart() throws IOException {
// Get RMIServer stub from directory or URL encoding if needed.
RMIServer stub;
try {
stub = (rmiServer!=null)?rmiServer:
findRMIServer(jmxServiceURL, env);
} catch (NamingException ne) {
throw new IOException("Failed to get a RMI stub: "+ne);
}
// Connect IIOP Stub if needed.
stub = connectStub(stub,env);
// Calling newClient on the RMIServer stub.
Object credentials = env.get(CREDENTIALS);
connection = stub.newClient(credentials);
// notif issues
final ClientListenerInfo[] old = rmiNotifClient.preReconnection();
reconnectNotificationListeners(old);
connectionId = getConnectionId();
Notification reconnectedNotif =
new JMXConnectionNotification(JMXConnectionNotification.OPENED,
this,
connectionId,
clientNotifSeqNo++,
"Reconnected to server",
null);
sendNotification(reconnectedNotif);
}
@Override
public void handleNotification(Notification notification, Object handback) {
JMXConnectionNotification noti = (JMXConnectionNotification) notification;
if (noti.getType().equals(JMXConnectionNotification.CLOSED)) {
disconnect(String.valueOf(handback));
} else if (noti.getType().equals(JMXConnectionNotification.FAILED)) {
disconnect(String.valueOf(handback));
} else if (noti.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
disconnect(String.valueOf(handback));
}
}