2017-12-27 17 views
0

ActiveMqインスタンスがあり、トピックをリスンするクライアントが100人いるとします。 新しいメッセージがトピックに公開されると、それを受信するサブスクライバの数を制限できますか(例10のみ)?ActiveMqのトピック内のメッセージが配信されるユーザの最大数を制限する

他のメッセージングコンポーネントがこれを行うことができますか、回避策/強化されたベストプラクティスがある場合は、

答えて

0

これはデフォルトの設定ではありませんが、このようなことを行うシンプルなプラグインを作成できます。

addConsumerを傍受し、あまりにも多くのサブスクライバがある場合は、SecurityExceptionをスローするプラグインです。 activemq.xmlの設定でも設定可能にすることができます。

これはすばやく汚れた例であることにご注意ください。おそらくそれを強化する必要があります。

MaxSubscribersPlugin.javaは

import org.apache.activemq.broker.BrokerPluginSupport; 
import org.apache.activemq.broker.ConnectionContext; 
import org.apache.activemq.broker.region.Destination; 
import org.apache.activemq.broker.region.Subscription; 
import org.apache.activemq.command.ActiveMQDestination; 
import org.apache.activemq.command.ConsumerInfo; 

import java.util.Map; 

public class MaxSubscribersPlugin extends BrokerPluginSupport{ 

    private int maxSubscribers = 1000; 

    @Override 
    public Subscription addConsumer(ConnectionContext ctx, ConsumerInfo info) throws Exception { 
     ActiveMQDestination dest = info.getDestination(); 
     if (dest.isTopic() && !dest.getQualifiedName().contains("Advisory")) { // TODO better way to filter out Advisories 
      Map<ActiveMQDestination, Destination> destinations = ctx.getBroker().getBrokerService() 
                    .getAdminView().getBroker().getDestinationMap(); 
      Destination activeTopic = destinations.get(info.getDestination()); 
      if(activeTopic != null && activeTopic.getConsumers().size() >= getMaxSubscribers()) { 

       throw new SecurityException("Too many active subscribers on topic " 
         + info.getDestination().getPhysicalName() 
         + ". Max is " + getMaxSubscribers()); 
      } 
     } 

     return super.addConsumer(ctx, info); 
    } 

    public int getMaxSubscribers() { 
     return maxSubscribers; 
    } 

    public void setMaxSubscribers(int maxSubscribers) { 
     this.maxSubscribers = maxSubscribers; 
    } 

} 

.jarでそれを詰め込んでのActiveMQのlibフォルダに入れて。次に、それを設定することができます。 - あまりにも多くの消費者が接続している場合

<plugins> 
      <bean xmlns="http://www.springframework.org/schema/beans" id="throttler" 
       class="my.package.MaxSubscribersPlugin"> 
      <property name="maxSubscribers" value="10"/> 
     </bean> 
    </plugins> 

その後、彼らは例外が発生します:javax.jms.JMSSecurityException: Too many active subscribers on topic topicX. Max is 10

ありActiveMQの中にログエントリが同様にログに記録されます:

WARN | Security Error occurred on connection to: tcp://127.0.0.1:52335, Too many active subscribers on topic topicX. Max is 10

関連する問題