问题
I have a problem with message driven EJB. I have too applications Web Service and EJB application which contains MessageDrivenBean.
To send message to JMS I'm using ObjectMessage: Here is my code:
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, 1);
MessageProducer messageProducer = session.createProducer(queue);
ObjectMessage outMessage = session.createObjectMessage();
outMessage.setObject(((Serializable) operation));
LOGGER.debug("Sending message...");
messageProducer.send(outMessage);
LOGGER.debug("Sending message: done.");
messageProducer.close();
session.close();
connection.close();
When I call my web service I am calling this method as well. The message arives at MDB and starts to process. Here is my MDB code:
@MessageDriven(mappedName = "jms/cbsDestination", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class OperationsBackgroundService implements MessageListener {
//Some code....
public void onMessage(Message message) {
LOGGER.debug("Got message: " + message.toString());
if (message instanceof ObjectMessage) {
ObjectMessage objectMessage = (ObjectMessage) message;
Operation operation = null;
}
Its all OK, I get the message, it starts to process and it ends as I expect.
But the problem is: When I send first message to MDB it starts process it (OK), then, when first message is processing I send second message to my MDB, and it starts processes it as well. Ass I know the JMS is characterized by that if I send one message and that one is processing, other messages waits until the first is processed. Or am I missing something here? Please help. Maybe there is some properties I forgot to set?
Thanks id advance.
回答1:
Your application server created more than one instance of OperationsBackgroundService
and registered each instance as a consumer. Each consumer can only process one message at a time, but when there are, say, 2 consumers, 2 messages can be processed concurrently. This is a feature, not a bug.
If you want to achieve single-threaded processing, simply tell your application server to create only one consumer per MDB. Consult your application server documentation to see how this can be configured.
来源:https://stackoverflow.com/questions/9152654/jms-and-messagedriven-ejb-bean