BasicRejectMethodHandler.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.server.handler;
022 
023 import org.apache.qpid.AMQException;
024 import org.apache.qpid.framing.BasicRejectBody;
025 import org.apache.qpid.server.AMQChannel;
026 import org.apache.qpid.server.queue.QueueEntry;
027 import org.apache.qpid.server.protocol.AMQProtocolSession;
028 import org.apache.qpid.server.state.AMQStateManager;
029 import org.apache.qpid.server.state.StateAwareMethodListener;
030 import org.apache.log4j.Logger;
031 
032 public class BasicRejectMethodHandler implements StateAwareMethodListener<BasicRejectBody>
033 {
034     private static final Logger _logger = Logger.getLogger(BasicRejectMethodHandler.class);
035 
036     private static BasicRejectMethodHandler _instance = new BasicRejectMethodHandler();
037 
038     public static BasicRejectMethodHandler getInstance()
039     {
040         return _instance;
041     }
042 
043     private BasicRejectMethodHandler()
044     {
045     }
046 
047     public void methodReceived(AMQStateManager stateManager, BasicRejectBody body, int channelIdthrows AMQException
048     {
049         AMQProtocolSession session = stateManager.getProtocolSession();
050 
051         AMQChannel channel = session.getChannel(channelId);
052 
053         if (channel == null)
054         {
055             throw body.getChannelNotFoundException(channelId);
056         }
057 
058         if (_logger.isDebugEnabled())
059         {
060             _logger.debug("Rejecting:" + body.getDeliveryTag() +
061                           ": Requeue:" + body.getRequeue() +
062                           //": Resend:" + evt.getMethod().resend +
063                           " on channel:" + channel.debugIdentity());
064         }
065 
066         long deliveryTag = body.getDeliveryTag();
067 
068         QueueEntry queueEntry = channel.getUnacknowledgedMessageMap().get(deliveryTag);
069 
070         if (queueEntry == null)
071         {
072             _logger.warn("Dropping reject request as message is null for tag:" + deliveryTag);
073 //            throw evt.getMethod().getChannelException(AMQConstant.NOT_FOUND, "Delivery Tag(" + deliveryTag + ")not known");
074         }
075         else
076         {
077             if (queueEntry.isQueueDeleted())
078             {
079                 _logger.warn("Message's Queue as already been purged, unable to Reject. " +
080                              "Dropping message should use Dead Letter Queue");
081                 queueEntry = channel.getUnacknowledgedMessageMap().remove(deliveryTag);
082                 if(queueEntry != null)
083                 {
084                     queueEntry.dequeueAndDelete(channel.getStoreContext());
085                 }
086                 //sendtoDeadLetterQueue(msg)
087                 return;
088             }
089 
090             if (queueEntry.isDeleted())
091             {
092                 _logger.warn("QueueEntry as already been deleted, unable to Reject.");
093                 return;
094             }
095 
096 
097             if (_logger.isDebugEnabled())
098             {
099                 _logger.debug("Rejecting: DT:" + deliveryTag + "-" + queueEntry.getMessage().debugIdentity() +
100                               ": Requeue:" + body.getRequeue() +
101                               //": Resend:" + evt.getMethod().resend +
102                               " on channel:" + channel.debugIdentity());
103             }
104 
105             // If we haven't requested message to be resent to this consumer then reject it from ever getting it.
106             //if (!evt.getMethod().resend)
107             {
108                 queueEntry.reject();
109             }
110 
111             if (body.getRequeue())
112             {
113                 channel.requeue(deliveryTag);
114             }
115             else
116             {
117                 _logger.warn("Dropping message as requeue not required and there is no dead letter queue");
118                  queueEntry = channel.getUnacknowledgedMessageMap().remove(deliveryTag);
119                 //sendtoDeadLetterQueue(AMQMessage message)
120 //                message.queue = channel.getDefaultDeadLetterQueue();
121 //                channel.requeue(deliveryTag);
122             }
123         }
124     }
125 }