001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq;
018
019import javax.jms.JMSException;
020
021import org.apache.activemq.advisory.AdvisorySupport;
022import org.apache.activemq.command.ActiveMQDestination;
023import org.apache.activemq.command.ActiveMQTempDestination;
024import org.apache.activemq.command.ConsumerId;
025import org.apache.activemq.command.ConsumerInfo;
026import org.apache.activemq.command.DataStructure;
027import org.apache.activemq.command.DestinationInfo;
028import org.apache.activemq.command.MessageAck;
029import org.apache.activemq.command.MessageDispatch;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033public class AdvisoryConsumer implements ActiveMQDispatcher {
034    private static final transient Logger LOG = LoggerFactory.getLogger(AdvisoryConsumer.class);
035
036    int deliveredCounter;
037
038    private final ActiveMQConnection connection;
039    private ConsumerInfo info;
040    private boolean closed;
041
042    public AdvisoryConsumer(ActiveMQConnection connection, ConsumerId consumerId) throws JMSException {
043        this.connection = connection;
044        info = new ConsumerInfo(consumerId);
045        info.setDestination(AdvisorySupport.TEMP_DESTINATION_COMPOSITE_ADVISORY_TOPIC);
046        info.setPrefetchSize(1000);
047        info.setNoLocal(true);
048        info.setDispatchAsync(true);
049
050        this.connection.addDispatcher(info.getConsumerId(), this);
051        this.connection.syncSendPacket(this.info);
052    }
053
054    public synchronized void dispose() {
055        if (!closed) {
056            try {
057                this.connection.asyncSendPacket(info.createRemoveCommand());
058            } catch (JMSException e) {
059                LOG.debug("Failed to send remove command: " + e, e);
060            }
061            this.connection.removeDispatcher(info.getConsumerId());
062            closed = true;
063        }
064    }
065
066    public void dispatch(MessageDispatch md) {
067
068        // Auto ack messages when we reach 75% of the prefetch
069        deliveredCounter++;
070        if (deliveredCounter > (0.75 * info.getPrefetchSize())) {
071            try {
072                MessageAck ack = new MessageAck(md, MessageAck.STANDARD_ACK_TYPE, deliveredCounter);
073                connection.asyncSendPacket(ack);
074                deliveredCounter = 0;
075            } catch (JMSException e) {
076                connection.onClientInternalException(e);
077            }
078        }
079
080        DataStructure o = md.getMessage().getDataStructure();
081        if (o != null && o.getClass() == DestinationInfo.class) {
082            processDestinationInfo((DestinationInfo)o);
083        } else {
084            //This can happen across networks
085            if (LOG.isDebugEnabled()) {
086                LOG.debug("Unexpected message was dispatched to the AdvisoryConsumer: "+md);
087            }
088        }
089
090    }
091
092    private void processDestinationInfo(DestinationInfo dinfo) {
093        ActiveMQDestination dest = dinfo.getDestination();
094        if (!dest.isTemporary()) {
095            return;
096        }
097
098        ActiveMQTempDestination tempDest = (ActiveMQTempDestination)dest;
099        if (dinfo.getOperationType() == DestinationInfo.ADD_OPERATION_TYPE) {
100            if (tempDest.getConnection() != null) {
101                tempDest = (ActiveMQTempDestination) tempDest.createDestination(tempDest.getPhysicalName());
102            }
103            connection.activeTempDestinations.put(tempDest, tempDest);
104        } else if (dinfo.getOperationType() == DestinationInfo.REMOVE_OPERATION_TYPE) {
105            connection.activeTempDestinations.remove(tempDest);
106        }
107    }
108
109}