use of javax.management.InstanceNotFoundException in project opennms by OpenNMS.
the class PollerRpcTimeoutIT method testPolling.
@Test
public void testPolling() throws Exception {
String queueName = new JmsQueueNameFactory("RPC", "Poller", NONEXISTENT_LOCATION).getName();
try {
ManagementFactory.getPlatformMBeanServer().getObjectInstance(ObjectName.getInstance("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=ActiveMQ.DLQ"));
fail("DLQ was unexpected found in the PlatformMBeanServer");
} catch (InstanceNotFoundException e) {
// Expected: the queue hasn't been created yet
}
try {
ManagementFactory.getPlatformMBeanServer().getObjectInstance(ObjectName.getInstance("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=" + queueName));
fail(queueName + " queue was unexpected found in the PlatformMBeanServer");
} catch (InstanceNotFoundException e) {
// Expected: the queue hasn't been created yet
}
// Start the poller: polls will timeout because there is no consumer for the location
startDaemons();
// Sleep long enough so that messages would be flushed to the DLQ
Thread.sleep(60000);
// Stop the poller daemon
stopDaemons();
try {
ManagementFactory.getPlatformMBeanServer().getObjectInstance(ObjectName.getInstance("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=ActiveMQ.DLQ"));
fail("DLQ was unexpected found in the PlatformMBeanServer");
} catch (InstanceNotFoundException e) {
// Expected: the DLQ still should be absent
}
Long dequeueCount = (Long) ManagementFactory.getPlatformMBeanServer().getAttribute(ObjectName.getInstance("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=" + queueName), "DequeueCount");
Long expiredCount = (Long) ManagementFactory.getPlatformMBeanServer().getAttribute(ObjectName.getInstance("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=" + queueName), "ExpiredCount");
Long dispatchCount = (Long) ManagementFactory.getPlatformMBeanServer().getAttribute(ObjectName.getInstance("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=" + queueName), "DispatchCount");
assertTrue("No expired messages were present", expiredCount > 0L);
assertEquals("Dequeued messages do not equal expired messages", expiredCount, dequeueCount);
assertEquals("Dispatched message count was not zero", 0, dispatchCount.intValue());
}
use of javax.management.InstanceNotFoundException in project jdk8u_jdk by JetBrains.
the class ArrayNotificationBuffer method fetchNotifications.
/**
* <p>Fetch notifications that match the given listeners.</p>
*
* <p>The operation only considers notifications with a sequence
* number at least <code>startSequenceNumber</code>. It will take
* no longer than <code>timeout</code>, and will return no more
* than <code>maxNotifications</code> different notifications.</p>
*
* <p>If there are no notifications matching the criteria, the
* operation will block until one arrives, subject to the
* timeout.</p>
*
* @param filter an object that will add notifications to a
* {@code List<TargetedNotification>} if they match the current
* listeners with their filters.
* @param startSequenceNumber the first sequence number to
* consider.
* @param timeout the maximum time to wait. May be 0 to indicate
* not to wait if there are no notifications.
* @param maxNotifications the maximum number of notifications to
* return. May be 0 to indicate a wait for eligible notifications
* that will return a usable <code>nextSequenceNumber</code>. The
* {@link TargetedNotification} array in the returned {@link
* NotificationResult} may contain more than this number of
* elements but will not contain more than this number of
* different notifications.
*/
public NotificationResult fetchNotifications(NotificationBufferFilter filter, long startSequenceNumber, long timeout, int maxNotifications) throws InterruptedException {
logger.trace("fetchNotifications", "starts");
if (startSequenceNumber < 0 || isDisposed()) {
synchronized (this) {
return new NotificationResult(earliestSequenceNumber(), nextSequenceNumber(), new TargetedNotification[0]);
}
}
// Check arg validity
if (filter == null || startSequenceNumber < 0 || timeout < 0 || maxNotifications < 0) {
logger.trace("fetchNotifications", "Bad args");
throw new IllegalArgumentException("Bad args to fetch");
}
if (logger.debugOn()) {
logger.trace("fetchNotifications", "filter=" + filter + "; startSeq=" + startSequenceNumber + "; timeout=" + timeout + "; max=" + maxNotifications);
}
if (startSequenceNumber > nextSequenceNumber()) {
final String msg = "Start sequence number too big: " + startSequenceNumber + " > " + nextSequenceNumber();
logger.trace("fetchNotifications", msg);
throw new IllegalArgumentException(msg);
}
/* Determine the end time corresponding to the timeout value.
Caller may legitimately supply Long.MAX_VALUE to indicate no
timeout. In that case the addition will overflow and produce
a negative end time. Set end time to Long.MAX_VALUE in that
case. We assume System.currentTimeMillis() is positive. */
long endTime = System.currentTimeMillis() + timeout;
if (// overflow
endTime < 0)
endTime = Long.MAX_VALUE;
if (logger.debugOn())
logger.debug("fetchNotifications", "endTime=" + endTime);
/* We set earliestSeq the first time through the loop. If we
set it here, notifications could be dropped before we
started examining them, so earliestSeq might not correspond
to the earliest notification we examined. */
long earliestSeq = -1;
long nextSeq = startSequenceNumber;
List<TargetedNotification> notifs = new ArrayList<TargetedNotification>();
/* On exit from this loop, notifs, earliestSeq, and nextSeq must
all be correct values for the returned NotificationResult. */
while (true) {
logger.debug("fetchNotifications", "main loop starts");
NamedNotification candidate;
/* Get the next available notification regardless of filters,
or wait for one to arrive if there is none. */
synchronized (this) {
/* First time through. The current earliestSequenceNumber
is the first one we could have examined. */
if (earliestSeq < 0) {
earliestSeq = earliestSequenceNumber();
if (logger.debugOn()) {
logger.debug("fetchNotifications", "earliestSeq=" + earliestSeq);
}
if (nextSeq < earliestSeq) {
nextSeq = earliestSeq;
logger.debug("fetchNotifications", "nextSeq=earliestSeq");
}
} else
earliestSeq = earliestSequenceNumber();
/* If many notifications have been dropped since the
last time through, nextSeq could now be earlier
than the current earliest. If so, notifications
may have been lost and we return now so the caller
can see this next time it calls. */
if (nextSeq < earliestSeq) {
logger.trace("fetchNotifications", "nextSeq=" + nextSeq + " < " + "earliestSeq=" + earliestSeq + " so may have lost notifs");
break;
}
if (nextSeq < nextSequenceNumber()) {
candidate = notificationAt(nextSeq);
// Skip security check if NotificationBufferFilter is not overloaded
if (!(filter instanceof ServerNotifForwarder.NotifForwarderBufferFilter)) {
try {
ServerNotifForwarder.checkMBeanPermission(this.mBeanServer, candidate.getObjectName(), "addNotificationListener");
} catch (InstanceNotFoundException | SecurityException e) {
if (logger.debugOn()) {
logger.debug("fetchNotifications", "candidate: " + candidate + " skipped. exception " + e);
}
++nextSeq;
continue;
}
}
if (logger.debugOn()) {
logger.debug("fetchNotifications", "candidate: " + candidate);
logger.debug("fetchNotifications", "nextSeq now " + nextSeq);
}
} else {
/* nextSeq is the largest sequence number. If we
already got notifications, return them now.
Otherwise wait for some to arrive, with
timeout. */
if (notifs.size() > 0) {
logger.debug("fetchNotifications", "no more notifs but have some so don't wait");
break;
}
long toWait = endTime - System.currentTimeMillis();
if (toWait <= 0) {
logger.debug("fetchNotifications", "timeout");
break;
}
/* dispose called */
if (isDisposed()) {
if (logger.debugOn())
logger.debug("fetchNotifications", "dispose callled, no wait");
return new NotificationResult(earliestSequenceNumber(), nextSequenceNumber(), new TargetedNotification[0]);
}
if (logger.debugOn())
logger.debug("fetchNotifications", "wait(" + toWait + ")");
wait(toWait);
continue;
}
}
/* We have a candidate notification. See if it matches
our filters. We do this outside the synchronized block
so we don't hold up everyone accessing the buffer
(including notification senders) while we evaluate
potentially slow filters. */
ObjectName name = candidate.getObjectName();
Notification notif = candidate.getNotification();
List<TargetedNotification> matchedNotifs = new ArrayList<TargetedNotification>();
logger.debug("fetchNotifications", "applying filter to candidate");
filter.apply(matchedNotifs, name, notif);
if (matchedNotifs.size() > 0) {
/* We only check the max size now, so that our
returned nextSeq is as large as possible. This
prevents the caller from thinking it missed
interesting notifications when in fact we knew they
weren't. */
if (maxNotifications <= 0) {
logger.debug("fetchNotifications", "reached maxNotifications");
break;
}
--maxNotifications;
if (logger.debugOn())
logger.debug("fetchNotifications", "add: " + matchedNotifs);
notifs.addAll(matchedNotifs);
}
++nextSeq;
}
// end while
/* Construct and return the result. */
int nnotifs = notifs.size();
TargetedNotification[] resultNotifs = new TargetedNotification[nnotifs];
notifs.toArray(resultNotifs);
NotificationResult nr = new NotificationResult(earliestSeq, nextSeq, resultNotifs);
if (logger.debugOn())
logger.debug("fetchNotifications", nr.toString());
logger.trace("fetchNotifications", "ends");
return nr;
}
use of javax.management.InstanceNotFoundException in project jdk8u_jdk by JetBrains.
the class ServerNotifForwarder method addNotificationListener.
public Integer addNotificationListener(final ObjectName name, final NotificationFilter filter) throws InstanceNotFoundException, IOException {
if (logger.traceOn()) {
logger.trace("addNotificationListener", "Add a listener at " + name);
}
checkState();
// Explicitly check MBeanPermission for addNotificationListener
//
checkMBeanPermission(name, "addNotificationListener");
if (notificationAccessController != null) {
notificationAccessController.addNotificationListener(connectionId, name, getSubject());
}
try {
boolean instanceOf = AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
public Boolean run() throws InstanceNotFoundException {
return mbeanServer.isInstanceOf(name, broadcasterClass);
}
});
if (!instanceOf) {
throw new IllegalArgumentException("The specified MBean [" + name + "] is not a " + "NotificationBroadcaster " + "object.");
}
} catch (PrivilegedActionException e) {
throw (InstanceNotFoundException) extractException(e);
}
final Integer id = getListenerID();
// 6238731: set the default domain if no domain is set.
ObjectName nn = name;
if (name.getDomain() == null || name.getDomain().equals("")) {
try {
nn = ObjectName.getInstance(mbeanServer.getDefaultDomain(), name.getKeyPropertyList());
} catch (MalformedObjectNameException mfoe) {
// impossible, but...
IOException ioe = new IOException(mfoe.getMessage());
ioe.initCause(mfoe);
throw ioe;
}
}
synchronized (listenerMap) {
IdAndFilter idaf = new IdAndFilter(id, filter);
Set<IdAndFilter> set = listenerMap.get(nn);
// Collections.singleton we make here, which is unmodifiable.
if (set == null)
set = Collections.singleton(idaf);
else {
if (set.size() == 1)
set = new HashSet<IdAndFilter>(set);
set.add(idaf);
}
listenerMap.put(nn, set);
}
return id;
}
use of javax.management.InstanceNotFoundException in project jdk8u_jdk by JetBrains.
the class ServerNotifForwarder method removeNotificationListener.
public void removeNotificationListener(ObjectName name, Integer listenerID) throws InstanceNotFoundException, ListenerNotFoundException, IOException {
if (logger.traceOn()) {
logger.trace("removeNotificationListener", "Remove the listener " + listenerID + " from " + name);
}
checkState();
if (name != null && !name.isPattern()) {
if (!mbeanServer.isRegistered(name)) {
throw new InstanceNotFoundException("The MBean " + name + " is not registered.");
}
}
synchronized (listenerMap) {
// Tread carefully because if set.size() == 1 it may be a
// Collections.singleton, which is unmodifiable.
Set<IdAndFilter> set = listenerMap.get(name);
IdAndFilter idaf = new IdAndFilter(listenerID, null);
if (set == null || !set.contains(idaf))
throw new ListenerNotFoundException("Listener not found");
if (set.size() == 1)
listenerMap.remove(name);
else
set.remove(idaf);
}
}
use of javax.management.InstanceNotFoundException in project jdk8u_jdk by JetBrains.
the class SnmpGenericObjectServer method get.
/**
* Execute an SNMP GET request.
*
* <p>
* This method first builds the list of attributes that need to be
* retrieved from the MBean and then calls getAttributes() on the
* MBean server. Then it updates the SnmpMibSubRequest with the values
* retrieved from the MBean.
* </p>
*
* <p>
* The SNMP metadata information is obtained through the given
* <code>meta</code> object, which usually is an instance of a
* <code>mibgen</code> generated class.
* </p>
*
* <p><b><i>
* This method is called internally by <code>mibgen</code> generated
* objects and you should never need to call it directly.
* </i></b></p>
*
* @param meta The metadata object impacted by the subrequest
* @param name The ObjectName of the MBean impacted by this subrequest
* @param req The SNMP subrequest to execute on the MBean
* @param depth The depth of the SNMP object in the OID tree.
*
* @exception SnmpStatusException whenever an SNMP exception must be
* raised. Raising an exception will abort the request.<br>
* Exceptions should never be raised directly, but only by means of
* <code>
* req.registerGetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
* </code>
**/
public void get(SnmpGenericMetaServer meta, ObjectName name, SnmpMibSubRequest req, int depth) throws SnmpStatusException {
// java.lang.System.out.println(">>>>>>>>> GET " + name);
final int size = req.getSize();
final Object data = req.getUserData();
final String[] nameList = new String[size];
final SnmpVarBind[] varList = new SnmpVarBind[size];
final long[] idList = new long[size];
int i = 0;
for (Enumeration<SnmpVarBind> e = req.getElements(); e.hasMoreElements(); ) {
final SnmpVarBind var = e.nextElement();
try {
final long id = var.oid.getOidArc(depth);
nameList[i] = meta.getAttributeName(id);
varList[i] = var;
idList[i] = id;
// Check the access rights according to the MIB.
// The MBean might be less restrictive (have a getter
// while the MIB defines the variable as AFN)
//
meta.checkGetAccess(id, data);
//java.lang.System.out.println(nameList[i] + " added.");
i++;
} catch (SnmpStatusException x) {
//java.lang.System.out.println("exception for " + nameList[i]);
//x.printStackTrace();
req.registerGetException(var, x);
}
}
AttributeList result = null;
int errorCode = SnmpStatusException.noSuchInstance;
try {
result = server.getAttributes(name, nameList);
} catch (InstanceNotFoundException f) {
//java.lang.System.out.println(name + ": instance not found.");
//f.printStackTrace();
result = new AttributeList();
} catch (ReflectionException r) {
//java.lang.System.out.println(name + ": reflexion error.");
//r.printStackTrace();
result = new AttributeList();
} catch (Exception x) {
result = new AttributeList();
}
final Iterator<?> it = result.iterator();
for (int j = 0; j < i; j++) {
if (!it.hasNext()) {
//java.lang.System.out.println(name + "variable[" + j +
// "] absent");
final SnmpStatusException x = new SnmpStatusException(errorCode);
req.registerGetException(varList[j], x);
continue;
}
final Attribute att = (Attribute) it.next();
while ((j < i) && (!nameList[j].equals(att.getName()))) {
//java.lang.System.out.println(name + "variable[" +j +
// "] not found");
final SnmpStatusException x = new SnmpStatusException(errorCode);
req.registerGetException(varList[j], x);
j++;
}
if (j == i)
break;
try {
varList[j].value = meta.buildSnmpValue(idList[j], att.getValue());
} catch (SnmpStatusException x) {
req.registerGetException(varList[j], x);
}
//java.lang.System.out.println(att.getName() + " retrieved.");
}
//java.lang.System.out.println(">>>>>>>>> END GET");
}
Aggregations