public List<Message> removeWithSelector(String selector) throws MailQueueException {
  Connection connection=null;
  Session session=null;
  Message message=null;
  MessageConsumer consumer=null;
  boolean first=true;
  List<Message> messages=new ArrayList<Message>();
  try {
    connection=connectionFactory.createConnection();
    connection.start();
    session=connection.createSession(true,Session.SESSION_TRANSACTED);
    Queue queue=session.createQueue(queueName);
    consumer=session.createConsumer(queue,selector);
    while (first || message != null) {
      if (first) {
        message=consumer.receive(2000);
      }
 else {
        message=consumer.receiveNoWait();
      }
      first=false;
      if (message != null) {
        messages.add(message);
      }
    }
    session.commit();
    return messages;
  }
 catch (  Exception e) {
    try {
      session.rollback();
    }
 catch (    JMSException e1) {
    }
    throw new MailQueueException("Unable to remove mails",e);
  }
 finally {
    if (consumer != null) {
      try {
        consumer.close();
      }
 catch (      JMSException e1) {
      }
    }
    try {
      if (session != null)       session.close();
    }
 catch (    JMSException e1) {
    }
    try {
      if (connection != null)       connection.close();
    }
 catch (    JMSException e1) {
    }
  }
}

public List < Message > removeWithSelector ( String selector ) throws MailQueueException { Connection connection = null ; Session session = null ; Message message = null ; MessageConsumer consumer = null ; boolean first = true ; List < Message > messages = new ArrayList < Message > ( ) ; try { connection = connectionFactory . createConnection ( ) ; connection . start ( ) ; session = connection . createSession ( true , Session . SESSION_TRANSACTED ) ; Queue queue = session . createQueue ( queueName ) ; consumer = session . createConsumer ( queue , selector ) ; while ( first || message != null ) { if ( first ) { message = consumer . receive ( 2000 ) ; } else { message = consumer . receiveNoWait ( ) ; } first = false ; if ( message != null ) { messages . add ( message ) ; } } session . commit ( ) ; return messages ; } catch ( Exception e ) { try { session . rollback ( ) ; } catch ( JMSException e1 ) { } throw new MailQueueException ( "Unable to remove mails" , e ) ; } finally { if ( consumer != null ) { try { consumer . close ( ) ; } catch ( JMSException e1 ) { } } try { if ( session != null ) session . close ( ) ; } catch ( JMSException e1 ) { } try { if ( connection != null ) connection . close ( ) ; } catch ( JMSException e1 ) { } } }
remove messages with the given selector
