Question

    public void run() {
    jmsTemplate.send(new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
            byte[] buf = createBytesMessage(5120);
            BytesMessage message = session.createBytesMessage();
            message.writeBytes(buf);
            message.setLongProperty("_publish_time", System.currentTimeMillis());
            return message;
        }   
    });
}

I have this code snippet, what I can make out is its using anonymous class. But I am confused upon how the createMessage() method will be called when the run() is invoked by thread?

Also somewhere I read there is nothing like "anonymous class" but instead its "anonymous inner class". Why its like that?

Was it helpful?

Solution

But I am confused upon how the createMessage() method will be called when the run() is invoked by thread?

The jmsTemplate will expect a MessageCreator instance to be passed and it will responsible for calling the MessageCreator's createMessage() method.

Also somewhere I read there is nothing like "anonymous class" but instead its "anonymous inner class". Why its like that?

Yes. The proper definition is anonymous inner class, since there is no such thing as anonymous public class. While local classes are class declarations, anonymous classes are expressions, which means that you define the class in another expression and so they are called inner.

More info:

OTHER TIPS

It's an "Anonymous inner class" because it isn't a class that stands by itself, it's a class with some abstract method that you've declared within another class.

As for the method itself, before I look up the class I'm assuming that the handler you send it to (msTemplate) just accepts an interface with the method createMessage

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top