简短的问题:有没有办法强制通过一个无状态的EJB叫住在EJB的背景下,这样的交易和资源注入将在POJO工作POJO?
Specfically在我所试图做的背景:我怎么能包括在调用POJO之前坚持在数据库中的一些数据的EJB的交易一个POJO JMS生产者发送消息,使得如果该消息不能由于被发送到一个例外,该数据库事务将回滚呢? 我想异步发送邮件。
这是(无状态会话bean内开始)的快乐路径:
- 数据保存到数据库//这工作
- 从已持续数据拉选择数据,并将其放置在一个定制的“消息”类(一个真正的DTO)
- 调用EmailQueueMessenger POJO的sendEmail方法传递给它的消息对象。
- 消息被发送到MDB处理和发送电子邮件(对于完整性不是问题的一部分,只是在这里)
以下作品的代码,它只是不会回滚数据库“坚持”在调用的类,如果我强迫中说,上下文查找错误。 顺便说一句,我不能让@Resource注入要么工作。
//In the EJB
EmailQueueMessenger eqm = new EmailQueueMessenger();
eqm.sendEmail(messageObject);
// mailObject will be translated into an email message at the other end of the queue.
/******************** POJO Below ************/
public class EmailQueueMessenger implements Serializable {
// Resource injection doesn't work... using 'lookup' below, which does work.
// @Resource(name = "jms/EmailerQueueConnectionFactory")
// private ConnectionFactory connectionFactory;
// @Resource(name = "jms/EmailerQueue")
// private Destination EmailerQueue;
public EmailQueueMessenger() {
}
public void sendEmail(MailMessageDTO theMessage) {
Context ctx = null;
try {
ctx = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("jms/EmailerQueueConnectionFactory");
System.out.println("JMS Producer CTX Name In Namespace: " + ctx.getNameInNamespace());
//Destination EmailerQueue = (Destination) ctx.lookup("jms/ERROR"); // forces exception
Destination EmailerQueue = (Destination) ctx.lookup("jms/EmailerQueue"); // normal working code
try {
Connection con = connectionFactory.createConnection();
Session session = con.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer msgProd = session.createProducer(EmailerQueue);
...
我曾尝试加入:
@TransactionAttribute(TransactionAttributeType.MANDATORY)
@Stateless
到POJO定义,但它不会有所作为。
FWIW我使用了EmailQueueMessenger单独的一类,因为会有,将需要发送的不定期电子邮件应用程序的其他部分,所以不想重复的代码。
应该指出,我做了一个测试,让我感动了所有JMS东西第一个EJB中,它正确地跑......但我需要这在一个单独的类的应用程序中的其他部分使用。