English 中文(简体)
当JMS Prod在POJO类助手中生活时,我怎么能把JMS的一位生产者纳入交易中。
原标题:How can I include a JMS Producer in a Transaction when the JMS Prod lives in a helper POJO class

短期问题: 是否有办法迫使一个无国籍的EJB呼吁的POJO生活在EJB的背景下,以便交易和资源注入能够在POJO工作?

具体地说,在我试图做些什么的情况下:我如何把一个POJO JMS生产商列入一个EJB的交易,该交易在要求POJO发出电文之前将一些数据留在一个数据库中,这样,如果信息可能由于一个例外而发出,数据库交易也会回头? 我想仓促地寄送邮件。

这是一条幸福的道路(从无国籍会议开始):

  • save data to database // this works
  • pull select data from the data that was persisted and place it in a custom message class (really a dto)
  • call the sendEmail method of the EmailQueueMessenger POJO passing it the message object.
  • message is sent to the MDB to process and send the email (not part of the question, just here for completeness)

下面的法典是行之有效的,如果我错说情况调查,它就只赢得了打字班的“老板”数据库。 BTW,我可以拿到@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 我在为电子邮件QueueMessenger使用一个单独的类别,因为其他部分需要寄送偶尔的电子邮件,因此不想重复编码。


值得一提的是,我进行了一项试验,即我把所有联医处迁到首批EJB中,而且情况正确......但我需要以单独一类工作,供其他部分使用。

最佳回答

我认为你有两个问题:

  1. 你们需要使你们的诗歌成为常设理事会。 它应当注入你们的神职人员,而不是直接叫来,以便你处理代议。 仍然可以重新使用这一说明,因为如果不部署在集装箱内,说明将被忽视。

  2. 你们正在利用非洲木材组织(ACKNOWLEDGE)设立一个小型会议,但需要加以改变。 此外,确保jms的连接来自JCA的交易来源,因为这将使会议与交易联系起来。

========= Update =========

法案;

抱歉,我认为外在的星座出于某种原因,是JMS听众。 无论如何,问题都是一样的。

如果您希望Email QueueMessenger按照您在其中的附加说明(交易、注射等)行事,你必须将其称为EJB,而不是简单的波纹。 因此,您的外在会议应当如此:

@EJB   // key difference
private EmailQueueMessenger eqm;

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void sendMessage(Object messageObject) {
   eqm.sendEmail(messageObject);
}

@Resource(name = "jms/EmailerQueueConnectionFactory")
@Resource(name = "jms/EmailerQueue")

以及

@TransactionAttribute(TransactionAttributeType.MANDATORY)
@Stateless

Lastly, your JMS sender will be enrolled in a transaction at the point of invocation 以及you need to make sure that the transaction manager is aware that you are enlisting a second resource manager in the transaction (first the DB , 以及now JMS). I am not that familiar with glassfish, but it seems there is a configuration screen with a switch that allows you to specify the level of transactional support for a connection factory.

我将把招标法改为:

Session session = con.createSession(true, Session.SESSION_TRANSACTED);

从技术上讲,你可以在电子邮件QueueMessenger一案中追捕JMS的联系人。 你的代码不应因为交易完成时将处理此事而关闭联审办会议(尽管我已经看到联审办/联审办在此问题上的执行存在分歧)。

I hope that clears it up, 以及I really hope it works !

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签