0

として: Jboss 7

ファイルで定義されたキューがあるとしましょう:

{JBOSS7_HOME}/standalone/configuration/standalone.xml

<jms-queue name="myQueue">
    <entry name="java:/queue/myQueue"/>
    <durable>true</durable>
</jms-queue>

質問:

キュー内のメッセージ数をプログラムで確認する方法は?

4

2 に答える 2

1

次のコードを使用できます。

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());
    }
}
于 2013-10-29T10:35:37.307 に答える
0

アプリ内アプローチを使用する場合は、次のセッション 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();
        }

    }

}
于 2014-03-14T19:49:17.787 に答える