I have a requirement to monitor a JBoss messaging queue, where users should be able to delete posted messages dynamically. The application has a MDPOJO registered and is up and running at server start up.
My requirement is, users should be able to delete a posted message any time he wishes. I tried below proto codes :
Option1
public void listAllJMS_Messages()
{
try {
ObjectName objectName=new ObjectName("jboss.messaging.destination:name=XXX,service=Queue");
List ls = (List) server.invoke(objectName, "listMessages" , null, null);
List<javax.jms.Message> messages=(List<javax.jms.Message>)server.invoke(objectName, "listAllMessages" , null, null);
int count=0;
for(javax.jms.Message msg : messages) {
System.out.println((++count)+"t"+msg.getJMSMessageID());
if(msg.getJMSType() != null && msg.getJMSType().equalsIgnoreCase("Text")) {
TextMessage text = (TextMessage)msg;
System.out.println(text.getText());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println((++count)+"t"+msg.getText());
}
Option2
public void listMsg() {
String destinationName = "queue/XXX";
Context ic = null;
ConnectionFactory cf = null;
Connection connection = null;
try {
ic = getInitialContext();
cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
Queue queue = (Queue)ic.lookup(destinationName);
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
QueueBrowser qbrowser = session.createBrowser(queue);
Enumeration<?> qamsgs = qbrowser.getEnumeration();
while (qamsgs.hasMoreElements()){
Message msg = (Message)qamsgs.nextElement();
}
connection.start();
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
if(ic != null)
{
try
{
ic.close();
}
catch(Exception e1)
{
System.out.println(e1);
}
}
}
}
In both the options,messages are getting listed in case of no message consumers at present.If have a live message consumer MDB, then list is returned as null.
I defintely need MDB consumer up and running.But still need to monitor messages.I should be able to do below :
1. List messages]
2. Delete a particular message
3. Change priority