2 Replies Latest reply on Aug 20, 2002 2:41 PM by Andreas Schaefer

    sending JMS messages from EJB as part of same tx

    Dmitri Colebatch Newbie

      (post from mailing list)

      hey all,

      I'm sure this is a simple question, so am hoping that someone can point out the error of my ways. I'm running JBoss 3.0.1, win2k, sun 1.3.1, postgres 7.2

      My problem is basically that I'm trying to send JMS messages from within an entity bean, but the messages either (a) aren't being sent, or (b) are sent (and received) as I send them instead of when the tx commits.

      Here's what I'm doing.

      in my ejb-jar.xml file I have this:

      <resource-ref>
      <res-ref-name>jms/publish</res-ref-name>
      <res-type>javax.jms.Queue</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>
      <resource-ref>
      <res-ref-name>jms/ConnectionFactory</res-ref-name>
      <res-type>javax.jms.QueueConnectionFactory</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>

      (inside the relevant entity bean declaration)

      in jboss.xml I have this:

      <resource-ref>
      <res-ref-name>jms/publish</res-ref-name>
      <resource-name>jms/publish</resource-name>
      </resource-ref>
      <resource-ref>
      <res-ref-name>jms/ConnectionFactory</res-ref-name>
      <resource-name>jms/ConnectionFactory</resource-name>
      </resource-ref>

      (inside the relevant entity bean declaration), and then this:

      <resource-manager>
      <res-name>jms/publish</res-name>
      <res-jndi-name>queue/publish</res-jndi-name>
      </resource-manager>
      <resource-manager>
      <res-name>jms/ConnectionFactory</res-name>
      <res-jndi-name>ConnectionFactory</res-jndi-name>
      </resource-manager>

      (in the resource managers section.

      The code I am using is this:

      commandSender = new CommandSender((QueueConnectionFactory)
      ctx.lookup("java:comp/env/jms/ConnectionFactory"),
      (Queue)
      ctx.lookup("java:comp/env/jms/publish"));

      (called from inside setEntityContext), command sender looks like this:

      public class CommandSender
      {
      private static final Category cat =
      Category.getInstance(CommandSender.class);

      private QueueConnection qConn;
      private Queue publishQueue;

      public CommandSender(QueueConnectionFactory qConnFactory, Queue
      publishQueue)
      throws JMSException
      {
      this.qConn = (QueueConnection) qConnFactory.createQueueConnection();
      this.publishQueue = publishQueue;
      }

      public void send(Command command) throws JMSException
      {
      QueueSession qSession = null;
      try
      {
      qSession = (QueueSession) qConn.createQueueSession(true,
      Session.AUTO_ACKNOWLEDGE);
      QueueSender qSender = qSession.createSender(publishQueue);
      ObjectMessage msg = qSession.createObjectMessage(command);
      qSender.send(msg);
      cat.debug("Sent message: " + msg);
      }
      finally
      {
      if (qSession != null) qSession.close();
      }
      }
      }


      I've tried various parameter combinations for the creating of the queue
      session, but with no luck....

      can someone point out what I'm not doing here please?

      thanks
      dim