你好,当我尝试向MDB发送超过1000条消息时,我的jms代码有问题.以下代码:
@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 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);
}
}
}
投
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)
我不明白为什么当我创建会话时我传递false作为第一个参数,表明会话是非事务模式.
最佳答案 您的代码不起作用,因为基本JMS API旨在在任何环境中工作,而不仅仅是在EJB容器中. EJB规范和JavaDoc中描述了运行时环境编程限制和行为,特别是
javax.jms.Connection.createSession(boolean transacted, int acknowledgeMode).
您的代码可以简化(假设您至少使用Java 7):
@TransactionAttribute(TransactionAttributeType.NOTSUPPORTED)
public void add(String participant, String password, List 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
}
请记住,除非另行指定,否则EJB方法会自动与事务关联.此外,请务必检查javadoc for javax.jms.Connection.createSession()及相关方法,尤其是描述不同运行时环境中的行为的部分.