Clément
Clément

Reputation: 3

EJB 3 + JMS and transaction

I'm trying to use JMS (activemq v 5.10) inside an EBJ, managed by tomee+ (v 1.7) container. I also would like to use JMS Transaction, and that's where i have troubles... This is my EJB :

@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) {
            ...
        }
    }

This EJB cause an illegalStateException when "commit" is called :

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)
    ...

How can i use JMS "transacted session" in my EJB?

What's strange is that JMS message is send and can be consumed...

Thanks in advance

Clément

Upvotes: 0

Views: 1178

Answers (1)

Clément
Clément

Reputation: 3

To solve this issue i had to convert EJB to BMT, my EJB looks like this now :

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)

public class EJBJmsSender implements JmsSender, ExceptionListener {

you also need to manage transaction. To do so, you have to add a property in EJB :

@Resource
private UserTransaction userTransaction;

Then, you need to begin/stop transaction as you need :

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

For more details see : http://tomee-openejb.979440.n4.nabble.com/Activemq-embedded-createSession-unable-to-make-it-work-td4672172.html

Upvotes: 0

Related Questions