use of org.apache.activemq.artemis.api.core.management.QueueControl in project activemq-artemis by apache.
the class ActiveMQServerControlTest method testCreateQueueWithNullAddress.
@Test
public void testCreateQueueWithNullAddress() throws Exception {
SimpleString address = RandomUtil.randomSimpleString();
SimpleString name = address;
ActiveMQServerControl serverControl = createManagementControl();
checkNoResource(ObjectNameBuilder.DEFAULT.getQueueObjectName(address, name, RoutingType.ANYCAST));
serverControl.createQueue(null, name.toString(), "ANYCAST");
checkResource(ObjectNameBuilder.DEFAULT.getQueueObjectName(name, name, RoutingType.ANYCAST));
QueueControl queueControl = ManagementControlHelper.createQueueControl(address, name, RoutingType.ANYCAST, mbeanServer);
Assert.assertEquals(address.toString(), queueControl.getAddress());
Assert.assertEquals(name.toString(), queueControl.getName());
Assert.assertNull(queueControl.getFilter());
Assert.assertEquals(true, queueControl.isDurable());
Assert.assertEquals(false, queueControl.isTemporary());
serverControl.destroyQueue(name.toString());
checkNoResource(ObjectNameBuilder.DEFAULT.getQueueObjectName(address, name, RoutingType.ANYCAST));
}
use of org.apache.activemq.artemis.api.core.management.QueueControl in project activemq-artemis by apache.
the class ActiveMQServerControlTest method testCreateAndDestroyQueue_4.
@Test
public void testCreateAndDestroyQueue_4() throws Exception {
SimpleString address = RandomUtil.randomSimpleString();
SimpleString name = RandomUtil.randomSimpleString();
boolean durable = RandomUtil.randomBoolean();
boolean purgeOnNoConsumers = RandomUtil.randomBoolean();
boolean autoCreateAddress = true;
int maxConsumers = RandomUtil.randomInt();
ActiveMQServerControl serverControl = createManagementControl();
checkNoResource(ObjectNameBuilder.DEFAULT.getQueueObjectName(address, name, RoutingType.ANYCAST));
serverControl.createQueue(address.toString(), RoutingType.ANYCAST.toString(), name.toString(), null, durable, maxConsumers, purgeOnNoConsumers, autoCreateAddress);
checkResource(ObjectNameBuilder.DEFAULT.getQueueObjectName(address, name, RoutingType.ANYCAST));
QueueControl queueControl = ManagementControlHelper.createQueueControl(address, name, RoutingType.ANYCAST, mbeanServer);
Assert.assertEquals(address.toString(), queueControl.getAddress());
Assert.assertEquals(name.toString(), queueControl.getName());
Assert.assertNull(queueControl.getFilter());
Assert.assertEquals(durable, queueControl.isDurable());
Assert.assertEquals(purgeOnNoConsumers, queueControl.isPurgeOnNoConsumers());
Assert.assertEquals(maxConsumers, queueControl.getMaxConsumers());
Assert.assertEquals(false, queueControl.isTemporary());
checkResource(ObjectNameBuilder.DEFAULT.getAddressObjectName(address));
AddressControl addressControl = ManagementControlHelper.createAddressControl(address, mbeanServer);
Assert.assertEquals(address.toString(), addressControl.getAddress());
serverControl.destroyQueue(name.toString(), true, true);
checkNoResource(ObjectNameBuilder.DEFAULT.getQueueObjectName(address, name, RoutingType.ANYCAST));
checkNoResource(ObjectNameBuilder.DEFAULT.getAddressObjectName(address));
}
use of org.apache.activemq.artemis.api.core.management.QueueControl 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.QueueControl in project activemq-artemis by apache.
the class JMXExample 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
connection = cf.createQueueConnection();
// Step 5. Create a JMS Session
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
// Step 7. Create a Text Message
TextMessage message = session.createTextMessage("This is a text message");
System.out.println("Sent message: " + message.getText());
// Step 8. Send the Message
producer.send(message);
// Step 9. Retrieve the ObjectName of the queue. This is used to identify the server resources to manage
ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(queue.getQueueName()), SimpleString.toSimpleString(queue.getQueueName()), RoutingType.ANYCAST);
// Step 10. Create JMX Connector to connect to the server's MBeanServer
// 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(JMXExample.JMX_URL), env);
// Step 11. Retrieve the MBeanServerConnection
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
// Step 12. Create a QueueControl proxy to manage the queue on the server
QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
// Step 13. Display the number of messages in the queue
System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
// Step 14. Remove the message sent at step #8
System.out.println("message has been removed: " + queueControl.removeMessages(null));
// Step 15. Display the number of messages in the queue
System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
// Step 16. We close the JMX connector
connector.close();
// Step 17. Create a JMS Message Consumer on the queue
MessageConsumer messageConsumer = session.createConsumer(queue);
// Step 18. Start the Connection
connection.start();
// Step 19. Trying to receive a message. Since the only message in the queue was removed by a management
// operation, there is none to consume.
// The call will timeout after 5000ms and messageReceived will be null
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
if (messageReceived != null) {
throw new IllegalStateException("message should be null!");
}
System.out.println("Received message: " + messageReceived);
} finally {
// Step 20. Be sure to close the resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
use of org.apache.activemq.artemis.api.core.management.QueueControl 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);
}
}
Aggregations