2016-10-28 53 views
4

您好,我尝试向MDB发送超过1000条消息时遇到了我的jms代码问题。下面的代码:在非事务会话中引发的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<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); 
    } 
} 

}

抛出

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) 

我不明白为什么CUS当我创建会话我通过假作为第一个参数,指示会话是非事务模式。

回答

2

您的代码不起作用,因为基本的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<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 
} 

记住,除非另行指定EJB方法会自动与交易相关联。此外,请确保检查javadoc的javax.jms.Connection.createSession()及相关方法,特别是描述不同运行时环境中的行为的部分。

+0

@Stave C谢谢你! –