You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by Mathew Phillips <ma...@wartburg.edu> on 2010/08/25 00:35:20 UTC

ActiveMQ consume message even with error

I have an MDB that i developed to consume messages from a queue and write the data to a database. It works fine as long as no errors are thrown in the onMessage method of the mdb. But if an exception is thrown
the message is not consumed from the queue. Is there a way to programmatically consume the message even if an error occurs?

class MyMDB implements MessageListener{
        ....
public void onMessage(Message message) {
        TextMessage txtMsg = (TextMessage)message;
        try {
                Connection dbconn = ds.getConnection();
                        String[] data = txtMsg.getText().split(",");
                        PreparedStatement stmt = dbconn.prepareStatement(
                                      "INSERT INTO CUSTOMER_TBL values(?,?,?)");
                        stmt.setInt(1,Integer.parseInt(data[0]));
                        stmt.setString(2,data[1]);
                        stmt.setString(3,data[2]);
                        stmt.execute();
                        dbconn.close();
        } catch (Exception e) {
                        carmdb_logger.error("Error inserting to database",e);
        }
       ...
}

Thanks,
Matt Phillips

RE: ActiveMQ consume message even with error

Posted by Mathew Phillips <ma...@wartburg.edu>.
Thank you for the link. Basically what I gathered from it was that I should send it to some sort of error queue. But since its an MDB i would have to implement the ability to put a message onto a queue in order
to get rid of the message. Is there a simpler way to just remove the message from the queue without having to go into the admin console and purge it manually?
________________________________________
From: Timothy Bish [tabish121@gmail.com]
Sent: Tuesday, August 24, 2010 5:50 PM
To: users@activemq.apache.org
Subject: Re: ActiveMQ consume message even with error

On Tue, 2010-08-24 at 22:35 +0000, Mathew Phillips wrote:
> I have an MDB that i developed to consume messages from a queue and write the data to a database. It works fine as long as no errors are thrown in the onMessage method of the mdb. But if an exception is thrown
> the message is not consumed from the queue. Is there a way to programmatically consume the message even if an error occurs?
>
> class MyMDB implements MessageListener{
>         ....
> public void onMessage(Message message) {
>         TextMessage txtMsg = (TextMessage)message;
>         try {
>                 Connection dbconn = ds.getConnection();
>                         String[] data = txtMsg.getText().split(",");
>                         PreparedStatement stmt = dbconn.prepareStatement(
>                                       "INSERT INTO CUSTOMER_TBL values(?,?,?)");
>                         stmt.setInt(1,Integer.parseInt(data[0]));
>                         stmt.setString(2,data[1]);
>                         stmt.setString(3,data[2]);
>                         stmt.execute();
>                         dbconn.close();
>         } catch (Exception e) {
>                         carmdb_logger.error("Error inserting to database",e);
>         }
>        ...
> }
>
> Thanks,
> Matt Phillips

See this article for a discussion of why you should handle all errors in
the onMessage method and not propagate them to the JMS client code.

http://effectivemessaging.blogspot.com/2009/01/messagelistener-exception-handling.html

Regards


--
Tim Bish

Open Source Integration: http://fusesource.com
ActiveMQ in Action: http://www.manning.com/snyder/

Follow me on Twitter: http://twitter.com/tabish121
My Blog: http://timbish.blogspot.com/


Re: ActiveMQ consume message even with error

Posted by Timothy Bish <ta...@gmail.com>.
On Tue, 2010-08-24 at 22:35 +0000, Mathew Phillips wrote:
> I have an MDB that i developed to consume messages from a queue and write the data to a database. It works fine as long as no errors are thrown in the onMessage method of the mdb. But if an exception is thrown
> the message is not consumed from the queue. Is there a way to programmatically consume the message even if an error occurs?
> 
> class MyMDB implements MessageListener{
>         ....
> public void onMessage(Message message) {
>         TextMessage txtMsg = (TextMessage)message;
>         try {
>                 Connection dbconn = ds.getConnection();
>                         String[] data = txtMsg.getText().split(",");
>                         PreparedStatement stmt = dbconn.prepareStatement(
>                                       "INSERT INTO CUSTOMER_TBL values(?,?,?)");
>                         stmt.setInt(1,Integer.parseInt(data[0]));
>                         stmt.setString(2,data[1]);
>                         stmt.setString(3,data[2]);
>                         stmt.execute();
>                         dbconn.close();
>         } catch (Exception e) {
>                         carmdb_logger.error("Error inserting to database",e);
>         }
>        ...
> }
> 
> Thanks,
> Matt Phillips

See this article for a discussion of why you should handle all errors in
the onMessage method and not propagate them to the JMS client code.

http://effectivemessaging.blogspot.com/2009/01/messagelistener-exception-handling.html

Regards


-- 
Tim Bish

Open Source Integration: http://fusesource.com
ActiveMQ in Action: http://www.manning.com/snyder/

Follow me on Twitter: http://twitter.com/tabish121
My Blog: http://timbish.blogspot.com/