如:Jboss 7
假设我在文件中定义了一个队列:
{JBOSS7_HOME}/standalone/configuration/standalone.xml
<jms-queue name="myQueue">
<entry name="java:/queue/myQueue"/>
<durable>true</durable>
</jms-queue>
问题:
如何以编程方式检查队列中的消息数量?
您可以使用以下代码。
String jndiFactory="org.jboss.naming.remote.client.InitialContextFactory";
String jmsFactory="jms/RemoteConnectionFactory";
String queueName="jms/queue/MyQueue";
String providerUrl="remote://ipAddress:4447";
String username="testuser";
String password="testpassword";
Session qsession = null;
Connection qcon = null;
QueueBrowser browser = null;
ConnectionFactory qconFactory;
InitialContext ctx = null;
int depth=0;
try{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, jndiFactory);
env.put(Context.PROVIDER_URL, providerUrl);
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
ctx = new InitialContext(env);
qconFactory = (QueueConnectionFactory) ctx.lookup(jmsFactory);
qcon = qconFactory.createConnection(username, password);
qsession = qcon.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup(qName);
browser = qsession.createBrowser(queue);
qcon.start();
for (Enumeration e = browser.getEnumeration(); e.hasMoreElements();e.nextElement()) {
depth++;
}
}catch(Exception e){
System.out.println("Some exception :"+e.toString());
}finally {
try {
if(qcon !=null)
qcon.close();
if(qsession !=null)
qsession.close();
if(browser !=null)
browser.close();
} catch (Exception e) {
System.out.println("Some exception :"+e.toString());
}
}
如果您想使用应用内方法,我将使用以下会话 Bean:
import java.util.Enumeration;
import java.util.HashMap;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.transaction.UserTransaction;
import org.apache.log4j.Logger;
import com.amway.dms.dataobject.event.EventHeader;
import com.amway.dms.dbaccess.event.EventHeaderAccess;
import com.amway.dms.util.common.DBUtils;
import com.amway.dms.util.common.StringUtil;
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
@Local(SBDMSIntegrationsInternalSenderLocal.class)
@Remote(SBDMSIntegrationsInternalSenderRemote.class)
public class SBQueuCount implements SBQueuCountRemote {
private static final String CONNECTION_FACTORY_NAME = "jms/testCF";
private static final String DESTINATION_NAME = "jms/testQueue";
// jms
@Resource(name = CONNECTION_FACTORY_NAME, mappedName = CONNECTION_FACTORY_NAME)
private ConnectionFactory connectionFactory;
@Resource(name = DESTINATION_NAME, mappedName = DESTINATION_NAME)
private Queue queue;
private static final Logger logger = Logger.getLogger(SBQueuCount.class);
@Override
public void getQueueDetails(String noOfMsgToPrint) {
logger.info("Begin getQueueDetails: noOfMsgToPrint:" + noOfMsgToPrint);
Connection connection;
Session session = null;
try {
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
int numOnQueue = 0;
QueueBrowser outQueueBrowser = null;
outQueueBrowser = session.createBrowser(queue);
if (outQueueBrowser == null) {
logger.info("outQueueBrowser is null");
}
Enumeration messagesOnQ = outQueueBrowser.getEnumeration();
if (messagesOnQ.hasMoreElements())
logger.info("Queue has elements");
while (messagesOnQ.hasMoreElements()) {
// TextMessage textMessage = (TextMessage) messagesOnQ.nextElement();
// if (StringUtil.isNumeric(noOfMsgToPrint)) {
// if (numOnQueue <= Integer.parseInt(noOfMsgToPrint)) {
// logger.info("EventHeader Message: " + textMessage.toString());
// }
// }
numOnQueue++;
}
outQueueBrowser.close();
logger.info("End number of messages on queue: " + numOnQueue);
} catch (JMSException e) {
logger.info("Error " + e);
e.printStackTrace();
}
}
}