繁体   English   中英

如何将xml文件的内容读取到Jms Queue

[英]How to read the content of xml file to Jms Queue

消息以xml格式排队,其中包含标记a。 我必须读取此标记,以便我的侦听器不会选择包含a,b或c的destinationInstance的消息。

Xml如下:

<?xml version="1.0"?>

-<Envelope>

    -<Header version="2.0">

        <senderInstance>asas</senderInstance>

        <destination>asassd</destination>

        <destinationInstance>a</destinationInstance>

        <correlationId>94825641</correlationId>

    </Header>

-<Envelope>

Java代码:

public void listenMessage(final String qLookUpName,final JMSClientFactory jmsClientFactory,int noOfListeners){

    Queue queue = null;

    QueueReceiver[] receiver = new QueueReceiver[noOfListeners + 1];

    Message message = null;

    queue = queueMap.get(qLookUpName);

    tracer.info("Queue name - " + queue);

    try {
        if (null == queue) {
            InitialContext ctx = new InitialContext(ldapProps);
            queue = (javax.jms.Queue) ctx.lookup(qLookUpName);
            queueMap.put(qLookUpName, queue);
        }
        logger.info("Entered listenJMSMessage()");

        for (int receiverCnt = 0; receiverCnt < noOfListeners; receiverCnt++) {
            try {
                logger.debug("queuename : " + queue.getQueueName());
                tracer.info("queuename : " + queue.getQueueName());
                receiver[receiverCnt] = session.createReceiver(queue);                                                  
                tracer.info("receiver : " + receiver);
                logger.debug("Listening for Messages");
                receiver[receiverCnt].setMessageListener(new MessageListener(){
                    JMSClientFactory clientFactory= jmsClientFactory;
                    Document xmlDocument;
                    public void onMessage(Message arg0) {
                        try {                               
                            logger.debug("got message "+ arg0.getJMSCorrelationID());                               
                            logger.debug("Received message from the queue :"  
                                    + arg0.getJMSCorrelationID()+" On Listener :" + this);                          
                            String xmlResponse=((TextMessage)arg0).getText();            
                            xmlDocument = DocumentBuilderFactory.newInstance()
                                    .newDocumentBuilder().parse(new InputSource(new StringReader(xmlResponse)));
                            String destinationInstance=xmlDocument.getElementsByTagName("destinationInstance").item(0).getTextContent();
                            logger.debug("  destinationInstance: " + destinationInstance);
                            if(destinationInstance.equalsIgnoreCase("a") || destinationInstance.equalsIgnoreCase("b") || 
                                    destinationInstance.equalsIgnoreCase("c")){

                                logger.debug("Calling process(arg0) method.");
                                clientFactory.process(arg0);
                            }

                        } catch (JMSException e) {
                            logger.error("Exception - " , e);
                            // TODO Auto-generated catch block
                            //e.printStackTrace();
                        } catch (Exception e) {
                            logger.error("Exception - " , e);
                        }   catch(Throwable t) {
                            logger.error("Error while dequeuing -", t);
                        } finally {
                            NDC.remove();
                        }

                    }
                });
            } catch (JMSException e) {
                logger.debug("stack trace -" ,e);
            }
        }
    } catch (NamingException e) {
        logger.debug("NamingException stack trace -" ,e);
    }

    logger.debug("Listening for another message");
}

默认JMS API将不允许基于此有效负载的消息选择。 您将不得不阅读并解析它,但这将破坏性地阅读消息。

JMS消息选择器如果消息传递应用程序需要过滤它接收的消息,则可以使用JMS API消息选择器,它允许消息使用者指定它感兴趣的消息。消息选择器将过滤消息的工作分配给JMS提供者而不是申请。 有关使用消息选择器的应用程序的示例,请参阅使用JMS API和会话Bean的应用程序。

消息选择器是包含表达式的String。 表达式的语法基于SQL92条件表达式语法的子集。 示例中的消息选择器选择具有设置为值“Sports”或“Opinion”值的NewsType属性的任何消息:

NewsType ='Sports'或NewsType ='意见'createConsumer和createDurableSubscriber方法允许您在创建消息使用者时将消息选择器指定为参数。

然后,消息使用者只接收其标题和属性与选择器匹配的消息。 (请参阅邮件标题和邮件属性。) 邮件选择器无法根据邮件正文的内容选择邮件。

您可能希望查看浏览消息,检查有效负载,然后根据条件查看处理消息,但这是一个更长的解决方案,需要浏览,过滤然后阅读消息。 这很麻烦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM