Krzysztof Szewczyk
Krzysztof Szewczyk

Reputation: 179

JMS transaction exception thrown in nontransaction session

Hello I have problem with my jms code when I try to send over 1000 messages to MDB. Following code:

@Stateless(mappedName = "RequestProcessingQueue")
public class RequestProcessingQueue {
private static final Logger logger = Logger.getLogger(RequestProcessingQueue.class);

@Resource(mappedName = "jmsRequestsFactory")
private ConnectionFactory connectionFactory;

@Resource(mappedName = "jmsRequestsDestination")
private Queue queue;

public void add(String participant, String password, List<Long> documents) throws JmsAppException {

    try {
        logger.debug("requests to process " + documents);
        Connection connecton = connectionFactory.createConnection();
        connecton.start();
        Session session = connecton.createSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueSender sender = (QueueSender) session.createProducer(queue);
        Message msg = msg = session.createMessage();
        msg.setStringProperty("participant", participant);
        msg.setStringProperty("password", password);

        for (Long id : documents) {
            msg.setLongProperty("request", id);
            sender.send(msg);
        }

        sender.close();
        session.close();
        connecton.close();
    } catch (JMSException e) {
        throw new JmsAppException(e);
    } catch (Throwable e) {
        throw new JmsAppException("Fatal error occured while sending  request to be processed", e);
    }
}

}

throws

 MQJMSRA_DS4001: JMSServiceException on send message:sendMessage: Sending message failed. Connection ID: 2979509408914231552 com.sun.messaging.jms.ra.DirectSession._sendMessage(DirectSession.java:1844) / sendMessage: Sending message failed. Connection ID: 2979509408914231552 com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.sendMessage(IMQDirectService.java:1955) / transaction failed: [B4303]: The maximum number of messages [1 000] that the producer can process in a single transaction (TID=2979509408914244096) has been exceeded. Please either limit the # of messages per transaction or increase the imq.transaction.producer.maxNumMsgs property. com.sun.messaging.jmq.jmsserver.data.handlers.DataHandler.routeMessage(DataHandler.java:467)'}
    at jms.example.RequestProcessingQueue.add(RequestProcessingQueue.java:48)

I do not understand why cus when I create session I pass false as first param indicating that session is non transactional mode.

Upvotes: 4

Views: 386

Answers (1)

Steve C
Steve C

Reputation: 19435

Your code does not work because the basic JMS API was designed to work in any environment, not just from within an EJB container. Runtime environment programming restrictions and behaviour are described in the EJB specifications and JavaDoc, in particular javax.jms.Connection.createSession(boolean transacted, int acknowledgeMode).

Your code can be simplified (assuming you're using at least Java 7) to:

@TransactionAttribute(TransactionAttributeType.NOTSUPPORTED)
public void add(String participant, String password, List<Long> documents) throws OgnivoException {

    try (Connection connection = connectionFactory.createConnection();
         Session session = connection.createSession();
         // session.start() not required
         MessageProducer sender = session.createProducer(queue)) {
        logger.debug("requests to process " + documents);

        for (Long id : documents) {
            Message msg = msg = session.createMessage();
            msg.setStringProperty("participant", participant);
            msg.setStringProperty("password", password);
            msg.setLongProperty("request", id);
            sender.send(msg);
        }

    } catch (JMSException e) {
        throw new JmsAppException(e);
    }
    // Don't catch throwable because it hides bugs
}

Remember that EJB methods are automatically associated with a transaction unless you specify otherwise. Additionally, be sure to check the javadoc for javax.jms.Connection.createSession() and associated methods, particularly the sections describing behaviour in different runtime environments.

Upvotes: 2

Related Questions