Skip to content

Instantly share code, notes, and snippets.

@martin-cowie
Created April 21, 2017 14:10
Show Gist options
  • Save martin-cowie/2631bb8fd69fe2da3ffb270786a800fe to your computer and use it in GitHub Desktop.
Save martin-cowie/2631bb8fd69fe2da3ffb270786a800fe to your computer and use it in GitHub Desktop.
TopicStatsMBean.java
package com.acme;
import java.util.Set;
import com.pushtechnology.diffusion.api.publisher.Publisher;
import com.pushtechnology.diffusion.api.publisher.Publishers;
import com.pushtechnology.diffusion.api.topic.Topic;
/**
* TopicStats
*<P>
* Employ the Diffusion APIs to query the set of Topics, and expose a number of statistics upon an MBean.
* Using the Diffusion-JMX integration, this bean itself shows up as a set of topics
*
* @author martincowie - created Apr 2, 2012
* @since 4.1
*/
public class TopicStats implements TopicStatsMBean {
private Publisher publisher;
public TopicStats(Publisher publisher)
{
this.publisher = publisher;
}
@Override
/**
* Find all topics that have at least one subscription
*/
public long getUniqueSubscriptions()
{
long result = 0;
Set<Topic> topics = Publishers.getTopicTree().getAllTopics();
for( Topic topic : topics )
if( topic.hasSubscribers() )
result++;
return result;
}
@Override
public long getTotalSubscriptions()
{
long result = 0;
Set<Topic> topics = Publishers.getTopicTree().getAllTopics();
for( Topic topic : topics )
result += topic.getCurrentNumberOfSubscribers();
return result;
}
@Override
public int getTotalTopics()
{
return Publishers.getTopicTree().getAllTopics().size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment