org.springframework.jms.connection.CachingConnectionFactory 提供用户名和密码

问题描述 投票:0回答:1

如何在

org.springframework.jms.connection.CachingConnectionFactory
中提供用户名和密码? (不是兔子)。

使用 Apache Camel,我正在尝试以下操作:

ActiveMQComponent amqComponent = new ActiveMQComponent();
//amqComponent.setUsername(amqUser);
//amqComponent.setPassword(amqPassword);
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
cf.setBrokerURL("tcp://" + amqServer + ":" + amqPort + "?jms.watchTopicAdvisories=false");

CachingConnectionFactory ccf = new CachingConnectionFactory(cf);
ccf.setClientId("clientID");
amqComponent.setConnectionFactory(ccf);

context.addComponent("activemq", amqComponent);

如果我取消注释

.setUsername
.setPassword
调用,则会收到错误:

“SingleConnectionFactory 不支持自定义用户名和密码”

(因为

SingleConnectionFactory
是这个
CachingConnectionFactory
的父类)。

显然,如果我保留注释行,我会收到身份验证错误,因为我没有提供用户/密码。

使用

CachingConnectionFactory
时如何传递 ActiveMQ 凭证?

(顺便说一句,Spring Integration:如何将 SingleConnectionFactory 与 ActiveMQ 结合使用? 讨论了为什么我尝试使用 CCF 而不是仅使用 ActiveMQConnectionFactory - “每次轮询时,它都会创建一个到 Broker 的新连接 - 泛洪我的经纪人有数百个联系”)

java spring apache-camel activemq-artemis
1个回答
0
投票

您需要使用

UserCredentialsConnectionFactoryAdaptor
,例如如下:

ActiveMQComponent amqComponent = new ActiveMQComponent();
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
cf.setBrokerURL("tcp://" + amqServer + ":" + amqPort + " jms.watchTopicAdvisories=false");
       
UserCredentialsConnectionFactoryAdapter uca = new UserCredentialsConnectionFactoryAdapter();
uca.setUsername(amqUser);
uca.setPassword(amqPassword);
uca.setTargetConnectionFactory(cf);
            
CachingConnectionFactory ccf = new CachingConnectionFactory(uca);
ccf.setClientId(amqClientID);
            
amqComponent.setConnectionFactory(ccf);
amqComponent.setMaxConcurrentConsumers(1);
            
context.addComponent("activemq", amqComponent);
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.