JBoss 7 Embedded ActiveMQ - MDB message listener not working

半世苍凉 提交于 2020-12-14 23:38:43

问题


I have to upgrade an existing app with JBOSS 4.2.2 and embedded activeMQ 5.3.0

To try with jboss 7.3 using an embedded active MQ, i did the following.

Following the instructions at https://developer.jboss.org/docs/DOC-18798#jive_content_id_ActiveMQ_as_an_internal_messaging_broker

  1. I configured the activemq-rar-5.6.0.rar in JBoss 7.3 resource-adapter
  2. Deployed the jboss quick start hello-world-mdb war file in jboss.
  3. Tried to send and consume messages using a message driven bean(MDB)

The problem I am facing is, I don't see the Messages being consumed by the message listener.

Below are my configurations

1.resource-adapters subsystem changes in standalone-full.xml file

<subsystem xmlns="urn:jboss:domain:resource-adapters:5.0">
            <resource-adapters>
                <resource-adapter id="activemq-rar-5.6.0.rar">
                    <archive>
                        activemq-rar-5.6.0.rar
                    </archive>
                    <transaction-support>XATransaction</transaction-support>
                    <!--<config-property name="ServerUrl">tcp://localhost:61616</config-property> --> 
                    <config-property name="ServerUrl">vm://localhost</config-property> 
                    <connection-definitions>
                        <connection-definition class-name="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" jndi-name="java:/activemq/ConnectionFactory" enabled="true" use-java-context="true" pool-name="ActiveMQConnectionFactoryPool" use-ccm="true">
                            <xa-pool>
                                <min-pool-size>1</min-pool-size>
                                <max-pool-size>20</max-pool-size>
                            </xa-pool>
                        </connection-definition>
                    </connection-definitions>
                    <admin-objects>
                        <admin-object class-name="org.apache.activemq.command.ActiveMQQueue" jndi-name="java:/queue/HELLOWORLDMDBQueue" use-java-context="true" pool-name="HELLOWORLDMDBQueue">
                            <config-property name="PhysicalName">HELLOWORLDMDBQueue</config-property>
                        </admin-object>
                        <admin-object class-name="org.apache.activemq.command.ActiveMQTopic" jndi-name="java:/topic/HELLOWORLDMDBTopic" use-java-context="true" pool-name="HELLOWORLDMDBTopic">
                            <config-property name="PhysicalName">HELLOWORLDMDBTopic</config-property>
                        </admin-object>
                    </admin-objects>
                </resource-adapter> 
            </resource-adapters>
        </subsystem>
  1. standalone-full.xml domain:ejb3 subsystem mdb changes
<mdb>       
    <resource-adapter-ref resource-adapter-name="activemq-rar-5.6.0.rar"/>
    <bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
</mdb>

The helloworld-mdb.war consists the following two classes.

  1. Message sender
@WebServlet("/HelloWorldMDBServletClient")
public class HelloWorldMDBServletClient extends HttpServlet {

    private static final long serialVersionUID = -1949285948189796311L; 
    
    @Resource(mappedName = "java:/activemq/ConnectionFactory")
    private ConnectionFactory connectionFactory;
    @Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue")
    private Destination queue;
    private Connection connection;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Session session = null;
        MessageProducer sender = null;
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.write(
                "<h1>Quickstart: Example demonstrates the use of <strong>JMS 2.0</strong> and <strong>EJB 3.2 Message-Driven Bean</strong> in JBoss EAP.</h1>");
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            sender = session.createProducer(queue);
            sender.setDeliveryMode(DeliveryMode.PERSISTENT);

            out.write("<p>Sending messages to <em>" + queue + "</em></p>");
            out.write("<h2>The following messages will be sent to the destination:</h2>");
            for (int i = 0; i < 3; i++) {
                String text = "This is message " + (i + 1);
                TextMessage response = session.createTextMessage(text);
                sender.send(response);
                out.write("Message (" + i + "): " + text + "</br>");
            }
            out.write(
                    "<p><i>Go to your JBoss EAP server console or server log to see the result of messages processing.</i></p>");
        } catch (JMSException e) {          
            e.printStackTrace();
        }finally {
            try {
                if (sender != null) {
                   sender.close();
                }
                if (session != null) {
                       session.close();
                    }
             } catch (JMSException e) {             
                e.printStackTrace();
            } 
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}

  1. Message Consumer MDB
@MessageDriven(name = "HelloWorldQueueMDB", activationConfig = {        
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "HELLOWORLDMDBQueue"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")})
@ResourceAdapter(value="activemq-rar-5.6.0.rar")
public class HelloWorldQueueMDB implements MessageListener {

    private static final Logger LOGGER = Logger.getLogger(HelloWorldQueueMDB.class.toString());

    /**
     * @see MessageListener#onMessage(Message)
     */
    public void onMessage(Message rcvMessage) {
        TextMessage msg = null;
        try {
            if (rcvMessage instanceof TextMessage) {
                msg = (TextMessage) rcvMessage;
                LOGGER.info("Received Message from queue: " + msg.getText());
            } else {
                LOGGER.warning("Message of wrong type: " + rcvMessage.getClass().getName());
            }
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
    }
}

I send messages from the browser by going to http://localhost:8080/helloworld-mdb/HelloWorldMDBServletClient. But I am not seeing the messages being consumed by the message listener, they are not showing in the jboss log.

The JBoss console log looks like this


回答1:


In the HelloWorldMDBServletClient you're creating a transacted session to send the messages, i.e.:

session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

However, you're never calling session.commit() so it looks to me like the messages are never actually sent.

You should either invoke session.commit() to send the messages or create the session as non-transacted, e.g.:

session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);


来源:https://stackoverflow.com/questions/64960547/jboss-7-embedded-activemq-mdb-message-listener-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!