2014-10-06 93 views
0

我試圖在EBJ內部使用JMS(activemq v 5.10),由tomee +(v 1.7)容器管理。 我也想使用JMS事務,而這也正是我有麻煩...... 這是我的EJB:EJB 3 + JMS和事務

@Singleton 
@Remote(value = JmsSender.class) 
public class EJBJmsSender implements JmsSender, ExceptionListener { 

@Resource 
private ConnectionFactory connectionFactory; 

private QueueConnection connection; 

public void sendMessage(String Queue, String contenuMessage) 
     throws ExceptionTechniqueApiJms { 

    InitialContext ctx = null; 
    Queue queueduService = null; 
    Connection queueConn = null; 
    try { 
     // get the initial context 
     ctx = new InitialContext(); 
     // lookup the queue object 
     queueduService = (Queue) ctx.lookup(Queue); 
    } catch (NamingException e) { 
     ... 
    } 

    Session queueSession = null; 
    try { 
     // create a queue connections 
     queueConn = connectionFactory.createConnection(); 
     queueConn.start(); 
     queueConn.setExceptionListener(this); 

     // create a transacted queue session 
     queueSession = queueConn.createSession(true,0); 

     // create a queue sender 
     MessageProducer queueSender = queueSession 
       .createProducer(queueduService); 
     queueSender.setDeliveryMode(DeliveryMode.PERSISTENT); 

     TextMessage message = queueSession 
       .createTextMessage(contenuMessage); 

     queueSender.send(message); 

     queueSession.commit(); 
    } catch (Exception e) { 
     ... 
    } finally { 

     try { 
      if (queueConn != null) { 
       queueConn.close(); 
      } 
     } catch (JMSException e) { 
      ... 
     } 
    } 

這個EJB原因一個IllegalStateException當「提交」被稱爲:

javax.jms.IllegalStateException: Not a transacted session 
    at org.apache.activemq.ActiveMQSession.commit(ActiveMQSession.java:569) 
    at org.apache.activemq.ra.ManagedSessionProxy.commit(ManagedSessionProxy.java:108) 
    ... 

如何在我的EJB中使用JMS「事務會話」?

什麼奇怪的是,JMS消息被提前發和可以食用...

感謝

克萊門特

+0

你可以在tomee論壇找到答案:http://tomee-openejb.979440.n4.nabble.com/Activemq-embedded-createSession-unable-to-make-it-work-td4672172.html – 2014-11-25 09:05:20

回答

0

爲了解決這個問題,我不得不EJB轉換爲BMT,我的長相EJB現在這個樣子:

@Stateless 
@TransactionManagement(TransactionManagementType.BEAN) 

公共類EJBJmsSender實現JmsSender,{的ExceptionListener

您還需要管理交易。要做到這一點,你必須在EJB添加屬性:

@Resource 
private UserTransaction userTransaction; 

然後,你需要開始/停止交易,因爲你需要:

public void sendMessage(String Queue, String contenuMessage) 
    throws ExceptionTechniqueApiJms { 
try { 
     userTransaction.begin(); 
     (...) 
     userTransaction.commit(); 
    } catch (Exception e) { 
     userTransaction.rollback(); 
    } 
} 

有關詳細信息,請參閱:http://tomee-openejb.979440.n4.nabble.com/Activemq-embedded-createSession-unable-to-make-it-work-td4672172.html