我正在尝试在spring boot / spring jms应用程序中连接到远程HornetQ代理,并设置一个@JmsListener
。
HornetQ ConnectionFactory
从HornetQ实例托管的JNDI注册表中获取。只要关闭HornetQ安全性,一切都可以正常工作,但是当它打开时,我会收到此错误
WARN o.s.j.l.DefaultMessageListenerContainer : Setup of JMS message listener invoker failed for destination 'jms/MI/Notification/Queue' - trying to recover. Cause: User: null doesn't have permission='CONSUME' on address jms.queue.MI/Notification/Queue
我运行了一个调试会话,以确定返回的ConnectionFactory
实例为HornetQXAConnectionFactory
,但未设置用户和密码字段,我相信这就是为什么用户为null的原因。我验证了在JNDI属性中设置了用户主体和凭证,但是不知何故未将其传递给ConnectionFactory实例。我将如何获得此设置的任何帮助将不胜感激。
这是我与jms相关的配置
@Configuration
@EnableJms
public class JmsConfig {
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
factory.setDestinationResolver(destinationResolver());
return factory;
}
@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.BYTES);
converter.setTypeIdPropertyName("_type");
return converter;
}
@Value("${jms.jndi.provider.url}")
private String jndiProviderURL;
@Value("${jms.jndi.principal}")
private String jndiPrincipal;
@Value("${jms.jndi.credentials}")
private String jndiCredential;
@Bean
public JndiTemplate jndiTemplate() {
Properties env = new Properties();
env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.provider.url", jndiProviderURL);
env.put("java.naming.security.principal", jndiPrincipal);
env.put("java.naming.security.credentials", jndiCredential);
return new JndiTemplate(env);
}
@Bean
public DestinationResolver destinationResolver() {
JndiDestinationResolver destinationResolver = new JndiDestinationResolver();
destinationResolver.setJndiTemplate(jndiTemplate());
return destinationResolver;
}
@Value("${jms.connectionfactory.jndiname}")
private String connectionFactoryJNDIName;
@Bean
public JndiObjectFactoryBean connectionFactoryFactory() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
jndiObjectFactoryBean.setJndiName(connectionFactoryJNDIName);
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setProxyInterface(ConnectionFactory.class);
return jndiObjectFactoryBean;
}
@Bean
public ConnectionFactory connectionFactory(JndiObjectFactoryBean connectionFactoryFactory) {
return (ConnectionFactory) connectionFactoryFactory.getObject();
}
}
答案 0 :(得分:1)
JNDI和JMS是100%独立的,因为它们是完全不同的规范,以可能完全不同的方式实现。因此,用于JNDI查找的凭据不适用于您的JMS资源。您需要在JMS连接上显式设置用户名和密码凭证。直接使用JMS API(例如通过javax.jms.ConnectionFactory#createConnection(String username, String password)
)很容易。由于您使用的是Spring,因此您可以使用以下内容:
@Bean
public ConnectionFactory connectionFactory(JndiObjectFactoryBean connectionFactoryFactory) {
UserCredentialsConnectionFactoryAdapter cf = new UserCredentialsConnectionFactoryAdapter();
cf.setTargetConnectionFactory((ConnectionFactory) connectionFactoryFactory.getObject());
cf.setUsername("yourJmsUsername");
cf.setPassword("yourJmsPassword");
return cf;
}
而且,就其价值而言,the HornetQ code-base was donated to the Apache ActiveMQ project已有三年半了,它一直以Apache ActiveMQ Artemis经纪人身份存在。从那时起,已经发布了22个版本,其中包含许多新功能和错误修复。我强烈建议您尽可能迁移。
答案 1 :(得分:1)
将连接工厂包装在UserCredentialsConnectionFactoryAdapter
中。
/**
* An adapter for a target JMS {@link javax.jms.ConnectionFactory}, applying the
* given user credentials to every standard {@code createConnection()} call,
* that is, implicitly invoking {@code createConnection(username, password)}
* on the target. All other methods simply delegate to the corresponding methods
* of the target ConnectionFactory.
* ...