Package javax.management.remote

Examples of javax.management.remote.NotificationResult


         while (isActive() && !thread.isInterrupted())
         {
            try
            {
               long sequence = getSequenceNumber();
               NotificationResult result = handler.fetchNotifications(sequence, maxNumber, timeout);
               RmiConnectorActivator.log(LogService.LOG_WARNING,"Fetched Notifications: " + result, null);

               long sleepTime = sleep;
               if (result != null)
               {
                  long nextSequence = result.getNextSequenceNumber();
                  TargetedNotification[] targeted = result.getTargetedNotifications();
                  int targetedLength = targeted == null ? 0 : targeted.length;
                  boolean notifsFilteredByServer = nextSequence - sequence != targetedLength;
                  boolean notifsLostByServer = sequence >= 0 && result.getEarliestSequenceNumber() > sequence;
                  if (notifsFilteredByServer)
                  {
                     // We lost some notification
                     handler.sendNotificationsLost(nextSequence - sequence - targetedLength);
                  }
                  if (notifsLostByServer)
                  {
                     // We lost some notification
                     handler.sendNotificationsLost(result.getEarliestSequenceNumber() - sequence);
                  }

                  setSequenceNumber(nextSequence);
                  deliverNotifications(targeted);
View Full Code Here


      public NotificationResult getNotifications(long sequenceNumber, int maxNotifications, long timeout)
      {
         synchronized (buffer)
         {
            NotificationResult result = null;
            int size = 0;
            if (sequenceNumber < 0)
            {
               // We loose the notifications between addNotificationListener() and fetchNotifications(), but c'est la vie.
               long sequence = getLastSequenceNumber();
               size = new Long(sequence + 1).intValue();
               result = new NotificationResult(getFirstSequenceNumber(), sequence, new TargetedNotification[0]);
                RmiConnectorActivator.log(LogService.LOG_DEBUG,"First fetchNotification call: " + this + ", returning " + result, null);
            }
            else
            {
               int start = new Long(sequenceNumber - getFirstSequenceNumber()).intValue();

               List sublist = null;
               boolean send = false;
               while (size == 0)
               {
                  int end = buffer.size();
                  if (end - start > maxNotifications) end = start + maxNotifications;

                  sublist = buffer.subList(start, end);
                  size = sublist.size();

                  if (send) break;

                  if (size == 0)
                  {
                     if (timeout <= 0) break;
                      RmiConnectorActivator.log(LogService.LOG_DEBUG,"No notifications to send, waiting " + timeout + " ms", null);

                     // We wait for notifications to arrive. Since we release the lock on the buffer
                     // other threads can modify it. To avoid ConcurrentModificationException we compute
                     // again the sublist
                     send = waitForNotifications(buffer, timeout);
                  }
               }

               TargetedNotification[] notifications = (TargetedNotification[])sublist.toArray(new TargetedNotification[size]);
               notifications = filterNotifications(notifications);
               result = new NotificationResult(getFirstSequenceNumber(), sequenceNumber + size, notifications);
                RmiConnectorActivator.log(LogService.LOG_DEBUG,"Non-first fetchNotification call: " + this + ", returning " + result, null);

               purgeNotifications(sequenceNumber, size);
               RmiConnectorActivator.log(LogService.LOG_DEBUG,"Purged Notifications: " + this, null);
            }
View Full Code Here

public class NotificationResultTest
  implements Testlet
{
  public void test(TestHarness h)
  {
    NotificationResult nr;
    TargetedNotification[] array = new TargetedNotification[]{};
    h.checkPoint("Constructor tests");
    try
      {
  nr = new NotificationResult(-1, 0, array);
  h.fail("Failed to catch negative earliest sequence number");
      }
    catch (Exception e)
      {
  if (e instanceof IllegalArgumentException)
    h.check(true, "Caught negative earliest sequence number.");
  else
    {
      h.debug(e);
      h.fail("Unknown exception");
    }
      }
    try
      {
  nr = new NotificationResult(0, -1, array);
  h.fail("Failed to catch negative next sequence number");
      }
    catch (Exception e)
      {
  if (e instanceof IllegalArgumentException)
    h.check(true, "Caught negative next sequence number.");
  else
    {
      h.debug(e);
      h.fail("Unknown exception");
    }
      }
    try
      {
  nr = new NotificationResult(0, 1, null);
  h.fail("Failed to catch null result array");
      }
    catch (Exception e)
      {
  if (e instanceof IllegalArgumentException)
    h.check(true, "Caught null result array.");
  else
    {
      h.debug(e);
      h.fail("Unknown exception");
    }
      }
    try
      {
  nr = new NotificationResult(0, 1, array);
  h.check(true, "NotificationResult successfully created.");
  h.check(nr.getEarliestSequenceNumber() == 0,
    "Retrieved earliest sequence number.");
  h.check(nr.getNextSequenceNumber() == 1,
    "Retrieved next sequence number.");
   h.check(nr.getTargetedNotifications() == array,
    "Retrieved array.");
      }
    catch (Exception e)
      {
  if (e instanceof IllegalArgumentException)
View Full Code Here

      else if (NotificationResultSer.NOTIFICATIONS.equals(hint)) targetedNotifications = (TargetedNotification[])value;
   }

   protected Object createObject() throws SAXException
   {
      return new NotificationResult(earliestSequenceNumber, nextSequenceNumber, targetedNotifications);
   }
View Full Code Here

      call.addParameter("sequence", XMLType.XSD_LONG, ParameterMode.IN);
      call.addParameter("maxNumber", XMLType.XSD_INT, ParameterMode.IN);
      call.addParameter("timeout", XMLType.XSD_LONG, ParameterMode.IN);
      call.setReturnType(new QName(SOAPConstants.NAMESPACE_URI, "NotificationResult"));

      NotificationResult result = (NotificationResult)call.invoke(new Object[]{new Long(clientSequenceNumber), new Integer(maxNotifications), new Long(timeout)});
      return result;
   }
View Full Code Here

   private static final QName NEXT_NUMBER_QNAME = new QName("", NEXT_NUMBER);
   private static final QName NOTIFICATIONS_QNAME = new QName("", NOTIFICATIONS);

   public void serialize(QName name, Attributes attributes, Object value, SerializationContext context) throws IOException
   {
      NotificationResult notificationResult = (NotificationResult)value;
      context.startElement(name, attributes);
      context.serialize(EARLIEST_NUMBER_QNAME, null, new Long(notificationResult.getEarliestSequenceNumber()));
      context.serialize(NEXT_NUMBER_QNAME, null, new Long(notificationResult.getNextSequenceNumber()));
      context.serialize(NOTIFICATIONS_QNAME, null, notificationResult.getTargetedNotifications());
      context.endElement();
   }
View Full Code Here

            while (isActive() && !thread.isInterrupted())
            {
               try
               {
                  long sequence = getSequenceNumber();
                  NotificationResult result = fetchNotifications(sequence, maxNumber, timeout);
                  if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Fetched Notifications: " + result);

                  long sleepTime = sleep;
                  if (result != null)
                  {
                     long nextSequence = result.getNextSequenceNumber();
                     TargetedNotification[] targeted = result.getTargetedNotifications();
                     int targetedLength = targeted == null ? 0 : targeted.length;
                     boolean notifsFilteredByServer = sequence >= 0 ? nextSequence - sequence != targetedLength : false;
                     boolean notifsLostByServer = sequence >= 0 && result.getEarliestSequenceNumber() > sequence;
                     if (notifsFilteredByServer)
                     {
                        // We lost some notification
                        sendConnectionNotificationLost(nextSequence - sequence - targetedLength);
                     }
                     if (notifsLostByServer)
                     {
                        // We lost some notification
                        sendConnectionNotificationLost(result.getEarliestSequenceNumber() - sequence);
                     }

                     setSequenceNumber(nextSequence);
                     int delivered = deliverNotifications(targeted);
                     if (delivered < targetedLength)
View Full Code Here

      private NotificationResult getNotifications(long sequenceNumber, int maxNotifications, long timeout)
      {
         Logger logger = getLogger();
         synchronized (this)
         {
            NotificationResult result = null;
            int size = 0;
            if (sequenceNumber < 0)
            {
               // We loose the notifications between addNotificationListener() and fetchNotifications(), but c'est la vie.
               long sequence = getLastSequenceNumber();
               size = new Long(sequence + 1).intValue();
               result = new NotificationResult(getFirstSequenceNumber(), sequence, new TargetedNotification[0]);
               if (lowestExpectedSequence < 0) lowestExpectedSequence = sequence;
               if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("First fetchNotification call: " + this + ", returning " + result);
            }
            else
            {
               long firstSequence = getFirstSequenceNumber();

               int losts = 0;
               int start = new Long(sequenceNumber - firstSequence).intValue();
               // In the time between 2 fetches the buffer may have overflew, so that start < 0.
               // It simply mean that we send the first notification we have (start = 0),
               // and the client will emit a notification lost event.
               if (start < 0)
               {
                  losts = -start;
                  start = 0;
               }

               List sublist = null;
               boolean send = false;
               while (size == 0)
               {
                  int end = notifications.size();
                  if (end - start > maxNotifications) end = start + maxNotifications;

                  sublist = notifications.subList(start, end);
                  size = sublist.size();

                  if (closed || send) break;

                  if (size == 0)
                  {
                     if (timeout <= 0) break;
                     if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("No notifications to send, waiting " + timeout + " ms");

                     // We wait for notifications to arrive. Since we release the lock on the buffer
                     // other threads can modify it. To avoid ConcurrentModificationException we compute
                     // again the sublist by coming up back to the while statement
                     send = waitForNotifications(this, timeout);
                  }
               }

               TargetedNotification[] notifications = (TargetedNotification[])sublist.toArray(new TargetedNotification[size]);
               notifications = filterNotifications(notifications);
               result = new NotificationResult(firstSequence, sequenceNumber + losts + size, notifications);
               if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Non-first fetchNotification call: " + this + ", returning " + result);

               int purged = purgeNotifications(sequenceNumber, size);
               if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Purged " + purged + " notifications: " + this);
            }
View Full Code Here

        try {
            if (serverTerminated) {
                // we must not call fetchNotifs() if the server is
                // terminated (timeout elapsed).
                //
                return new NotificationResult(0L, 0L,
                                              new TargetedNotification[0]);

            }
            final long csn = clientSequenceNumber;
            final int mn = maxNotifications;
View Full Code Here

        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 (endTime < 0) // overflow
            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;
    }
View Full Code Here

TOP

Related Classes of javax.management.remote.NotificationResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.