users@glassfish.java.net

Re: JMS transactionality troubles in session bean (x-post ...

From: <forums_at_java.net>
Date: Mon, 18 Nov 2013 01:07:00 -0600 (CST)

> I run your program with adding connection.start() against my GF3.1.2.2
installation, and it works fine. that is excellent news! Thanks for helping
out! here's my full class. ---------------------------- import
java.util.logging.Level; import java.util.logging.Logger; import
javax.annotation.PostConstruct; import javax.annotation.Resource; import
javax.ejb.Schedule; import javax.ejb.Singleton; import javax.ejb.Startup;
import javax.ejb.Timeout; import javax.ejb.TransactionManagement; import
javax.ejb.TransactionManagementType; import javax.jms.Connection; import
javax.jms.ConnectionFactory; import javax.jms.JMSException; import
javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.Queue;
import javax.jms.Session; import javax.transaction.UserTransaction;
@Singleton @Startup @TransactionManagement(TransactionManagementType.BEAN)
public class MessageConsumerTest { private static final Logger log =
Logger.getLogger(MessageConsumerTest.class.getName()); @Resource(mappedName =
"jms/TestQueue") private Queue jmsQueue; @Resource private UserTransaction
utx; @Resource(mappedName = "jms/TestBroker") private ConnectionFactory
connectionFactory; private Connection connection; @PostConstruct void init()
throws JMSException { connection = connectionFactory.createConnection();
connection.start(); } @Timeout @Schedule(hour="*", minute="*", second="0")
public void getMessages() { Session session = null; MessageConsumer consumer
= null; boolean rollback = false; int processedThisBatch = 0; try { int
commitAfter = 100; utx.begin(); session = connection.createSession(true, 0);
consumer = session.createConsumer(jmsQueue); rollback = true; while
(processedThisBatch < commitAfter) { Message msg = consumer.receive(200); if
(msg == null) break; // do stuff with msg log.info("Message " +
msg.getJMSMessageID() + " processed"); processedThisBatch++; if
(processedThisBatch % 10 == 0) throw new Exception("Bork!"); }
consumer.close(); session.close(); utx.commit(); log.info("Transaction
committed"); rollback = false; } catch (Exception e) { log.log(Level.SEVERE,
"Exception caught processing test queue", e); } finally { try { if (rollback)
{ utx.rollback(); log.info("Transaction rolled back"); } session.close(); }
catch (Exception e2) { log.log(Level.SEVERE, "Failed to rollback tx", e2); }
} } }

--
[Message sent by forum member 'davidwheeler']
View Post: http://forums.java.net/node/900132