3

I want to send a large string message (3-5 MB) with jmstemplate (with activemq jms broker). In the middle of sending the message, i shutdowned my broker; Why i can't catch throwed exception in catch(Exception e)? What should i do to handle this exception?

public class MessageSenderServiceImpl implements MessageSenderService
{
    private JmsTemplate jmsTemplate;

    @Override
    @Transactional
    public void sendMessage(final String messageContent, final String destination) throws SendingMessageException
    {
        try
        {
            jmsTemplate.send(destination, new MessageCreator()
            {
                @Override
                public Message createMessage(Session session) throws JMSException
                {
                    BytesMessage byteMessage = session.createBytesMessage();
                    try
                    {
                        byteMessage.writeBytes(messageContent.getBytes("UTF-8"));
                        return byteMessage;
                    } 
                    catch (UnsupportedEncodingException e)
                    {
                        e.printStackTrace();
                        throw new JMSException(e.getMessage());
                    }
                }
            });         
        }
        catch(Exception e)
        {
            throw new SendingMessageException(e.getMessage(),e);
        }
    }

    public void setJmsTemplate(JmsTemplate jmsTemplate)
    {
        this.jmsTemplate = jmsTemplate;
    }
}

thanks in advance.

Arya
  • 2,809
  • 5
  • 34
  • 56

1 Answers1

0

It is just a guess, but it could be because Spring's JmsTemplate re-throws exceptions as JmsException which is a Runtime exception, so you need to catch Throwable to actually catch it.

Marina
  • 3,894
  • 9
  • 34
  • 41
  • RuntimeException extends Exception, i think. – Arya Jul 12 '13 at 19:29
  • yes, you are right - please disregard my comment - I was looking at this post and missed that you already are catching top Exception: http://forum.springsource.org/showthread.php?95919-Exception-handling-while-doing-a-jmsTemplate-send – Marina Jul 14 '13 at 14:59
  • Its not recommended to catch Exception its to abstract. I give you a point for giving me the name of the exception :) JmsException – JWqvist Oct 27 '15 at 08:54
  • This is mostly wrong. `JmsException` is not a subclass of `RuntimeException`. I suggest the following guideline for catching `Throwable`: if you're not sure whether or not you should catch `Throwable`, you shouldn't. – augurar Feb 24 '18 at 13:09