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 049 this.connection.addDispatcher(info.getConsumerId(), this); 050 this.connection.syncSendPacket(this.info); 051 } 052 053 public synchronized void dispose() { 054 if (!closed) { 055 try { 056 this.connection.asyncSendPacket(info.createRemoveCommand()); 057 } catch (JMSException e) { 058 LOG.debug("Failed to send remove command: " + e, e); 059 } 060 this.connection.removeDispatcher(info.getConsumerId()); 061 closed = true; 062 } 063 } 064 065 public void dispatch(MessageDispatch md) { 066 067 // Auto ack messages when we reach 75% of the prefetch 068 deliveredCounter++; 069 if (deliveredCounter > (0.75 * info.getPrefetchSize())) { 070 try { 071 MessageAck ack = new MessageAck(md, MessageAck.STANDARD_ACK_TYPE, deliveredCounter); 072 connection.asyncSendPacket(ack); 073 deliveredCounter = 0; 074 } catch (JMSException e) { 075 connection.onClientInternalException(e); 076 } 077 } 078 079 DataStructure o = md.getMessage().getDataStructure(); 080 if (o != null && o.getClass() == DestinationInfo.class) { 081 processDestinationInfo((DestinationInfo)o); 082 } else { 083 //This can happen across networks 084 if (LOG.isDebugEnabled()) { 085 LOG.debug("Unexpected message was dispatched to the AdvisoryConsumer: "+md); 086 } 087 } 088 089 } 090 091 private void processDestinationInfo(DestinationInfo dinfo) { 092 ActiveMQDestination dest = dinfo.getDestination(); 093 if (!dest.isTemporary()) { 094 return; 095 } 096 097 ActiveMQTempDestination tempDest = (ActiveMQTempDestination)dest; 098 if (dinfo.getOperationType() == DestinationInfo.ADD_OPERATION_TYPE) { 099 if (tempDest.getConnection() != null) { 100 tempDest = (ActiveMQTempDestination) tempDest.createDestination(tempDest.getPhysicalName()); 101 } 102 connection.activeTempDestinations.put(tempDest, tempDest); 103 } else if (dinfo.getOperationType() == DestinationInfo.REMOVE_OPERATION_TYPE) { 104 connection.activeTempDestinations.remove(tempDest); 105 } 106 } 107 108}