MDB onMessage()针对唯一请求的各种响应消息

时间:2018-08-17 12:43:52

标签: java-ee jms ejb

我目前对MDB经验不足。我想将各种消息发送到JMS响应队列。问题在于onMessage()是事务方法,并且该事实不允许我发送具有交错睡眠的各种消息。有没有一种方法可以针对一个请求消息发送多个响应消息?我尝试了上下文事务。这是我的onMessage方法:

public void onMessage(Message message) {


        synchronized (this) {
            try {

                LOG.debug("Received a message" + SystemUtils.LINE_SEPARATOR + message);
                if (message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    this.logMessage(textMessage, textMessage.getJMSDestination().toString(), "request");
                    String body = textMessage.getText();
                    Document doc = XMLHelper.parseText(body);
                    int amountOfXq = Integer.parseInt((String) AIAMultipleResponseEmulatorBean.initialCtx.lookup("java:comp/env/XQY_COUNT"));

                    for (int i = 0; i <= (amountOfXq - 1); i++) {

                            this.ctx.getUserTransaction().begin();
                        this.xqueryUri = new URI((String) AIAMultipleResponseEmulatorBean.initialCtx.lookup("java:comp/env/" + i));

                        LOG.debug(XMLHelper.prettyPrintXML(doc));
                        JMSHelper helper = JMSHelper.instance(this.destinationName);
                        TextMessage response = helper.createTextMessage();
                        System.out.println("Response just after creation: " + response.getText());
                        response.setStringProperty("MESSAGE_TYPE", "EMULATOR_RESPONSE");
                        response.setStringProperty("APPLICATION_TYPE", this.getClass().getName());
                        response.setStringProperty("APPLICATION_ID", this.getClass().getName());
                        response.setIntProperty("VERSION_ID", 1);
                        response.setJMSDeliveryMode(message.getJMSDeliveryMode());
                        LOG.debug("JMSCorrelationID=" + message.getJMSCorrelationID());
                        String osmID = this.getOSMID(message);
                        System.out.println("CorrelationID completo: " + message.getJMSCorrelationID());
                        System.out.println("CorrelationID osmID: " + osmID);
                        if( i == 0) {
                            response.setJMSCorrelationID(message.getJMSCorrelationID());
                        }else{
                            response.setJMSCorrelationID("TESA-CORR-" + osmID);
                            System.out.println("CorrelationID osmID: TESA-CORR-" + osmID);
                        }
                        response.setJMSPriority(message.getJMSPriority());
                        this.transformRequestToResponse(body, response); // HERE
                        this.logMessage(response, textMessage.getJMSDestination().toString(), "response");
                        LOG.debug(response.getText());
                        helper.sendMessageNoResponse(response);
                        //TimeUnit.SECONDS.sleep(5);
                        Thread.sleep(5000);

                        this.ctx.getUserTransaction().commit();


                    }

                }
            } catch (Exception var9) {
                LOG.error("onMessage:Exception:[" + var9.getMessage() + "]", var9);
                this.ctx.setRollbackOnly();
            }

        }

    }

在ejb-jar.xml中,我已将必需设置为跨属性。

1 个答案:

答案 0 :(得分:1)

我看到两种解决方法:

  1. 将您的trans-attribute设置为NOT_SUPPORTED。这样可以防止onMessage()在事务中运行。

  2. 使用不会自动在呼叫者的事务中注册其连接的连接工厂(即避免基于JCA的连接工厂)。