像bean方法一样配置brokerURL

时间:2016-06-02 15:56:29

标签: java spring jms activemq

我需要配置JMS发送方。但我必须有机会在属性文件中设置DNS名称。我还有应该添加到DNS的参数。我决定创建一个bean,它将完整的url返回给broker。但我有一个启动问题。 所以这是我的XML:

<bean id="path" class="com.promptlink.stbtp.Config" factory-method="getJMSPath"/>


<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" ref="path"/>
</bean>

这是最后一个链接:

nio://10.20.6.192:9091?connectionTimeout=3000&amp;jms.useAsyncSend=true&amp;wireFormat.tightEncodingEnabled=false 

如果我将链接直接设置为属性的值 - 没有问题。 JMS开局不错。但是当我试图将链接作为bean传递时 - 它崩溃了。例外:

javax.jms.JMSException: Error while attempting to add new Connection to the pool
    at org.apache.activemq.jms.pool.PooledConnectionFactory.createJmsException(PooledConnectionFactory.java:251)
    at org.apache.activemq.jms.pool.PooledConnectionFactory.createConnection(PooledConnectionFactory.java:210)
    at org.apache.activemq.jms.pool.PooledConnectionFactory.createConnection(PooledConnectionFactory.java:189)
    at org.apache.activemq.jms.pool.PooledConnectionFactory.start(PooledConnectionFactory.java:271)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1706)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1645)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at com.promptlink.stbtp.messaging.NodeMessageDispatcher.getInstance(NodeMessageDispatcher.java:20)
    at com.promptlink.stbtp.messaging.NodeMessageDispatcher.sendToWebapp(NodeMessageDispatcher.java:33)
    at com.promptlink.stbtp.Config.init(Config.java:170)
    at com.promptlink.stbtp.App.main(App.java:159)
Caused by: javax.jms.JMSException: Could not create Transport. Reason: java.lang.IllegalArgumentException: Invalid connect parameters: {amp;wireFormat.tightEncodingEnabled=false, amp;jms.useAsyncSend=true}
    at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:36)
    at org.apache.activemq.ActiveMQConnectionFactory.createTransport(ActiveMQConnectionFactory.java:317)
    at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:330)
    at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:303)
    at org.apache.activemq.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:243)
    at org.apache.activemq.jms.pool.PooledConnectionFactory.createConnection(PooledConnectionFactory.java:259)
    at org.apache.activemq.jms.pool.PooledConnectionFactory$1.makeObject(PooledConnectionFactory.java:104)
    at org.apache.activemq.jms.pool.PooledConnectionFactory$1.makeObject(PooledConnectionFactory.java:84)
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.addObject(GenericKeyedObjectPool.java:1748)
    at org.apache.activemq.jms.pool.PooledConnectionFactory.createConnection(PooledConnectionFactory.java:206)
    ... 24 more
Caused by: java.lang.IllegalArgumentException: Invalid connect parameters: {amp;wireFormat.tightEncodingEnabled=false, amp;jms.useAsyncSend=true}
    at org.apache.activemq.transport.TransportFactory.doConnect(TransportFactory.java:122)
    at org.apache.activemq.transport.TransportFactory.connect(TransportFactory.java:64)
    at org.apache.activemq.ActiveMQConnectionFactory.createTransport(ActiveMQConnectionFactory.java:315)
    ... 32 more

那么我做错了什么,以及如何解决这个问题?我只需要 - 它将URL传递给JMS代理。

1 个答案:

答案 0 :(得分:2)

这与配置上下文有关。在XML中指定值时 该 require('Connections.php'); 被正确解码为 &安培; 由XML解析器。所以传递给brokerURL属性的实际值是nio://10.20.6.192:9091?connectionTimeout = 3000&amp; jms.useAsyncSend = true&amp; wireFormat.tightEncodingEnabled = false

这是因为&amp;是XML中的特殊字符,需要进行转义

使用代码生成URL时,请确保使用正确的格式

&amp;