use of javax.management.remote.JMXConnectionNotification in project camel by apache.
the class NotificationXmlFormatter method format.
public String format(Notification aNotification) throws NotificationFormatException {
ObjectHelper.notNull(jaxbContext, "jaxbContext");
NotificationEventType jaxb;
boolean wrap = false;
if (aNotification instanceof AttributeChangeNotification) {
AttributeChangeNotification ac = (AttributeChangeNotification) aNotification;
jaxb = mObjectFactory.createAttributeChangeNotification().withAttributeName(ac.getAttributeName()).withAttributeType(ac.getAttributeType()).withNewValue(ac.getNewValue() == null ? null : String.valueOf(ac.getNewValue())).withOldValue(ac.getOldValue() == null ? null : String.valueOf(ac.getOldValue()));
} else if (aNotification instanceof JMXConnectionNotification) {
jaxb = mObjectFactory.createJMXConnectionNotification().withConnectionId(((JMXConnectionNotification) aNotification).getConnectionId());
} else if (aNotification instanceof MBeanServerNotification) {
jaxb = mObjectFactory.createMBeanServerNotification().withMBeanName(String.valueOf(((MBeanServerNotification) aNotification).getMBeanName()));
} else if (aNotification instanceof MonitorNotification) {
MonitorNotification mn = (MonitorNotification) aNotification;
jaxb = mObjectFactory.createMonitorNotification().withDerivedGauge(String.valueOf(mn.getDerivedGauge())).withObservedAttribute(mn.getObservedAttribute()).withObservedObject(String.valueOf(mn.getObservedObject())).withTrigger(String.valueOf(mn.getTrigger()));
} else if (aNotification instanceof RelationNotification) {
RelationNotification rn = (RelationNotification) aNotification;
jaxb = mObjectFactory.createRelationNotification().withObjectName(String.valueOf(rn.getObjectName())).withRelationId(rn.getRelationId()).withRelationTypeName(rn.getRelationTypeName()).withRoleName(rn.getRoleName());
if (rn.getNewRoleValue() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getNewRoleValue());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withNewRoleValue(ont);
}
if (rn.getOldRoleValue() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getOldRoleValue());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withOldRoleValue(ont);
}
if (rn.getMBeansToUnregister() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getMBeansToUnregister());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withMBeansToUnregister(ont);
}
} else if (aNotification instanceof TimerNotification) {
jaxb = mObjectFactory.createTimerNotification().withNotificationId(((TimerNotification) aNotification).getNotificationID());
} else {
jaxb = mObjectFactory.createNotificationEventType();
wrap = true;
}
// add all of the common properties
jaxb.withMessage(aNotification.getMessage()).withSequence(aNotification.getSequenceNumber()).withSource(String.valueOf(aNotification.getSource())).withTimestamp(aNotification.getTimeStamp()).withType(aNotification.getType());
if (aNotification.getUserData() != null) {
jaxb.withUserData(String.valueOf(aNotification.getUserData()));
}
try {
DatatypeFactory df = getDatatypeFactory();
Date date = new Date(aNotification.getTimeStamp());
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
jaxb.withDateTime(df.newXMLGregorianCalendar(gc));
Object bean = wrap ? mObjectFactory.createNotificationEvent(jaxb) : jaxb;
StringWriter sw = new StringWriter();
// must create a new marshaller as its not thread safe
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(bean, sw);
return sw.toString();
} catch (JAXBException e) {
throw new NotificationFormatException(e);
} catch (DatatypeConfigurationException e) {
throw new NotificationFormatException(e);
}
}
use of javax.management.remote.JMXConnectionNotification in project jdk8u_jdk by JetBrains.
the class RMIConnector method close.
// allows to do close after setting the flag "terminated" to true.
// It is necessary to avoid a deadlock, see 6296324
private synchronized void close(boolean intern) throws IOException {
final boolean tracing = logger.traceOn();
final boolean debug = logger.debugOn();
final String idstr = (tracing ? "[" + this.toString() + "]" : null);
if (!intern) {
//
if (terminated) {
if (closeException == null) {
if (tracing)
logger.trace("close", idstr + " already closed.");
return;
}
} else {
terminated = true;
}
}
if (closeException != null && tracing) {
//
if (tracing) {
logger.trace("close", idstr + " had failed: " + closeException);
logger.trace("close", idstr + " attempting to close again.");
}
}
String savedConnectionId = null;
if (connected) {
savedConnectionId = connectionId;
}
closeException = null;
if (tracing)
logger.trace("close", idstr + " closing.");
if (communicatorAdmin != null) {
communicatorAdmin.terminate();
}
if (rmiNotifClient != null) {
try {
rmiNotifClient.terminate();
if (tracing)
logger.trace("close", idstr + " RMI Notification client terminated.");
} catch (RuntimeException x) {
closeException = x;
if (tracing)
logger.trace("close", idstr + " Failed to terminate RMI Notification client: " + x);
if (debug)
logger.debug("close", x);
}
}
if (connection != null) {
try {
connection.close();
if (tracing)
logger.trace("close", idstr + " closed.");
} catch (NoSuchObjectException nse) {
// OK, the server maybe closed itself.
} catch (IOException e) {
closeException = e;
if (tracing)
logger.trace("close", idstr + " Failed to close RMIServer: " + e);
if (debug)
logger.debug("close", e);
}
}
// Clean up MBeanServerConnection table
//
rmbscMap.clear();
if (savedConnectionId != null) {
Notification closedNotif = new JMXConnectionNotification(JMXConnectionNotification.CLOSED, this, savedConnectionId, clientNotifSeqNo++, "Client has been closed", null);
sendNotification(closedNotif);
}
//
if (closeException != null) {
if (tracing)
logger.trace("close", idstr + " failed to close: " + closeException);
if (closeException instanceof IOException)
throw (IOException) closeException;
if (closeException instanceof RuntimeException)
throw (RuntimeException) closeException;
final IOException x = new IOException("Failed to close: " + closeException);
throw EnvHelp.initCause(x, closeException);
}
}
use of javax.management.remote.JMXConnectionNotification in project jdk8u_jdk by JetBrains.
the class Client method run.
public static void run(String url) throws Exception {
final int notifEmittedCnt = 10;
final CountDownLatch counter = new CountDownLatch(notifEmittedCnt);
final Set<Long> seqSet = Collections.synchronizedSet(new HashSet<Long>());
final AtomicBoolean duplNotification = new AtomicBoolean();
JMXServiceURL serverUrl = new JMXServiceURL(url);
ObjectName name = new ObjectName("test", "foo", "bar");
JMXConnector jmxConnector = JMXConnectorFactory.connect(serverUrl);
System.out.println("client connected");
jmxConnector.addConnectionNotificationListener(new NotificationListener() {
@Override
public void handleNotification(Notification notification, Object handback) {
System.out.println("connection notification: " + notification);
if (!seqSet.add(notification.getSequenceNumber())) {
duplNotification.set(true);
}
if (notification.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
long lostNotifs = ((Long) ((JMXConnectionNotification) notification).getUserData()).longValue();
for (int i = 0; i < lostNotifs; i++) {
counter.countDown();
}
}
}
}, null, null);
MBeanServerConnection jmxServer = jmxConnector.getMBeanServerConnection();
jmxServer.addNotificationListener(name, new NotificationListener() {
@Override
public void handleNotification(Notification notification, Object handback) {
System.out.println("client got: " + notification);
if (!seqSet.add(notification.getSequenceNumber())) {
duplNotification.set(true);
}
counter.countDown();
}
}, null, null);
System.out.println("client invoking foo (" + notifEmittedCnt + " times)");
for (int i = 0; i < notifEmittedCnt; i++) {
System.out.print(".");
jmxServer.invoke(name, "foo", new Object[] {}, new String[] {});
}
System.out.println();
try {
System.out.println("waiting for " + notifEmittedCnt + " notifications to arrive");
if (!counter.await(30, TimeUnit.SECONDS)) {
throw new InterruptedException();
}
if (duplNotification.get()) {
System.out.println("ERROR: received duplicated notifications");
throw new Error("received duplicated notifications");
}
System.out.println("\nshutting down client");
} catch (InterruptedException e) {
System.out.println("ERROR: notification processing thread interrupted");
throw new Error("notification thread interrupted unexpectedly");
}
}
Aggregations