use of org.apache.activemq.artemis.api.core.management.MessageCounterInfo in project activemq-artemis by apache.
the class QueueControlTest method testListMessageCounter.
@Test
public void testListMessageCounter() throws Exception {
SimpleString address = RandomUtil.randomSimpleString();
SimpleString queue = RandomUtil.randomSimpleString();
session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
QueueControl queueControl = createManagementControl(address, queue);
ActiveMQServerControl serverControl = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
serverControl.enableMessageCounters();
serverControl.setMessageCounterSamplePeriod(100);
String jsonString = queueControl.listMessageCounter();
MessageCounterInfo info = MessageCounterInfo.fromJSON(jsonString);
Assert.assertEquals(0, info.getDepth());
Assert.assertEquals(0, info.getCount());
ClientProducer producer = session.createProducer(address);
producer.send(session.createMessage(durable));
Thread.sleep(200);
jsonString = queueControl.listMessageCounter();
info = MessageCounterInfo.fromJSON(jsonString);
Assert.assertEquals(1, info.getDepth());
Assert.assertEquals(1, info.getDepthDelta());
Assert.assertEquals(1, info.getCount());
Assert.assertEquals(1, info.getCountDelta());
producer.send(session.createMessage(durable));
Thread.sleep(200);
jsonString = queueControl.listMessageCounter();
info = MessageCounterInfo.fromJSON(jsonString);
Assert.assertEquals(2, info.getDepth());
Assert.assertEquals(1, info.getDepthDelta());
Assert.assertEquals(2, info.getCount());
Assert.assertEquals(1, info.getCountDelta());
consumeMessages(2, session, queue);
Thread.sleep(200);
jsonString = queueControl.listMessageCounter();
info = MessageCounterInfo.fromJSON(jsonString);
Assert.assertEquals(0, info.getDepth());
Assert.assertEquals(-2, info.getDepthDelta());
Assert.assertEquals(2, info.getCount());
Assert.assertEquals(0, info.getCountDelta());
session.deleteQueue(queue);
}
use of org.apache.activemq.artemis.api.core.management.MessageCounterInfo in project activemq-artemis by apache.
the class MessageCounterExample method main.
public static void main(final String[] args) throws Exception {
QueueConnection connection = null;
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// 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 5. 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());
// Step 6. Sleep a little bit so that the queue is sampled
System.out.println("Sleep a little bit to have the queue sampled...");
Thread.sleep(3000);
// Step 7. Use JMX to retrieve the message counters using the JMSQueueControl
ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(queue.getQueueName()), SimpleString.toSimpleString(queue.getQueueName()), RoutingType.ANYCAST);
// we dont actually need credentials as the guest login i sused but this is how its done
HashMap env = new HashMap();
String[] creds = { "guest", "guest" };
env.put(JMXConnector.CREDENTIALS, creds);
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), env);
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
// Step 8. List the message counters and convert them to MessageCounterInfo data structure.
String counters = queueControl.listMessageCounter();
MessageCounterInfo messageCounter = MessageCounterInfo.fromJSON(counters);
// Step 9. Display the message counter
displayMessageCounter(messageCounter);
// Step 10. Sleep again to have the queue sampled again
System.out.println("Sleep a little bit again...");
Thread.sleep(3000);
// Step 11. List the messages counters again
counters = queueControl.listMessageCounter();
messageCounter = MessageCounterInfo.fromJSON(counters);
displayMessageCounter(messageCounter);
// Step 12. Create a JMS consumer on the queue
MessageConsumer consumer = session.createConsumer(queue);
// Step 13. Start the connection to receive messages on the consumer
connection.start();
// Step 14. Receive a JMS message from the queue. It corresponds to the message sent at step #5
TextMessage messageReceived = (TextMessage) consumer.receive(5000);
System.out.format("Received message: %s%n%n", messageReceived.getText());
// Step 15. Sleep on last time to have the queue sampled
System.out.println("Sleep a little bit one last time...");
Thread.sleep(3000);
// Step 16. Display one last time the message counter
counters = queueControl.listMessageCounter();
messageCounter = MessageCounterInfo.fromJSON(counters);
displayMessageCounter(messageCounter);
} finally {
// Step 17. Be sure to close our JMS resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
use of org.apache.activemq.artemis.api.core.management.MessageCounterInfo in project activemq-artemis by apache.
the class ClusteredMessageCounterTest method testMessageCounterWithPaging.
@Test
public void testMessageCounterWithPaging() throws Exception {
startServers(0, 1);
try {
setupSessionFactory(0, isNetty());
setupSessionFactory(1, isNetty());
createQueue(0, "queues", "queue0", null, false);
createQueue(1, "queues", "queue0", null, false);
waitForBindings(1, "queues", 1, 0, true);
waitForBindings(0, "queues", 1, 0, false);
System.out.println("sending.....");
Thread sendThread = new Thread(new Runnable() {
@Override
public void run() {
try {
send(0, "queues", numMsg, true, null);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("messages sent.");
}
});
QueueControl control = (QueueControl) servers[1].getManagementService().getResource(ResourceNames.QUEUE + "queue0");
ActiveMQServerControl serverControl = (ActiveMQServerControl) servers[1].getManagementService().getResource(ResourceNames.BROKER);
serverControl.setMessageCounterSamplePeriod(300);
CountDownLatch resultLatch = new CountDownLatch(40);
MessageCounterCollector collector = new MessageCounterCollector(control, resultLatch);
timer1.schedule(collector, 0);
PeriodicalReceiver receiver = new PeriodicalReceiver(50, 1, 100);
timer2.schedule(receiver, 0);
sendThread.start();
try {
resultLatch.await(120, TimeUnit.SECONDS);
} finally {
stopFlag.set(true);
}
sendThread.join();
System.out.println("Results collected: " + results.size());
// checking
for (MessageCounterInfo info : results) {
assertTrue("countDelta should be positive " + info.getCountDelta() + dumpResults(results), info.getCountDelta() >= 0);
}
} finally {
timer1.cancel();
timer2.cancel();
stopServers(0, 1);
}
}
use of org.apache.activemq.artemis.api.core.management.MessageCounterInfo in project activemq-artemis by apache.
the class QueueControlTest method testResetMessageCounter.
@Test
public void testResetMessageCounter() throws Exception {
SimpleString address = RandomUtil.randomSimpleString();
SimpleString queue = RandomUtil.randomSimpleString();
session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
QueueControl queueControl = createManagementControl(address, queue);
ActiveMQServerControl serverControl = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
serverControl.enableMessageCounters();
serverControl.setMessageCounterSamplePeriod(MessageCounterManagerImpl.MIN_SAMPLE_PERIOD);
String jsonString = queueControl.listMessageCounter();
MessageCounterInfo info = MessageCounterInfo.fromJSON(jsonString);
Assert.assertEquals(0, info.getDepth());
Assert.assertEquals(0, info.getCount());
ClientProducer producer = session.createProducer(address);
producer.send(session.createMessage(durable));
Thread.sleep(MessageCounterManagerImpl.MIN_SAMPLE_PERIOD * 2);
jsonString = queueControl.listMessageCounter();
info = MessageCounterInfo.fromJSON(jsonString);
Assert.assertEquals(1, info.getDepth());
Assert.assertEquals(1, info.getDepthDelta());
Assert.assertEquals(1, info.getCount());
Assert.assertEquals(1, info.getCountDelta());
consumeMessages(1, session, queue);
Thread.sleep(MessageCounterManagerImpl.MIN_SAMPLE_PERIOD * 2);
jsonString = queueControl.listMessageCounter();
info = MessageCounterInfo.fromJSON(jsonString);
Assert.assertEquals(0, info.getDepth());
Assert.assertEquals(-1, info.getDepthDelta());
Assert.assertEquals(1, info.getCount());
Assert.assertEquals(0, info.getCountDelta());
queueControl.resetMessageCounter();
Thread.sleep(MessageCounterManagerImpl.MIN_SAMPLE_PERIOD * 2);
jsonString = queueControl.listMessageCounter();
info = MessageCounterInfo.fromJSON(jsonString);
Assert.assertEquals(0, info.getDepth());
Assert.assertEquals(0, info.getDepthDelta());
Assert.assertEquals(0, info.getCount());
Assert.assertEquals(0, info.getCountDelta());
session.deleteQueue(queue);
}
Aggregations