JBoss AS7 MessageDrivenBean Example

getting JMS running wasn’t as easy as I thought, but after some research I found out how to get as7 with included hornetq up and running. First copy the messaging part from standalone-preview.xml to standalone.xml. Then look for ejb3 configuration and set lite=”false” instead of true (default) as in light mode MDBs are not supported and don’t consume messages. Also copy messaging and messaging-throuput socket-bindings from the socket-binding-group part of the standalone-preview.xml.

Here you have a simple sender that can be invoced from a jsf-page and a consumer for the messages:

package at.coffeebeans.shop.controller;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.jboss.solder.logging.Logger;

/**
 * @author manuel
 *
 */
@Stateless
@Named
public class TestSender {

    // @Resource(lookup = "java:/ConnectionFactory")
    @Resource(lookup = "java:/JmsXA")
    private ConnectionFactory connectionFactory;

    @Resource(lookup = "java:/queue/test")
    private Queue testQueue;

    @Inject
    private Logger log;

    public String sendMessage() {
        Connection connection = null;
        Session session = null;
        try {
            log.debug("sending message");

            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            final MessageProducer publisher = session.createProducer(testQueue);

            final TextMessage message = session.createTextMessage("Hello");
            publisher.send(message);

            log.debug("message sent");
        } catch (final JMSException exc) {
            log.error(exc);
        } finally {
            try {
                if (null != session) {
                    session.close();
                }
            } catch (JMSException exc) {
                log.warn(exc);
            }
            try {
                if (null != connection) {
                    connection.close();
                }
            } catch (JMSException exc) {
                log.warn(exc);
            }
        }
        return "test";
    }
}
package at.coffeebeans.shop.mdb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Inject;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.jboss.solder.logging.Logger;

/**
 * @author manuel
 */
@MessageDriven(name = "TestMessageConsumer", activationConfig = {
  @ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/queue/test"),
  @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
  @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "AUTO_ACKNOWLEDGE") })
public class TestMessageConsumer implements MessageListener {

	@Inject
	private Logger log;

	@Override
	public void onMessage(final Message msg) {
		if (msg instanceof TextMessage) {
			final TextMessage tmp = (TextMessage) msg;
			try {
				log.debugf("received textmessage: %s", tmp.getText());
			} catch (JMSException exc) {
				log.error(exc);
			}
		} else {
			log.warn("got unknown messagetype");
		}
	}

}

 

 

Leave a Comment