[jboss-cvs] JBoss Messaging SVN: r6472 - in trunk: examples/jms and 16 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Apr 17 08:03:10 EDT 2009


Author: jmesnil
Date: 2009-04-17 08:03:10 -0400 (Fri, 17 Apr 2009)
New Revision: 6472

Added:
   trunk/examples/jms/message-counters/
   trunk/examples/jms/message-counters/build.xml
   trunk/examples/jms/message-counters/readme.html
   trunk/examples/jms/message-counters/server0/
   trunk/examples/jms/message-counters/server0/client-jndi.properties
   trunk/examples/jms/message-counters/server0/jbm-configuration.xml
   trunk/examples/jms/message-counters/server0/jbm-jms.xml
   trunk/examples/jms/message-counters/server0/jbm-queues.xml
   trunk/examples/jms/message-counters/server0/jbm-security.xml
   trunk/examples/jms/message-counters/server0/jbm-standalone-beans.xml
   trunk/examples/jms/message-counters/server0/logs/
   trunk/examples/jms/message-counters/src/
   trunk/examples/jms/message-counters/src/org/
   trunk/examples/jms/message-counters/src/org/jboss/
   trunk/examples/jms/message-counters/src/org/jboss/jms/
   trunk/examples/jms/message-counters/src/org/jboss/jms/example/
   trunk/examples/jms/message-counters/src/org/jboss/jms/example/MessageCounterExample.java
Modified:
   trunk/.classpath
   trunk/src/main/org/jboss/messaging/core/config/Configuration.java
   trunk/src/main/org/jboss/messaging/core/config/impl/ConfigurationImpl.java
   trunk/src/main/org/jboss/messaging/core/config/impl/FileConfiguration.java
   trunk/src/main/org/jboss/messaging/core/management/impl/ManagementServiceImpl.java
   trunk/src/main/org/jboss/messaging/core/messagecounter/impl/MessageCounterManagerImpl.java
   trunk/src/main/org/jboss/messaging/core/server/impl/MessagingServerImpl.java
   trunk/src/schemas/jbm-configuration.xsd
   trunk/tests/config/ConfigurationTest-config.xml
   trunk/tests/src/org/jboss/messaging/tests/integration/paging/PageCrashTest.java
   trunk/tests/src/org/jboss/messaging/tests/unit/core/config/impl/FileConfigurationTest.java
Log:
MessageCounter Example

* added example for message counters (code is not commented, missing readme)
* added message counter configuration (max-day-history, sample-period)

Modified: trunk/.classpath
===================================================================
--- trunk/.classpath	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/.classpath	2009-04-17 12:03:10 UTC (rev 6472)
@@ -34,6 +34,7 @@
 	<classpathentry kind="src" path="examples/jms/large-message/src"/>
 	<classpathentry kind="src" path="examples/jms/last-value-queue/src"/>
 	<classpathentry kind="src" path="examples/jms/management/src"/>
+	<classpathentry kind="src" path="examples/jms/message-counters/src"/>
 	<classpathentry kind="src" path="examples/jms/message-group/src"/>
 	<classpathentry kind="src" path="examples/jms/message-priority/src"/>
 	<classpathentry kind="src" path="examples/jms/paging/src"/>


Property changes on: trunk/examples/jms/message-counters
___________________________________________________________________
Name: svn:ignore
   + build
logs


Added: trunk/examples/jms/message-counters/build.xml
===================================================================
--- trunk/examples/jms/message-counters/build.xml	                        (rev 0)
+++ trunk/examples/jms/message-counters/build.xml	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE project [
+      <!ENTITY libraries SYSTEM "../../../thirdparty/libraries.ent">
+      ]>
+
+<!-- =========================================================================================== -->
+<!--                                                                                             -->
+<!-- JBoss, Home of Professional Open Source                                                     -->
+<!-- Copyright 2005, JBoss Inc., and individual contributors as indicated                        -->
+<!-- by the @authors tag. See the copyright.txt in the distribution for a                        -->
+<!-- full listing of individual contributors.                                                    -->
+<!--                                                                                             -->
+<!-- This is free software; you can redistribute it and/or modify it                             -->
+<!-- under the terms of the GNU Lesser General Public License as                                 -->
+<!-- published by the Free Software Foundation; either version 2.1 of                            -->
+<!-- the License, or (at your option) any later version.                                         -->
+<!--                                                                                             -->
+<!-- This software is distributed in the hope that it will be useful,                            -->
+<!-- but WITHOUT ANY WARRANTY; without even the implied warranty of                              -->
+<!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU                            -->
+<!-- Lesser General Public License for more details.                                             -->
+<!--                                                                                             -->
+<!-- You should have received a copy of the GNU Lesser General Public                            -->
+<!-- License along with this software; if not, write to the Free                                 -->
+<!-- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA                          -->
+<!-- 02110-1301 USA, or see the FSF site: http://www.fsf.org.                                    -->
+<!--                                                                                             -->
+<!-- =========================================================================================== -->
+
+
+<project default="run" name="JBoss Messaging Message Counter Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.MessageCounterExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.MessageCounterExample"/>
+         <param name="jbm.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>
\ No newline at end of file

Added: trunk/examples/jms/message-counters/readme.html
===================================================================
--- trunk/examples/jms/message-counters/readme.html	                        (rev 0)
+++ trunk/examples/jms/message-counters/readme.html	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,186 @@
+<html>
+  <head>
+    <title>JBoss Messaging Expiration Example</title>
+    <link rel="stylesheet" type="text/css" href="../common/common.css">
+  </head>
+  <body>
+     <h1>JMS Expiration Example</h1>
+
+     <p>This example shows you how to define and deal with message expiration.</p>
+     <p>Messages can be retained in the messaging system for a limited period of time before being removed.
+         JMS specification states that clients should not receive messages that have been expired (but it does not guarantee this will not happen).</p>
+     <p>JBoss Messaging can assign a <em>expiry destination</em> to a given queue so that when messages are expired, they are removed from the queue and sent
+         to the expiry destination. These "expired" messages can later be consumed from the expiry destination for further inspection.
+     <p>
+         The example will send 1 message with a short <em>time-to-live</em> to a queue. We will wait for the message to expire and checks that the message
+         is no longer in the queue it was sent to.
+         We will instead consume it from an <em>expiry queue</em> where it was moved when it expired.
+     </p>
+     <h2>Example setup</h2>
+     <p>Expiry destinations are defined as in the queue settings configuration file <a href="server0/jbm-queues.xml">jbm-queues.xml</a>:</p>
+     <pre>
+         <code>&lt;address-settings match="jms.queue.exampleQueue"&gt;
+            &lt;expiry-address&gt;jms.queue.expiryQueue&lt;/expiry-address&gt;
+         &lt;/address-settings&gt;
+         </code>
+     </pre>          
+     <p>This configuration will moved expired messages from the <code>exampleQueue</code> to the <code>expiryQueue</code></p>
+     <p>JBoss Messaging allows to specify either a <code>Queue</code> by prefixing the <code>expiry-address</code> with <code>jms.queue.</code>
+         or a <code>Topic</code> by prefixing with <code>jms.topic.</code>.<br />
+         In this example, we will use a <code>Queue</code> to hold the expired messages.</p>
+     <p>Since we want to consume messages from this expiryQueue, we also need to add a JNDI binding to perform a lookup.
+         This is configured in <a href="server0/jbm-jms.xml">jbm-jms.xml</a></p>
+     <pre>
+         <code>&lt;queue name="expiryQueue"&gt;
+            &lt;entry name="/queue/expiryQueue"/&gt;
+         &lt;/queue&gt;</code>
+     </pre>
+     </p>
+     <h2>Example step-by-step</h2>
+     <p><i>To run the example, simply type <code>ant</code> from this directory</i></p>
+     <ol>
+        <li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
+        <pre>
+           <code>InitialContext initialContext = getContext();</code>
+        </pre>
+
+        <li>We look up the JMS queue object from JNDI</li>
+        <pre>
+           <code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
+        </pre>
+
+        <li>We look up the JMS connection factory object from JNDI</li>
+        <pre>
+           <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
+        </pre>
+
+        <li>We create a JMS connection</li>
+        <pre>
+           <code>connection = cf.createConnection();</code>
+        </pre>
+
+        <li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages</li>
+        <pre>
+           <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+        </pre>
+
+        <li>We create a JMS message producer on the session. This will be used to send the messages</li>
+        <pre>
+          <code>MessageProducer messageProducer = session.createProducer(topic);</code>
+       </pre>
+       
+       <li>Messages sent by this producer will be retained for 1s (1000ms) before expiration</li>
+       <pre>
+           <code>producer.setTimeToLive(1000);</code>
+       </pre>
+
+        <li>We create a text messages</li>
+        <pre>
+            <code>TextMessage message = session.createTextMessage("this is a text message");</code>
+        </pre>
+
+        <li>We send the message to the queue</li>
+        <pre>
+            <code>producer.send(message);</code>
+        </pre>
+        
+       <li>We sleep a little bit to let the message expire</li>
+        <pre>
+            <code>Thread.sleep(5000);</code>
+        </pre>
+
+        <p>We will now try to consume the message from the queue but it won't be there since it has expired</p>
+
+        <li>We create a JMS message consumer on the queue</li>
+        <pre>
+            <code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
+        </pre>
+        
+        <li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
+        <pre>
+           <code>connection.start();</code>
+        </pre>
+
+        <li>We try to receive a message from the queue. Since there is none, the call will timeout after 5000ms and <code>messageReceived</code> will be <code>null</code>
+        <pre>
+           <code>TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
+           System.out.println("Received message from " + queue.getQueueName() + ": " + messageReceived);</code>
+        </pre>
+        
+        <p>However, we have configured JBoss Messaging to send any expired messages to the <code>expiryQueue</code>.
+            We will now consume messages from this expiry queue and receives the <em>expired</em> message.</p>
+            
+        <li>We look up the JMS <em>expiry queue</em> object from JNDI</li>
+        <pre>
+           <code>Queue expiryQueue = (Queue)initialContext.lookup("/queue/expiryQueue");</code>
+        </pre>
+                  
+        <li>We create a JMS message consumer on the expiry queue</li>
+        <pre>
+            <code>MessageConsumer expiryConsumer = session.createConsumer(expiryQueue);</code>
+        </pre>
+        
+        <li>We consume a message from the expiry queue:</li>
+        <pre>
+            <code>messageReceived = (TextMessage)expiryConsumer.receive(5000);</code>
+        </pre>
+        
+        <li>The message consumed from the <em>expiry queue</em> has the <em>same content</em> than the message which was sent to the <em>queue</em>
+        <pre>
+            <code>System.out.println("Received message from " + expiryQueue.getQueueName() + ": " + messageReceived.getText());</code>
+        </pre>    
+            
+        <p>JMS does not specify the notion of expiry queue. From JMS point of view, the message received from the expiry queue
+            is a <strong>different</strong> message than the message expired from the queue: the two messages have the same content (properties and body) but
+            their JMS headers differ.<br />
+            JBoss Messaging defines additional properties to correlate the message received from the expiry queue with the 
+            message expired from the queue</p>
+            
+        <li>The expired message's destination is the expiry queue</li>
+        <pre>
+            <code>System.out.println("Destination of the expired message: " + ((Queue)messageReceived.getJMSDestination()).getQueueName());</code>
+        </pre>
+
+        <li>The expired message has its own <em>expiration time</em> (its time to live in the <strong>expiry queue</strong>)</li>
+        <pre>
+            <code>System.out.println("Expiration time of the expired message (relative to the expiry queue): " + messageReceived.getJMSExpiration());</code>
+        </pre>
+        
+        <p>As we have not defined a time-to-live for the expiry queue, messages sent to the expiry queue will be kept forever (their JMS Expiration value is 0)</p>
+
+        <li>The <strong>origin destination</strong> is stored in the <code>_JBM_ORIG_DESTINATION</code> property
+        <pre>
+            <code>System.out.println("*Origin destination* of the expired message: " + messageReceived.getStringProperty("_JBM_ORIG_DESTINATION"));</code>
+        </pre>
+
+        <li>The <strong>actual expiration time</strong> (when the message was expired from the queue) is stored in the <code>_JBM_ACTUAL_EXPIRY</code> property
+        <pre>
+            <code>System.out.println("*Actual expiration time* of the expired message: " + messageReceived.getLongProperty("_JBM_ACTUAL_EXPIRY"));</code>
+        </pre>
+
+        </p>    
+        <li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
+
+        <pre>
+           <code>finally
+           {
+              if (initialContext != null)
+              {
+                initialContext.close();
+              }
+              if (connection != null)
+              {
+                 connection.close();
+              }
+           }</code>
+        </pre>
+     </ol>
+     
+     <h2>More information</h2>
+     
+     <ul>
+         <li>Expiry address is <a href="../../../docs/userguide/en/html/configuration.html#configuration.queues">configured on a queue-basis</a></li>
+         <li>Expiry scan period and thread priority are <a href="../../../docs/userguide/en/html/configuration.html#configuration.main">configured at the server level</a></li>
+     </ul>
+  </body>
+</html>
\ No newline at end of file


Property changes on: trunk/examples/jms/message-counters/server0
___________________________________________________________________
Name: svn:ignore
   + data


Added: trunk/examples/jms/message-counters/server0/client-jndi.properties
===================================================================
--- trunk/examples/jms/message-counters/server0/client-jndi.properties	                        (rev 0)
+++ trunk/examples/jms/message-counters/server0/client-jndi.properties	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,3 @@
+java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
+java.naming.provider.url=jnp://localhost:1099
+java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Added: trunk/examples/jms/message-counters/server0/jbm-configuration.xml
===================================================================
--- trunk/examples/jms/message-counters/server0/jbm-configuration.xml	                        (rev 0)
+++ trunk/examples/jms/message-counters/server0/jbm-configuration.xml	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,33 @@
+<deployment xmlns="urn:jboss:messaging"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:jboss:messaging ../../../../src/schemas/jbm-configuration.xsd">
+   <configuration>
+
+      <jmx-management-enabled>true</jmx-management-enabled>
+      <message-counter-enabled>true</message-counter-enabled>
+      <message-counter-sample-period>10000</message-counter-sample-period>
+      <message-counter-max-day-history>2</message-counter-max-day-history>
+
+      <!-- Connectors -->
+      <connectors>
+         <connector name="netty-connector">
+            <factory-class>org.jboss.messaging.integration.transports.netty.NettyConnectorFactory</factory-class>
+            <param key="jbm.remoting.netty.port" value="5445" type="Integer"/>
+         </connector>
+      </connectors>
+      
+      <!-- Acceptors -->
+      <acceptors>
+         <acceptor name="netty-acceptor">
+            <factory-class>org.jboss.messaging.integration.transports.netty.NettyAcceptorFactory</factory-class>
+            <param key="jbm.remoting.netty.port" value="5445" type="Integer"/>
+         </acceptor>
+      </acceptors>
+
+      <!-- Other config -->
+
+      <journal-min-files>2</journal-min-files>
+
+   </configuration>
+
+</deployment>

Added: trunk/examples/jms/message-counters/server0/jbm-jms.xml
===================================================================
--- trunk/examples/jms/message-counters/server0/jbm-jms.xml	                        (rev 0)
+++ trunk/examples/jms/message-counters/server0/jbm-jms.xml	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,25 @@
+<deployment xmlns="urn:jboss:messaging"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-jms.xsd ">
+   <!--the connection factory used by the example-->
+   <connection-factory name="ConnectionFactory">
+      <connector-ref connector-name="netty-connector"/>
+      <entries>
+         <entry name="ConnectionFactory"/>
+         <entry name="XAConnectionFactory"/>
+         <entry name="java:/ConnectionFactory"/>
+         <entry name="java:/XAConnectionFactory"/>
+      </entries>
+   </connection-factory>
+
+   <!--the queue used by the example-->
+   <queue name="exampleQueue">
+      <entry name="/queue/exampleQueue"/>
+   </queue>
+
+   <!--the expiry queue where expired messages will be sent-->
+   <queue name="expiryQueue">
+      <entry name="/queue/expiryQueue"/>
+   </queue>
+
+</deployment>
\ No newline at end of file

Added: trunk/examples/jms/message-counters/server0/jbm-queues.xml
===================================================================
--- trunk/examples/jms/message-counters/server0/jbm-queues.xml	                        (rev 0)
+++ trunk/examples/jms/message-counters/server0/jbm-queues.xml	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,30 @@
+<settings xmlns="urn:jboss:messaging"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-queues.xsd ">
+
+   <!--security for example queue-->
+   <security match="jms.queue.exampleQueue">
+      <permission type="createDurableQueue" roles="guest"/>
+      <permission type="deleteDurableQueue" roles="guest"/>
+      <permission type="createTempQueue" roles="guest"/>
+      <permission type="deleteTempQueue" roles="guest"/>
+      <permission type="consume" roles="guest"/>
+      <permission type="send" roles="guest"/>
+   </security>
+
+   <!--security for dead letter queue-->
+   <security match="jms.queue.expiryQueue">
+      <permission type="createDurableQueue" roles="guest"/>
+      <permission type="deleteDurableQueue" roles="guest"/>
+      <permission type="createTempQueue" roles="guest"/>
+      <permission type="deleteTempQueue" roles="guest"/>
+      <permission type="consume" roles="guest"/>
+      <permission type="send" roles="guest"/>
+   </security>
+
+   <!--override the expiry address for the example-->
+   <address-settings match="jms.queue.exampleQueue">
+      <expiry-address>jms.queue.expiryQueue</expiry-address>
+   </address-settings>
+
+</settings>

Added: trunk/examples/jms/message-counters/server0/jbm-security.xml
===================================================================
--- trunk/examples/jms/message-counters/server0/jbm-security.xml	                        (rev 0)
+++ trunk/examples/jms/message-counters/server0/jbm-security.xml	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,7 @@
+<deployment xmlns="urn:jboss:messaging" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-security.xsd ">
+   <!-- the default user.  this is used where username is null-->
+   <defaultuser name="guest" password="guest">
+      <role name="guest"/>
+   </defaultuser>
+</deployment>
\ No newline at end of file

Added: trunk/examples/jms/message-counters/server0/jbm-standalone-beans.xml
===================================================================
--- trunk/examples/jms/message-counters/server0/jbm-standalone-beans.xml	                        (rev 0)
+++ trunk/examples/jms/message-counters/server0/jbm-standalone-beans.xml	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+   <bean name="Naming" class="org.jnp.server.NamingBeanImpl"/>
+
+   <!-- JNDI server. Disable this if you don't want JNDI -->
+   <bean name="JNDIServer" class="org.jnp.server.Main">
+      <property name="namingInfo">
+         <inject bean="Naming"/>
+      </property>
+      <property name="port">1099</property>
+      <property name="bindAddress">localhost</property>
+      <property name="rmiPort">1098</property>
+      <property name="rmiBindAddress">localhost</property>
+   </bean>
+   
+   <!-- MBean server -->
+   <bean name="MBeanServer" class="javax.management.MBeanServer">
+      <constructor factoryClass="java.lang.management.ManagementFactory"
+                   factoryMethod="getPlatformMBeanServer"/>
+   </bean> 
+
+   <!-- The core configuration -->
+   <bean name="Configuration" class="org.jboss.messaging.core.config.impl.FileConfiguration"/>
+
+   <!-- The security manager -->
+   <bean name="JBMSecurityManager" class="org.jboss.messaging.core.security.impl.JBMSecurityManagerImpl">
+      <start ignored="true"/>
+      <stop ignored="true"/>
+   </bean>
+
+   <!-- The core server -->
+   <bean name="MessagingServer" class="org.jboss.messaging.core.server.impl.MessagingServerImpl"> 
+      <constructor>
+         <parameter>
+            <inject bean="Configuration"/>
+         </parameter>
+         <parameter>
+            <inject bean="MBeanServer"/>
+         </parameter>
+         <parameter>
+            <inject bean="JBMSecurityManager"/>
+         </parameter>        
+      </constructor>         
+   </bean>
+   
+   <!-- The JMS server -->
+   <bean name="JMSServerManager" class="org.jboss.messaging.jms.server.impl.JMSServerManagerImpl">
+      <constructor>         
+         <parameter>
+            <inject bean="MessagingServer"/>
+         </parameter>
+      </constructor>
+   </bean>
+
+</deployment>

Added: trunk/examples/jms/message-counters/src/org/jboss/jms/example/MessageCounterExample.java
===================================================================
--- trunk/examples/jms/message-counters/src/org/jboss/jms/example/MessageCounterExample.java	                        (rev 0)
+++ trunk/examples/jms/message-counters/src/org/jboss/jms/example/MessageCounterExample.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -0,0 +1,153 @@
+/*
+   * JBoss, Home of Professional Open Source
+   * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
+   * by the @authors tag. See the copyright.txt in the distribution for a
+   * full listing of individual contributors.
+   *
+   * This is free software; you can redistribute it and/or modify it
+   * under the terms of the GNU Lesser General Public License as
+   * published by the Free Software Foundation; either version 2.1 of
+   * the License, or (at your option) any later version.
+   *
+   * This software is distributed in the hope that it will be useful,
+   * but WITHOUT ANY WARRANTY; without even the implied warranty of
+   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+   * Lesser General Public License for more details.
+   *
+   * You should have received a copy of the GNU Lesser General Public
+   * License along with this software; if not, write to the Free
+   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+   */
+package org.jboss.jms.example;
+
+import java.util.HashMap;
+
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.QueueConnection;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.QueueSession;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.management.MBeanServerConnection;
+import javax.management.MBeanServerInvocationHandler;
+import javax.management.ObjectName;
+import javax.management.openmbean.CompositeData;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.management.remote.JMXServiceURL;
+import javax.naming.InitialContext;
+
+import org.jboss.messaging.core.management.MessageCounterInfo;
+import org.jboss.messaging.core.management.ObjectNames;
+import org.jboss.messaging.jms.server.management.JMSQueueControlMBean;
+
+/**
+ * An example showing how to use message counters to have information on a queue.
+ *
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ *
+ */
+public class MessageCounterExample extends JMSExample
+{
+   private String JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:3001/jmxrmi";
+
+   public static void main(String[] args)
+   {
+      String[] serverJMXArgs = new String[] { "-Dcom.sun.management.jmxremote",
+                                             "-Dcom.sun.management.jmxremote.port=3001",
+                                             "-Dcom.sun.management.jmxremote.ssl=false",
+                                             "-Dcom.sun.management.jmxremote.authenticate=false" };
+      new MessageCounterExample().run(serverJMXArgs, args);
+   }
+
+   public boolean runExample() throws Exception
+   {
+      QueueConnection connection = null;
+      InitialContext initialContext = null;
+      try
+      {
+         // Step 1. Create an initial context to perform the JNDI lookup.
+         initialContext = getContext(0);
+
+         // Step 2. Perfom a lookup on the queue
+         Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
+
+         // Step 3. Perform a lookup on the Connection Factory
+         QueueConnectionFactory cf = (QueueConnectionFactory)initialContext.lookup("/ConnectionFactory");
+
+         // Step 4.Create a JMS Connection, session and a producer for the queue
+         connection = cf.createQueueConnection();
+         QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
+         MessageProducer producer = session.createProducer(queue);
+
+         // Step 7. Create and send a Text Message
+         TextMessage message = session.createTextMessage("This is a text message");
+         producer.send(message);
+         System.out.println("Sent message: " + message.getText());
+
+         System.out.println("Sleep a little bit to have the message counters sampled...");
+         Thread.sleep(12000);
+         
+         ObjectName on = ObjectNames.getJMSQueueObjectName(queue.getQueueName());
+         JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap());
+         MBeanServerConnection mbsc = connector.getMBeanServerConnection();
+         JMSQueueControlMBean queueControl = (JMSQueueControlMBean)MBeanServerInvocationHandler.newProxyInstance(mbsc,
+                                                                                                                 on,
+                                                                                                                 JMSQueueControlMBean.class,
+                                                                                                                 false);
+
+         CompositeData compositeData = queueControl.listMessageCounter();
+         MessageCounterInfo messageCounter = MessageCounterInfo.from(compositeData);
+         displayMessageCounter(messageCounter);
+
+         System.out.println("Sleep a little bit again...");
+         Thread.sleep(12000);
+         
+         compositeData = queueControl.listMessageCounter();
+         messageCounter = MessageCounterInfo.from(compositeData);
+
+         displayMessageCounter(messageCounter);
+
+         MessageConsumer consumer = session.createConsumer(queue);
+         
+         connection.start();
+         
+         TextMessage messageReceived = (TextMessage)consumer.receive(5000);
+         System.out.format("Received message: %s\n\n", messageReceived.getText());
+         
+         System.out.println("Sleep a little bit on last time...");
+         Thread.sleep(12000);
+         
+         compositeData = queueControl.listMessageCounter();
+         messageCounter = MessageCounterInfo.from(compositeData);
+
+         displayMessageCounter(messageCounter);
+         
+         return true;
+      }
+      finally
+      {
+         // Step 20. Be sure to close our JMS resources!
+         if (initialContext != null)
+         {
+            initialContext.close();
+         }
+         if (connection != null)
+         {
+            connection.close();
+         }
+      }
+   }
+   
+   private void displayMessageCounter(MessageCounterInfo counter)
+   {
+      System.out.format("%s (sample updated at %s)\n",  counter.getName(), counter.getUdpateTimestamp());
+      System.out.format("   %s message(s) added to the queue (since last sample: %s)\n", counter.getCount(), counter.getCountDelta());
+      System.out.format("   %s message(s) in the queue (since last sample: %s)\n", counter.getDepth(), counter.getDepthDelta());
+      System.out.format("   last message added at %s\n\n", counter.getLastAddTimestamp());
+   }
+
+}

Modified: trunk/src/main/org/jboss/messaging/core/config/Configuration.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/config/Configuration.java	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/src/main/org/jboss/messaging/core/config/Configuration.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -238,6 +238,10 @@
 
    boolean isMessageCounterEnabled();
 
+   long getMessageCounterSamplePeriod();
+
+   int getMessageCounterMaxDayHistory();
+
    long getTransactionTimeoutScanPeriod();
 
    void setTransactionTimeoutScanPeriod(long period);
@@ -250,4 +254,5 @@
 
    void setMessageExpiryThreadPriority(int messageExpiryThreadPriority);
 
+
 }

Modified: trunk/src/main/org/jboss/messaging/core/config/impl/ConfigurationImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/config/impl/ConfigurationImpl.java	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/src/main/org/jboss/messaging/core/config/impl/ConfigurationImpl.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -103,6 +103,10 @@
 
    public static final boolean DEFAULT_MESSAGE_COUNTER_ENABLED = false;
 
+   public static final long DEFAULT_MESSAGE_COUNTER_SAMPLE_PERIOD = 10000;
+   
+   public static final int DEFAULT_MESSAGE_COUNTER_MAX_DAY_HISTORY = 10;
+   
    public static final long DEFAULT_TRANSACTION_TIMEOUT = 60000;
 
    public static final long DEFAULT_TRANSACTION_TIMEOUT_SCAN_PERIOD = 1000;
@@ -233,6 +237,10 @@
 
    protected boolean messageCounterEnabled = DEFAULT_MESSAGE_COUNTER_ENABLED;
 
+   protected long messageCounterSamplePeriod = DEFAULT_MESSAGE_COUNTER_SAMPLE_PERIOD;
+
+   protected int messageCounterMaxDayHistory = DEFAULT_MESSAGE_COUNTER_MAX_DAY_HISTORY;
+   
    protected long transactionTimeout = DEFAULT_TRANSACTION_TIMEOUT;
 
    protected long transactionTimeoutScanPeriod = DEFAULT_TRANSACTION_TIMEOUT_SCAN_PERIOD;
@@ -739,6 +747,16 @@
       return messageCounterEnabled;
    }
 
+   public long getMessageCounterSamplePeriod()
+   {
+      return messageCounterSamplePeriod;
+   }
+
+   public int getMessageCounterMaxDayHistory()
+   {
+      return messageCounterMaxDayHistory;
+   }
+
    public SimpleString getManagementAddress()
    {
       return managementAddress;

Modified: trunk/src/main/org/jboss/messaging/core/config/impl/FileConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/config/impl/FileConfiguration.java	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/src/main/org/jboss/messaging/core/config/impl/FileConfiguration.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -306,6 +306,10 @@
       wildcardRoutingEnabled = getBoolean(e, "wild-card-routing-enabled", wildcardRoutingEnabled);
 
       messageCounterEnabled = getBoolean(e, "message-counter-enabled", messageCounterEnabled);
+
+      messageCounterSamplePeriod = getLong(e, "message-counter-sample-period", messageCounterSamplePeriod);
+
+      messageCounterMaxDayHistory = getInteger(e, "message-counter-max-day-history", messageCounterMaxDayHistory);
       
       started = true;
    }

Modified: trunk/src/main/org/jboss/messaging/core/management/impl/ManagementServiceImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/management/impl/ManagementServiceImpl.java	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/src/main/org/jboss/messaging/core/management/impl/ManagementServiceImpl.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -141,15 +141,17 @@
 
    // Constructor ----------------------------------------------------
 
-   public ManagementServiceImpl(final MBeanServer mbeanServer, final boolean jmxManagementEnabled, final boolean messageCounterEnabled)
+   public ManagementServiceImpl(final MBeanServer mbeanServer, final Configuration configuration)
    {
       this.mbeanServer = mbeanServer;
-      this.jmxManagementEnabled = jmxManagementEnabled;
-      this.messageCounterEnabled = messageCounterEnabled;
+      this.jmxManagementEnabled = configuration.isJMXManagementEnabled();
+      this.messageCounterEnabled = configuration.isMessageCounterEnabled();
       registry = new HashMap<String, Object>();
       broadcaster = new NotificationBroadcasterSupport();
       noticationsEnabled = true;
       messageCounterManager = new MessageCounterManagerImpl();
+      messageCounterManager.setMaxDayCount(configuration.getMessageCounterMaxDayHistory());
+      messageCounterManager.reschedule(configuration.getMessageCounterSamplePeriod());
    }
 
    // Public --------------------------------------------------------

Modified: trunk/src/main/org/jboss/messaging/core/messagecounter/impl/MessageCounterManagerImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/messagecounter/impl/MessageCounterManagerImpl.java	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/src/main/org/jboss/messaging/core/messagecounter/impl/MessageCounterManagerImpl.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -29,6 +29,7 @@
 import java.util.Timer;
 import java.util.TimerTask;
 
+import org.jboss.messaging.core.config.impl.ConfigurationImpl;
 import org.jboss.messaging.core.logging.Logger;
 import org.jboss.messaging.core.messagecounter.MessageCounter;
 import org.jboss.messaging.core.messagecounter.MessageCounterManager;
@@ -46,11 +47,11 @@
 public class MessageCounterManagerImpl implements MessageCounterManager
 {
    
-   public static final long DEFAULT_SAMPLE_PERIOD = 10000;
+   public static final long DEFAULT_SAMPLE_PERIOD = ConfigurationImpl.DEFAULT_MESSAGE_COUNTER_SAMPLE_PERIOD;
 
    public static final long MIN_SAMPLE_PERIOD = 1000;
 
-   public static final int DEFAULT_MAX_DAY_COUNT = 10;
+   public static final int DEFAULT_MAX_DAY_COUNT = ConfigurationImpl.DEFAULT_MESSAGE_COUNTER_MAX_DAY_HISTORY;
 
    private static final Logger log = Logger.getLogger(MessageCounterManagerImpl.class);
    

Modified: trunk/src/main/org/jboss/messaging/core/server/impl/MessagingServerImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/server/impl/MessagingServerImpl.java	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/src/main/org/jboss/messaging/core/server/impl/MessagingServerImpl.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -499,7 +499,7 @@
          return;
       }
 
-      managementService = new ManagementServiceImpl(mbeanServer, configuration.isJMXManagementEnabled(), configuration.isMessageCounterEnabled());
+      managementService = new ManagementServiceImpl(mbeanServer, configuration);
 
       remotingService = new RemotingServiceImpl(configuration, this, managementService);
       

Modified: trunk/src/schemas/jbm-configuration.xsd
===================================================================
--- trunk/src/schemas/jbm-configuration.xsd	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/src/schemas/jbm-configuration.xsd	2009-04-17 12:03:10 UTC (rev 6472)
@@ -64,6 +64,12 @@
 				<xsd:element name="message-counter-enabled"
 					type="xsd:boolean" maxOccurs="1" minOccurs="0">
 				</xsd:element>
+                <xsd:element name="message-counter-sample-period"
+                    type="xsd:unsignedLong" maxOccurs="1" minOccurs="0">
+                </xsd:element>
+                <xsd:element name="message-counter-max-day-history"
+                    type="xsd:positiveInteger" maxOccurs="1" minOccurs="0">
+                </xsd:element>
 				<xsd:element name="connection-scan-period"
 					type="xsd:unsignedLong" maxOccurs="1" minOccurs="0">
 				</xsd:element>

Modified: trunk/tests/config/ConfigurationTest-config.xml
===================================================================
--- trunk/tests/config/ConfigurationTest-config.xml	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/tests/config/ConfigurationTest-config.xml	2009-04-17 12:03:10 UTC (rev 6472)
@@ -8,6 +8,9 @@
       <wild-card-routing-enabled>true</wild-card-routing-enabled>
       <management-address>Giraffe</management-address>
       <management-request-timeout>91</management-request-timeout>
+      <message-counter-enabled>true</message-counter-enabled>
+      <message-counter-sample-period>5678</message-counter-sample-period>
+      <message-counter-max-day-history>78910</message-counter-max-day-history>
       <connection-scan-period>6543</connection-scan-period>
       <transaction-timeout>98765</transaction-timeout>
       <transaction-timeout-scan-period>56789</transaction-timeout-scan-period>

Modified: trunk/tests/src/org/jboss/messaging/tests/integration/paging/PageCrashTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/integration/paging/PageCrashTest.java	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/tests/src/org/jboss/messaging/tests/integration/paging/PageCrashTest.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -24,7 +24,6 @@
 
 import java.io.File;
 import java.io.ObjectInputStream;
-import java.lang.management.ManagementFactory;
 import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.List;
@@ -59,8 +58,6 @@
 import org.jboss.messaging.core.client.ClientSession;
 import org.jboss.messaging.core.client.ClientSessionFactory;
 import org.jboss.messaging.core.config.Configuration;
-import org.jboss.messaging.core.management.ManagementService;
-import org.jboss.messaging.core.management.impl.ManagementServiceImpl;
 import org.jboss.messaging.core.paging.Page;
 import org.jboss.messaging.core.paging.PagedMessage;
 import org.jboss.messaging.core.paging.PagingManager;
@@ -252,10 +249,6 @@
    {
       JBMSecurityManager securityManager = new JBMSecurityManagerImpl();
 
-      ManagementService managementService = new ManagementServiceImpl(ManagementFactory.getPlatformMBeanServer(),
-                                                                      false,
-                                                                      false);
-
       MessagingServer server = new FailingMessagingServerImpl(configuration, securityManager);
 
       AddressSettings defaultSetting = new AddressSettings();

Modified: trunk/tests/src/org/jboss/messaging/tests/unit/core/config/impl/FileConfigurationTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/config/impl/FileConfigurationTest.java	2009-04-17 11:58:44 UTC (rev 6471)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/config/impl/FileConfigurationTest.java	2009-04-17 12:03:10 UTC (rev 6472)
@@ -65,6 +65,9 @@
       
       assertNotNull(conf.getBackupConnectorName());
       
+      assertEquals(true, conf.isMessageCounterEnabled());
+      assertEquals(5678, conf.getMessageCounterSamplePeriod());
+      assertEquals(78910, conf.getMessageCounterMaxDayHistory());
       //TODO - sort this out
       
 //      assertEquals("org.jboss.messaging.tests.unit.core.config.impl.TestConnectorFactory1", conf.getBackupConnectorConfiguration().getFactoryClassName());




More information about the jboss-cvs-commits mailing list