在 ActiveMq 中实现 MessageCreator 时出现编译时错误

Getting Compile time error while implementing MessageCreator in ActiveMq

我正在尝试进入 ActiveMq 并按照下面的教程进行操作 link:

Spring-ActiveMq Example

在创建 ActiveMQMessageProducer class 时,我收到以下行的编译时错误:

textMessage.setIntProperty(MSG_COUNT, messageCount);

错误是

"Cannot refer to the non-final local variable messageCount defined in an enclosing scope"

我不明白它是如何根据教程工作的。

谢谢

本教程中存在一个错误,因为内部匿名方法 class 不能引用非最终变量。这是一个固定版本:

   public void generateMessages() throws JMSException 
{
    for (int messageCount = 0; messageCount < 10; messageCount++) 
    {
        final String text = "TP Message " + messageCount;
        final int count = messageCount; // copy the value into a final
        jmsTemplate.send(new MessageCreator() 
        {
            public Message createMessage(Session session) throws JMSException
            {
                TextMessage textMessage = session.createTextMessage(text);
                textMessage.setIntProperty(MSG_COUNT, count); // use it             
                return textMessage;
            }
        });
    }
}