0

Here is my problem: I have to read a property (activemq.connector.port) and add 2 to that and use that to set an attribute (connectorPort) on the bean ManagementContext. Here the property comes from a file or it could be system property. Spring does not really care I suppose.

 <amq:broker id="broker" useJmx="true" persistent="true">
       <amq:managementContext createConnector="true" connectorPort="#{${activemq.connector.port}+2}"/>
       <amq:transportConnectors>
           <amq:transportConnector uri="vm://localhost:${activemq.connector.port:1099}"/>
       </amq:transportConnectors>
 </amq:broker> 

When I tried to do this I got this exception.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '{1099 + 2 }' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:568)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)

How do I make this work? Are there any alternative approaches to achieve this?

FourOfAKind
  • 2,298
  • 5
  • 31
  • 34

2 Answers2

1

Before all just a try: "#{2 + ${activemq.connector .port}}" to Force spEL use placeholder as numbers.

Else Use this syntax (is the spEL notation for Java code Integer.valueOf(value of 'activemq.connector.port') + 2):

#{ T(java.lang.Integer).valueOf('${activemq.connector.port}') + 2 }

Probably ${} are managed as String and not as Number so a "cast" is needed T(Class).<staticMethod>(params) is the spEL syntax to call a static method

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
0

I got a very similar issue. I am using Spring 4.2.7.RELEASE and ActiveMQ 5.13.4. I prefer an XML file to configure activeMQ, so I set up an XML file with the following:

    <persistenceAdapter>
        <kahaDB 
            directory="${activemq.persistence.directory}/KahaDB"
            journalMaxFileLength="#{100*1024*1024}"
            indexWriteBatchSize="100"
            enableIndexWriteAsync="true"
            concurrentStoreAndDispatchQueues="false"
            concurrentStoreAndDispatchTopics="false" />
    </persistenceAdapter>

Then I got the following error message: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '{100*1024*1024}' is defined.

I can work around the issue by adding a space before the #. Strange... It looks like there is another escape mechanism. I didn't understand.

In my case, I solve the issue by using the syntax "100 Mb" that is more convenient.

mcoolive
  • 3,805
  • 1
  • 27
  • 32