<commit_before>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.jca.adapters.mail.inflow;

import java.util.concurrent.PriorityBlockingQueue;

import javax.resource.spi.work.Work;
import javax.resource.spi.work.WorkEvent;
import javax.resource.spi.work.WorkException;
import javax.resource.spi.work.WorkListener;
import javax.resource.spi.work.WorkManager;

import org.jboss.logging.Logger;

/**
 * Handles new messages
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class NewMsgsWorker implements Work, WorkListener
{
   private static Logger log = Logger.getLogger(NewMsgsWorker.class);

   private boolean released;

   private WorkManager mgr;

   private PriorityBlockingQueue<MailActivation> pollQueue;

   /**
    * Constructor
    * @param mgr The work manager
    * @param queueSize The queue size
    */
   public NewMsgsWorker(WorkManager mgr, Integer queueSize)
   {
      this.mgr = mgr;
      this.pollQueue = new PriorityBlockingQueue<MailActivation>(queueSize.intValue());
   }

   /**
    * Watch an activation
    * @param activation The activation
    * @exception InterruptedException Thrown if the queue is interrupted
    */
   public void watch(MailActivation activation) throws InterruptedException
   {
      activation.updateNextNewMsgCheckTime(System.currentTimeMillis());

      pollQueue.put(activation);
   }

   /**
    * Release
    */
   public void release()
   {
      released = true;

      log.tracef("released");
   }

   /**
    * Run
    */
   public void run()
   {
      log.tracef("Begin run");

      while (!released)
      {
         try
         {
            MailActivation ma = (MailActivation) pollQueue.take();

            // Wait until its time to check for new msgs
            long now = System.currentTimeMillis();
            long nextTime = ma.getNextNewMsgCheckTime();
            long sleepMS = nextTime - now;

            if (sleepMS > 0)
               Thread.sleep(sleepMS);

            if (released)
               break;

            // This has to go after the sleep otherwise we can get into an inconsistent state
            if (ma.isReleased())<commit_msg>Fix warning<commit_after>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.jca.adapters.mail.inflow;

import java.util.concurrent.PriorityBlockingQueue;

import javax.resource.spi.work.Work;
import javax.resource.spi.work.WorkEvent;
import javax.resource.spi.work.WorkException;
import javax.resource.spi.work.WorkListener;
import javax.resource.spi.work.WorkManager;

import org.jboss.logging.Logger;

/**
 * Handles new messages
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class NewMsgsWorker implements Work, WorkListener
{
   private static Logger log = Logger.getLogger(NewMsgsWorker.class);

   private boolean released;

   private WorkManager mgr;

   private PriorityBlockingQueue<MailActivation> pollQueue;

   /**
    * Constructor
    * @param mgr The work manager
    * @param queueSize The queue size
    */
   public NewMsgsWorker(WorkManager mgr, Integer queueSize)
   {
      this.mgr = mgr;
      this.pollQueue = new PriorityBlockingQueue<MailActivation>(queueSize.intValue());
   }

   /**
    * Watch an activation
    * @param activation The activation
    * @exception InterruptedException Thrown if the queue is interrupted
    */
   public void watch(MailActivation activation) throws InterruptedException
   {
      activation.updateNextNewMsgCheckTime(System.currentTimeMillis());

      pollQueue.put(activation);
   }

   /**
    * Release
    */
   public void release()
   {
      released = true;

      log.tracef("released");
   }

   /**
    * Run
    */
   public void run()
   {
      log.tracef("Begin run");

      while (!released)
      {
         try
         {
            MailActivation ma = pollQueue.take();

            // Wait until its time to check for new msgs
            long now = System.currentTimeMillis();
            long nextTime = ma.getNextNewMsgCheckTime();
            long sleepMS = nextTime - now;

            if (sleepMS > 0)
               Thread.sleep(sleepMS);

            if (released)
               break;

            // This has to go after the sleep otherwise we can get into an inconsistent state
            if (ma.isReleased())
{'fraction_non_alphanumeric': 0.08832311802608832, 'fraction_numerical': 0.005657708628005658, 'mean_word_length': 2.9163076923076923, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3112182', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1700, 'n_words': 617}