Re: Message-Driven Bean OnMesssage() etc

Johan Eltes <[email protected]> Thu, 29 Dec 2005 09:29:03 +0100
Newsgroups gmane.comp.java.sun.ejb.general
Message-ID <[email protected]>
You are not allowed to create threads in an EJB.
One of the reasons is that transaction contexts are bound to threads.
If you create a thread (most app servers will not let you), that the
app server will not be able to manage it.

Would it be possible to have multiple MDBs (MDB pool size > 1)
instead of forking threads within your business logic?

/Johan

29 dec 2005 kl. 06.00 skrev Jingwen Jin:

> I'm writing a pub/sub server that, on receiving an incoming message,
> stores it into a messageArray, and then creates a thread to forward
> messages in the array. The following code works ok if the
> "forwardMessage"
> method is called within onMessage(); messages get delivered.
>
> But whenever I try to deliver the messages in messageArray using a
> thread
> as shown below, I get the following exception and messages don't get
> delivered:
>
> ReplyMsgBean.forwardMessage: JMSException:
> com.sun.messaging.jms.JMSException: [SEND_REPLY(9)] [C4036]: A server
> error occurred. : transaction failed: Unexpected Broker Exception:
> [received message with Unknown Transaction ID -1: ignoring message]
>
> It looks like the server could not create the connection within the
> forwardMessage method.
>
> I'd appreciate any suggestions.
>
> Thanks,
>
> Jingwen
>
> ---------------------------------
>
>
>
> public class ReplyMsgBean implements MessageDrivenBean,
> MessageListener {
> ...
>     public ReplyMsgBean() throws Exception {
>  messageArray = new ArrayList();
>  mft = new MessageForwardingThread();
>  mft.start();
>     }
>     public void ejbCreate() {
>         try {
>             Context initial = new InitialContext();
>             cf = (ConnectionFactory) initial.lookup(
>                     "java:comp/env/jms/MyConnectionFactory");
>         } catch (Exception ex) {
>             logger.severe("ReplyMsgBean.ejbCreate: Exception: " +
>                 ex.toString());
>         }
>     }
>     public void onMessage(Message inMessage) {
>        put the message into messageArray
>     }
>     private static void forwardMessage(TextMessage msg) {
>          ...
>          Connection con = cf.createConnection();
>          Session ses = con.createSession(true, 0);
>          producer = ses.createProducer(topic);
>  producer.send(msg);
>          con.close();
>     }
>
>
>    final class MessageForwardingThread extends Thread {
>          ...
>  public final void run() {
>              for every-message (msg) in messageArray
>   ReplyMsgBean.forwardMessage(msg);
>
>  }
>
>    }
>
> ======================================================================
> =====
> To unsubscribe, send email to [email protected] and include in
> the body
> of the message "signoff EJB-INTEREST".  For general help, send
> email to
> [email protected] and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[email protected] and include in the body of the message "help".