If you have many MDB instances (e.g. threaded MDBs) simultaneously
pulling messages off the queue/topic, you could let each mdb instance
forward the message without creating a new thread. You wouldn't
usually need to do asynchronous *wihin* an MDB, since the MDB is
asynchronous itself. What exactly is it you need to achieve by
threading, in addition to what you get by having an MDB?
/Johan
1 jan 2006 kl. 01.06 skrev Jingwen Jin:
Hi, Thanks a lot for the suggestion. The fact that EJB does not
support
multi-threading surprised me. As I said, I wanted my MDB to store
incoming
messages in a data structure, and then have threads working on the
sending of
messages. How could such a task be achieved by using multiple
beans? More
details would be very helpful.
When google-ing, I found somebody commenting that multi-threading
would work
when separating the "multi threadedness" into a separate layer
outside of the
EJB layer. In such a case, a java job scheduler is needed to handle
the
scheduling. Have you or has anybody on the list used any scheduler
(such as the
Quartz scheduler)?
Thank you,
Jingwen
---- Original message ----
Date: Thu, 29 Dec 2005 09:29:03 +0100
From: Johan Eltes <[EMAIL PROTECTED]>
Subject: Re: Message-Driven Bean OnMesssage() etc
To: EJB-INTEREST@JAVA.SUN.COM
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".
======================================================================
=====
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".