1

I'm trying to use Spring profiles to selectively declare an ActiveMQ Broker. I'm also using the amq:broker namespace. However I keep getting a "Caused by: org.xml.sax.SAXParseException; lineNumber: 179; columnNumber: 129; cvc-id.2: There are multiple occurrences of ID value 'messageCentreBroker'." error on Context initialisation.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:amq="http://activemq.apache.org/schema/core"         
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="
              http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
              http://www.springframework.org/schema/jms 
              http://www.springframework.org/schema/jms/spring-jms-3.0.xsd 
              http://activemq.apache.org/schema/core 
              http://activemq.apache.org/schema/core/activemq-core.xsd">

    <beans profile="dev">   
        <amq:broker id="messageCentreBroker" brokerName="messageCentreBroker" useJmx="true" persistent="true" advisorySupport="true">   
                ... other stuff particular to "dev" profile ...
        </amq:broker>
    </beans>
    <beans profile="local">
        <amq:broker id="messageCentreBroker" brokerName="messageCentreBroker" useJmx="true" persistent="true" advisorySupport="true">   
                ... other stuff particular to "local" profile...
        </amq:broker>   
    </beans>    
</beans>

And here is the full stacktrace too

2013-05-31 10:34:55,807 [DEBUG] org.springframework.beans.factory.xml.PluggableSchemaResolver: Found XML schema [http://activemq.apache.org/schema/core/activemq-core.xsd] in classpath: activemq.xsd
2013-05-31 10:34:56,128 [ERROR] org.springframework.web.context.ContextLoader: Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 179 in XML document from class path resource [au/gov/dha/messagecentre/messagecentre-jms.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 179; columnNumber: 129; cvc-id.2: There are multiple occurrences of ID value 'messageCentreBroker'.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.xml.sax.SAXParseException; lineNumber: 179; columnNumber: 129; cvc-id.2: There are multiple occurrences of ID value 'messageCentreBroker'.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.processOneAttribute(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.processAttributes(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
    ... 21 more

My web.xml is currently selecting the profile e.g. (without placeholder)

<servlet>
    <servlet-name>messagecentre</servlet-name>
    <init-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>local</param-value>
    </init-param>
</servlet>
Wayne Earnshaw
  • 527
  • 6
  • 15

1 Answers1

1

Your stacktrace indicates that you are currently defining an ApplicationContext through ContextLoaderListener.

ContextLoaderListener does not uses init-param but context-param. So try using in your web.xml

<context-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>local</param-value>
</context-param>
Guillaume Darmont
  • 5,002
  • 1
  • 23
  • 35
  • Thanks! I had stumbled on http://forum.springsource.org/showthread.php?119190-Spring-Profiles-not-working-when-configured-thru-lt-init-param-gt too which is similar, sorry I didn't get back here to update with the answer before you did (a little busy as we all are :) – Wayne Earnshaw Jun 04 '13 at 23:31
  • A `ContextProfileInitializer` that `implements ApplicationContextInitializer` is also a good idea if you don't want to use an explicit `context-param`. For my specific problem I also found Spring Profiles don't work with the ActiveMQ namespace i.e. `amq:broker` etc... So avoid those and only use standard `` definitions of the ActiveMQ configuration elements. – Wayne Earnshaw Jul 16 '13 at 01:52
  • Maybe the amq namespaces are not working with profiles due to the XSD version you're using - must be >= 3.1 to use profile feature. See also [this answer](http://stackoverflow.com/a/33003965/993262) – Stephen Hartley Oct 07 '15 at 23:20