tags:

views:

577

answers:

1

I have configured my Topic in jbossmq-destinations-service.xml as follows:

<mbean code="org.jboss.mq.server.jmx.Topic"
    name="jboss.mq.destination:service=Topic,name=myTopicName">
  <depends optional-attribute-name="DestinationManager">
           jboss.mq:service=DestinationManager</depends>
  <depends optional-attribute-name="SecurityManager">
           jboss.mq:service=SecurityManager</depends>
  <attribute name="SecurityConf">
    <security>
      <role name="guest" read="true" write="true" create="true"/>
      <role name="publisher" read="true" write="true" create="false"/>
      <role name="durpublisher" read="true" write="true" create="true"/>
    </security>
  </attribute>
</mbean>

I can easily connect to the topic to publish messages by doing a JNDI lookup on the topic name and everything works fine.

However, I want to be able to clear the messages at certain intervals since they may become out of date depending on some other system functionality but I can't find any examples or explanations of how to do this.

The problem is that when I do a JNDI lookup on the topic it returns a org.jboss.mq.SpyTopic which does not have any functionality for removing messages or even to find out how many messages are on the topic. All the examples seem to suggest that org.jboss.mq.server.JMSTopic has all the functionality but I can't figure out how to convert a SpyTopic to a JMSTopic or how to set up the topic so that the JNDI lookup will return a JMSTopic.

Can anyone help?
Thanks.

A: 

Finally after a lot of searching I came up with this method.

public static void removeAllMessagesFromTopic(String topicName)
{
    try
    {
        //==============================================================
        // Set up the name of the topic object.
        // Alternative way:
        // objName = new ObjectName(
        //     "jboss.mq.destination:service=Topic,name=" + topicName);
        //==============================================================
        String domain = "jboss.mq.destination";
        Hashtable<String, String> keys = new Hashtable<String, String>();
        keys.put("service", "Topic");
        keys.put("name", topicName);
        ObjectName objName = new ObjectName(domain, keys);

        //==============================================================
        // Get the MBean server
        //==============================================================
        MBeanServer server = (MBeanServer)MBeanServerFactory
                .findMBeanServer(null).iterator().next();

        //==============================================================
        // Invoke the MBean
        //==============================================================
        Integer msgCount = (Integer)server.getAttribute(objName, "AllMessageCount");

        log.debug("Invoking removeAllMessages. (" + msgCount + "messages)");

        server.invoke(objName, "removeAllMessages",
                new Object[] {},  // No paramaters needed for removeAllMessages
                new String[] {});

        log.debug("Messages removed.");
    }
    catch (Exception ex)
    {
        log.error("Failed to remove messages from topic. Exception: " + ex);
        ex.printStackTrace();
    }

}