How to fetch Enqueue Count for ActiveMQ? - java

Can someone please help me how to fetch the Enqueue Count for a particular queue in Active MQ?
Here is my code:
BrokerService broker = new BrokerService()
String queueName= "queue.Test"
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory('ACTIVEMQ URL')
Connection connection = connectionFactory.createConnection()
connection.start()
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)
ActiveMQDestination requestDestination = session.createTopic(queueName)
QueueViewMBean view = (QueueViewMBean)broker.getDestination(requestDestination)
println("Count = "+view.getEnqueueCount())
But this does not seem to be working. How can I fix it?

You appear to be trying to cast ActiveMQ JMS client resources into JMX MBeans which of course would never work. You need to use JMX to access the management capabilities of the broker.
Some examples of using the MBeans can be found in the unit tests of the broker

Related

is it possible to establish rabbitMQ connection with javax.jms (not using rabbitMQ jms client/ java client )? if yes how?

is it possible to establish rabbitMQ connection with javax.jms (not using rabbitMQ jms client/ java client )? if yes how (not with springboot).
You either need to use the driver or write your own driver. You should not expect anyone here to write it for you. What have you tried?
Yes it's possible. RabbitMQ provides a class implementing JMS Connection factory com.rabbitmq.client.ConnectionFactory:
ConnectionFactory connectionFactory = new Connection().newConnection();
// create a Connection
connection = connectionFactory.createConnection();
connection.setClientID(clientId);
// create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// create the Topic from which messages will be received
Topic topic = session.createTopic(topicName);
// create a MessageConsumer for receiving messages
messageConsumer = session.createConsumer(topic);
// start the connection in order to receive messages
connection.start();
Except for the first line (Factory creation), this is pure JMS code.

Rabbit Mq status check

In my spring, hibernate application RMQ is used. I have a url in this application to check the status the DB connection and the status of the threads. I also want to include the status check of RMQ connection from the application. Please guide me.
It is hard to give a specific answer as this depends on how you manage connections to the RabbitMQ broker in your application...
However in essence all you need to do is look at the IsOpen property on the connection
You probably have this code somewhere in your application:
ConnectionFactory factory = new ConnectionFactory();
factory.set...
Connection connection = factory.newConnection();
all you need to do to check the current state is:
bool connected = connection.IsOpen();

How to initialize activeMQ broker with failover

I need to send JMS messages to the following provider location:
failover:(tcp://amq.vip.ebay.com:61616,tcp://amqstby.vip.ebay.com:61616)?initialReconnectDelay=100&randomize=false&wireFormat.maxInactivityDuration=0
How to correctly initialize ConnectorFactory for it? Should I just do the follwing?
String url = "failover:(tcp://amq.vip.ebay.com:61616,tcp://amqstby.vip.ebay.com:61616)?initialReconnectDelay=100&randomize=false&wireFormat.maxInactivityDuration=0";
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Or the things are more tricky with this kind of provider urls?
That is the correct syntax. Be careful when turning off the inactivity monitor.

Glassfish - get all queues in a session

My question is pretty simple, but I am having a bad time finding a solution.
I want to be able to get all the queues in the server (or session, that would also be ok). Is that possible?
My situation is the following:
I am new to openMQ, Glassfish, activeMQ and so on. I developed a monitoring system for activeMQ in which I get all the existing queues and show to the user, so it can get information about number of messages and so on.
To do that, I have this code:
ActiveMQConnection.makeConnection("tcp://localhost:61616");
activeMQConnection.start();
//Get queues
DestinationSource destinationSource = activeMQConnection.getDestinationSource();
Set<ActiveMQQueue> queues = destinationSource.getQueues();
this last line gets all the queues for the connection, and this is exactly what I need. But this was my code for ActiveMQ.
Now the team decided to change to openMQ, and I have to adapt my monitoring system to be able to handle that. I would like to use LDAP so I can do it technology-independent. After a lot of research I came to this code:
ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("cn=QueueConnectionFactory");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue myQueue = session.createQueue("myQueue");
This is good, because it would be completely independente (I think, have to test it). But the problem is that I have to register the queue, something I would not like to do. I would like to get all the existing queues in the server without any need for registering, but I can't find any GetQueues() method or any way to mimic its behavior.
I also found out the the DestinationSource class in activeMQ inherits from MessageListener, but this class doesn't provides any similar method :(
Could you please help me?
thank you,
Oscar
I was able to do that using JMX, here is the code:
HashMap environment = new HashMap();
String[] credentials = new String[] { "user", "pass" };
environment.put(JMXConnector.CREDENTIALS, credentials);
JMXServiceURL url = new JMXServiceURL("URL");
// Get JMX connector, supplying user name and password
JMXConnector jmxc1 = JMXConnectorFactory.connect(url, environment);
// Get MBean server connection
MBeanServerConnection mbsc = jmxc1.getMBeanServerConnection();
ObjectName destMgrConfigName = new ObjectName(MQObjectName.DESTINATION_MANAGER_MONITOR_MBEAN_NAME);
// Create operation's parameter and signature arrays
Object opParams[] = {};
String opSig[] = {};
// Invoke operation
ObjectName[] objectNames = (ObjectName[]) mbsc.invoke(destMgrConfigName, DestinationOperations.GET_DESTINATIONS, opParams, opSig);
for (ObjectName objectName : objectNames) {
System.out.println(objectName.getCanonicalName());
System.out.println(objectName.getKeyProperty("name"));
}
more references here: http://forums.oracle.com/forums/thread.jspa?threadID=2129291&tstart=0

Simple JMS Clients on OS X

I haven't touched any J2EE stuff in years and I need to whip up a quick JMS client for a demo.
I'm using Eclipse, on OS X and I can't even get started because I can't seem to figure out how to get the required libraries.
This is supposed to be a simple stand alone application (not running in a container) that pulls messages from a topic.
Every JMS implementation has its own set of libraries that specify how you get the initial connection factory. If you have an existing server from which to pull messages, you need to examine the documentation of that server to determine where to find the libraries to put in your classpath and how to create your initial connection factory. If you want to create a server for the purposes of the demonstration, I recommend using an embedded Active MQ broker.
Once you have your connection factory, polling for messages from a topic is pretty straightforward. Here is some example code which can be called to drain a topic of its current messages.
// Implementation specific code
public void drainTopic(TopicConnectionFactory factory, String topicName, String subscriberId)
// set factory properties like the server ID
Connection conn = factory.createConnection();
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(topicName);
MessageConsumer consumer = session.createDurableSubscriber(topic, subscriberId);
Message message;
while (null != (message = consumer.receive(1000))) {
// do work on the message
}
conn.close();
}
Note the use of a durable subscriber. This means that I don't have to try to maintain a single connection all the time and handle the errors if it times out somehow. But because the subscription is durable, the server knows to retain any messages the topic receives while I'm not connected and provide them on my next connection. This code would be the same regardless of the host OS. The only tricky part is the creation of the provider specific connection factory.

Categories

Resources