JBoss 7 嵌入式 ActiveMQ - MDB 消息侦听器不起作用 [英] JBoss 7 Embedded ActiveMQ - MDB message listener not working

查看:27
本文介绍了JBoss 7 嵌入式 ActiveMQ - MDB 消息侦听器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 JBOSS 4.2.2 和嵌入式 activeMQ 5.3.0 升级现有应用

为了使用嵌入式主动 MQ 尝试使用 jboss 7.3,我执行了以下操作.

按照

解决方案

HelloWorldMDBServletClient 中,您正在创建一个 transacted 会话来发送消息,即:

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

但是,您从不调用 session.commit(),因此在我看来,消息从未真正发送过.

您应该调用 session.commit() 来发送消息或创建非事务性会话,例如:

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

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

解决方案

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);

这篇关于JBoss 7 嵌入式 ActiveMQ - MDB 消息侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆