JMS消息消耗不会发生在bean之外

时间:2011-12-09 21:54:45

标签: glassfish queue jms

我正在运行Glassfish Web流程,我需要一个非容器托管类(EJBUserManager)才能从MessageDrivenBean接收消息。该类有javax.jms.Queues和连接工厂,我可以写入队列。队列发送到正确接收代码的MessageDrivenBeanAccountValidatorBean),然后回写消息。但是EJBUserManager尝试从队列中读取并且从不接收消息。

@Override
public boolean doesExist(String username) throws FtpException {
    LOGGER.finer(String.format("Query if username %s exists", username));

    QueueConnection queueConnection = null;
    boolean doesExist = false;

    try {
        queueConnection = connectionFactory.createQueueConnection();
        final UserManagerMessage userManagerMessage = 
            new UserManagerMessage(UserManagerQueryCommands.VALIDATE_USER, username);
        final Session session = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final ObjectMessage objectMessage = session.createObjectMessage(userManagerMessage);
        session.createProducer(accountValidatorQueue).send(objectMessage);
        session.close();
        queueConnection.close();
        queueConnection = connectionFactory.createQueueConnection();
        final QueueSession queueSession = 
            queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

        LOGGER.finest(String.format("Right before doesExist receive for username %s", username));
        final Message firstAttemptMessage = queueSession.createConsumer(userManagerQueue).receive(3000);
        final Message message = firstAttemptMessage != null ? 
                firstAttemptMessage : queueSession.createConsumer(userManagerQueue).receiveNoWait();
        LOGGER.finest(String.format("Right after doesExist receive for username %s", username));
        LOGGER.finest(String.format("Is the message null: %b", message != null));
        if (message != null && message instanceof StreamMessage) {

            final StreamMessage streamMessage = (StreamMessage) message;
            doesExist = streamMessage.readBoolean();
        }

    } catch (JMSException e) {
        e.printStackTrace();
    } finally {
        if (queueConnection != null) {
            try {
                queueConnection.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }

    return doesExist;
}

以上是EJBUserManager的代码。现在,它可以发送到accountValidatorQueue。它永远不会从userManagerQueue

收到

以下是AccountValidatorBean

的代码
private void validateUser(final String username) {
    QueueConnection queueConnection = null;
    final String doctype = doctypeLookupDAO.getDocumentTypeForUsername(username);
    LOGGER.finest(String.format("Doctype %s for username %s", doctype, username));


    try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        final Session session = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);


        //final StreamMessage message = session.createStreamMessage();
        //message.clearBody();
        //message.writeBoolean(doctype != null);
        //message.reset();
        final ObjectMessage message = session.createObjectMessage(Boolean.valueOf(doctype != null));

        final MessageProducer messageProducer = 
            session.createProducer(userManagerQueue);
        LOGGER.finest(String.format("Queue name %s of producing queue", userManagerQueue.getQueueName()));
        messageProducer.send(message);

        LOGGER.finest(String.format("Sending user validate message for user %s", username));
        messageProducer.close();
        session.close();
    } catch (JMSException e) {
        e.printStackTrace();

    } finally {
        if (queueConnection != null) {
            try {
                queueConnection.close();
            } catch (JMSException e1) {
                e1.printStackTrace();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

固定。我需要调用QueueConnection.start()来使用队列中的消息。