【问题标题】:How to read the content of xml file to Jms Queue如何将xml文件的内容读入Jms Queue
【发布时间】:2016-08-31 20:34:08
【问题描述】:

消息以 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 提供程序?

标签: java jms


【解决方案1】:

默认的 JMS API 不允许基于负载选择消息。您将必须阅读并解析该消息,但这将破坏性地阅读该消息。

JMS 消息选择器 如果您的消息应用程序需要过滤 它接收到的消息,您可以使用 JMS API 消息选择器, 它允许消息消费者指定它是什么消息 感兴趣。消息选择器分配过滤消息的工作 JMS 提供者而不是应用程序。举个例子 使用消息选择器的应用程序,请参阅 将 JMS API 与会话 Bean 一起使用。

消息选择器是一个包含表达式的字符串。语法 表达式基于 SQL92 条件的子集 表达式语法。示例中的消息选择器选择任何 具有设置为值“体育”的 NewsType 属性的消息 或“意见”:

NewsType = 'Sports' OR NewsType = 'Opinion' createConsumer 和 createDurableSubscriber 方法允许您指定消息 选择器作为创建消息使用者时的参数。

然后,消息消费者只接收其标头和 属性匹配选择器。 (请参阅消息标题和消息 属性。) 消息选择器不能根据 消息正文的内容。

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

【讨论】:

  • 监听器有没有办法读取和解析xml文件?
  • 监听器将读取和解析负载的非物质消息。然后,您必须从侦听器代码的逻辑中对其进行路由。
  • 你能提供步骤,如何从监听器代码上的逻辑路由它
  • 使用标准的 JMS Consumer /Listener 代码来获取消息。获取 JMS 消息(作为 TextMessage),获取 Text 将返回消息。然后,您可以使用 XML 解析器获取所需的 XPATH,然后使用 if/else 条件将它们路由到不同的端点。我猜最简单的方法是在上游设置一个消息属性,以便您可以使用 JMS 消息过滤器
  • 我已经添加了java代码...你能检查一下..它会工作吗
猜你喜欢
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多