Romain Manni-Bucau wrote
> commit/rollbackis handled with transaction status so don'tcall it yourself
My goal is to be able to call a rollback when a fonctionnal error arrives.
So i have to manage transaction.
Finally, i found what i was looking for (thanks for your help!), only need
to manage transaction correctly now!
I had to use @Asynchronous method and BMT (to be able to manage
transaction).
This is the result :
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class MyAsyncReceiver {
...
@Resource
private UserTransaction userTransaction;
/**
* receive message.
*/
@Asynchronous
@Lock(LockType.READ)
@AccessTimeout(-1)
public void receiveMessage() {
QueueConnectionFactory connectionFactory = null;
QueueConnection connection = null;
QueueSession session = null;
Queue orderQueue = null;
MessageConsumer msgConsumer = null;
Message inMessage = null;
// receive message
while (true) {
try {
userTransaction.begin();
try {
Context ctx = new InitialContext();
connectionFactory =
(QueueConnectionFactory) ctx
.lookup("MyJNDIFactoryName");
connection =
connectionFactory.createQueueConnection();
session =
connection.createQueueSession(true, 0);
orderQueue = (Queue)
ctx.lookup("MyJNDIQueueName";
connection.start();
// Create receiver
msgConsumer =
session.createConsumer(orderQueue);
} catch (Exception e) {
LOGGER.error("Connection problem: " +
e.toString());
userTransaction.rollback();
continue;
}
// receive message
String text = null;
try {
inMessage = msgConsumer.receive();
TextMessage textMessage = (TextMessage)
inMessage;
text = textMessage.getText();
} catch (JMSException e) {
userTransaction.rollback();
continue;
}
// Call a function witch can throw an exception
try {
myFunction(text);
// validate message if no exception
occured
userTransaction.commit();
} catch (InvalidParameterException e) {
LOGGER.error("Functionnal error", e);
// unvalidate message to be able to
consume msg again
userTransaction.rollback();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
LOGGER.error("Error
when closing connections", e);
}
}
}
} catch (Exception e) {
LOGGER.error(
"Transaction problem : Begin,
Commit or Rollback error",
e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
LOGGER.error("Error when
closing connections", e);
}
}
}
}
}
...
}
Now, i need to Manage transaction correctly, and i don't think my method is
a good one because :
- What about transaction timeout? I start transaction before calling
"receive" (wait for a message), if no message arrive and transaction timeout
occured : how to manage this case?
Thanks in advance
--
View this message in context:
http://tomee-openejb.979440.n4.nabble.com/Activemq-embedded-createSession-unable-to-make-it-work-tp4672172p4672203.html
Sent from the TomEE Users mailing list archive at Nabble.com.