2016-10-28 54 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謝謝你! –